VampireAchao 2 lat temu
rodzic
commit
c0550c4d46

+ 6 - 6
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/StaticService.java → mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/Db.java

@@ -36,15 +36,15 @@ import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
  * @author VampireAchao
  * @since 2022-05-03
  */
-public class StaticService {
+public class Db {
 
     /**
      * 默认批次提交数量
      */
     public static final int DEFAULT_BATCH_SIZE = 1000;
-    private static final Log log = LogFactory.getLog(StaticService.class);
+    private static final Log log = LogFactory.getLog(Db.class);
 
-    private StaticService() {
+    private Db() {
         /* Do not new me! */
     }
 
@@ -85,7 +85,7 @@ public class StaticService {
         Class<T> entityClass = getEntityClass(entityList);
         Class<?> mapperClass = ClassUtils.toClassConfident(getTableInfo(entityClass).getCurrentNamespace());
         String sqlStatement = SqlHelper.getSqlStatement(mapperClass, SqlMethod.INSERT_ONE);
-        return SqlHelper.executeBatch(entityClass, LogFactory.getLog(StaticService.class), entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
+        return SqlHelper.executeBatch(entityClass, LogFactory.getLog(Db.class), entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
     }
 
     /**
@@ -112,7 +112,7 @@ public class StaticService {
         Class<?> mapperClass = ClassUtils.toClassConfident(tableInfo.getCurrentNamespace());
         String keyProperty = tableInfo.getKeyProperty();
         Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for primary key from entity!");
-        return SqlHelper.saveOrUpdateBatch(entityClass, mapperClass, LogFactory.getLog(StaticService.class), entityList, batchSize, (sqlSession, entity) -> {
+        return SqlHelper.saveOrUpdateBatch(entityClass, mapperClass, LogFactory.getLog(Db.class), entityList, batchSize, (sqlSession, entity) -> {
             Object idVal = tableInfo.getPropertyValue(entity, keyProperty);
             return StringUtils.checkValNull(idVal)
                 || CollectionUtils.isEmpty(sqlSession.selectList(SqlHelper.getSqlStatement(mapperClass, SqlMethod.SELECT_BY_ID), entity));
@@ -208,7 +208,7 @@ public class StaticService {
         Class<T> entityClass = getEntityClass(entityList);
         TableInfo tableInfo = getTableInfo(entityClass);
         String sqlStatement = SqlHelper.getSqlStatement(ClassUtils.toClassConfident(tableInfo.getCurrentNamespace()), SqlMethod.UPDATE_BY_ID);
-        return SqlHelper.executeBatch(entityClass, LogFactory.getLog(StaticService.class), entityList, batchSize, (sqlSession, entity) -> {
+        return SqlHelper.executeBatch(entityClass, LogFactory.getLog(Db.class), entityList, batchSize, (sqlSession, entity) -> {
             MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
             param.put(Constants.ENTITY, entity);
             sqlSession.update(sqlStatement, param);

+ 52 - 52
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/toolkit/StaticServiceTest.java → mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/toolkit/DbTest.java

@@ -22,7 +22,7 @@ import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
 import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import com.baomidou.mybatisplus.extension.toolkit.StaticService;
+import com.baomidou.mybatisplus.extension.toolkit.Db;
 import com.baomidou.mybatisplus.test.BaseDbTest;
 import com.baomidou.mybatisplus.test.sqlrunner.Entity;
 import com.baomidou.mybatisplus.test.sqlrunner.EntityMapper;
@@ -33,23 +33,23 @@ import com.baomidou.mybatisplus.test.sqlrunner.EntityMapper;
  * @author VampireAchao
  * @since 2022-05-03
  */
-class StaticServiceTest extends BaseDbTest<EntityMapper> {
+class DbTest extends BaseDbTest<EntityMapper> {
 
     @Test
     void testSave() {
         Entity entity = new Entity();
         entity.setName("ruben");
-        boolean isSuccess = StaticService.save(entity);
+        boolean isSuccess = Db.save(entity);
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals(3L, StaticService.count(Entity.class));
+        Assertions.assertEquals(3L, Db.count(Entity.class));
     }
 
     @Test
     void testSaveBatch() {
         List<Entity> list = Arrays.asList(new Entity(), new Entity());
-        boolean isSuccess = StaticService.saveBatch(list);
+        boolean isSuccess = Db.saveBatch(list);
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals(4, StaticService.count(Entity.class));
+        Assertions.assertEquals(4, Db.count(Entity.class));
     }
 
     @Test
@@ -58,21 +58,21 @@ class StaticServiceTest extends BaseDbTest<EntityMapper> {
         entity.setId(1L);
         entity.setName("cat");
         List<Entity> list = Arrays.asList(new Entity(), entity);
-        boolean isSuccess = StaticService.saveOrUpdateBatch(list);
+        boolean isSuccess = Db.saveOrUpdateBatch(list);
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals(3, StaticService.count(Entity.class));
+        Assertions.assertEquals(3, Db.count(Entity.class));
     }
 
     @Test
     void testRemoveById() {
         Entity entity = new Entity();
         entity.setId(1L);
-        boolean isSuccess = StaticService.removeById(entity);
+        boolean isSuccess = Db.removeById(entity);
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals(1, StaticService.count(Entity.class));
-        isSuccess = StaticService.removeById(2L, Entity.class);
+        Assertions.assertEquals(1, Db.count(Entity.class));
+        isSuccess = Db.removeById(2L, Entity.class);
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals(0, StaticService.count(Entity.class));
+        Assertions.assertEquals(0, Db.count(Entity.class));
     }
 
     @Test
@@ -80,23 +80,23 @@ class StaticServiceTest extends BaseDbTest<EntityMapper> {
         Entity entity = new Entity();
         entity.setId(1L);
         entity.setName("bee bee I'm a sheep");
-        boolean isSuccess = StaticService.updateById(entity);
+        boolean isSuccess = Db.updateById(entity);
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals("bee bee I'm a sheep", StaticService.getById(1L, Entity.class).getName());
+        Assertions.assertEquals("bee bee I'm a sheep", Db.getById(1L, Entity.class).getName());
     }
 
     @Test
     void testUpdate() {
-        boolean isSuccess = StaticService.update(Wrappers.lambdaUpdate(Entity.class).eq(Entity::getId, 1L).set(Entity::getName, "be better"));
+        boolean isSuccess = Db.update(Wrappers.lambdaUpdate(Entity.class).eq(Entity::getId, 1L).set(Entity::getName, "be better"));
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals("be better", StaticService.getById(1L, Entity.class).getName());
+        Assertions.assertEquals("be better", Db.getById(1L, Entity.class).getName());
 
         Entity entity = new Entity();
         entity.setId(1L);
         entity.setName("bee bee I'm a sheep");
-        isSuccess = StaticService.update(entity, Wrappers.lambdaQuery(Entity.class).eq(Entity::getId, 1L));
+        isSuccess = Db.update(entity, Wrappers.lambdaQuery(Entity.class).eq(Entity::getId, 1L));
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals("bee bee I'm a sheep", StaticService.getById(1L, Entity.class).getName());
+        Assertions.assertEquals("bee bee I'm a sheep", Db.getById(1L, Entity.class).getName());
     }
 
     @Test
@@ -108,31 +108,31 @@ class StaticServiceTest extends BaseDbTest<EntityMapper> {
         Entity ruben = new Entity();
         ruben.setId(2L);
         ruben.setName("rabbit");
-        boolean isSuccess = StaticService.updateBatchById(Arrays.asList(sheep, ruben));
+        boolean isSuccess = Db.updateBatchById(Arrays.asList(sheep, ruben));
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals("bee bee I'm a sheep", StaticService.getById(1L, Entity.class).getName());
-        Assertions.assertEquals("rabbit", StaticService.getById(2L, Entity.class).getName());
+        Assertions.assertEquals("bee bee I'm a sheep", Db.getById(1L, Entity.class).getName());
+        Assertions.assertEquals("rabbit", Db.getById(2L, Entity.class).getName());
     }
 
     @Test
     void testRemove() {
-        boolean isSuccess = StaticService.remove(Wrappers.lambdaQuery(Entity.class).eq(Entity::getId, 1L));
+        boolean isSuccess = Db.remove(Wrappers.lambdaQuery(Entity.class).eq(Entity::getId, 1L));
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals(1, StaticService.count(Entity.class));
+        Assertions.assertEquals(1, Db.count(Entity.class));
     }
 
     @Test
     void testRemoveByIds() {
-        boolean isSuccess = StaticService.removeByIds(Arrays.asList(1L, 2L), Entity.class);
+        boolean isSuccess = Db.removeByIds(Arrays.asList(1L, 2L), Entity.class);
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals(0, StaticService.count(Entity.class));
+        Assertions.assertEquals(0, Db.count(Entity.class));
     }
 
     @Test
     void testRemoveByMap() {
-        boolean isSuccess = StaticService.removeByMap(Collections.singletonMap("id", 1L), Entity.class);
+        boolean isSuccess = Db.removeByMap(Collections.singletonMap("id", 1L), Entity.class);
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals(1, StaticService.count(Entity.class));
+        Assertions.assertEquals(1, Db.count(Entity.class));
     }
 
     @Test
@@ -140,21 +140,21 @@ class StaticServiceTest extends BaseDbTest<EntityMapper> {
         Entity entity = new Entity();
         entity.setId(null);
         entity.setName("bee bee I'm a sheep");
-        boolean isSuccess = StaticService.saveOrUpdate(entity);
+        boolean isSuccess = Db.saveOrUpdate(entity);
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals("bee bee I'm a sheep", StaticService.getById(entity.getId(), Entity.class).getName());
+        Assertions.assertEquals("bee bee I'm a sheep", Db.getById(entity.getId(), Entity.class).getName());
 
         entity.setName("be better");
-        isSuccess = StaticService.saveOrUpdate(entity, Wrappers.lambdaQuery(Entity.class).eq(Entity::getId, entity.getId()));
+        isSuccess = Db.saveOrUpdate(entity, Wrappers.lambdaQuery(Entity.class).eq(Entity::getId, entity.getId()));
         Assertions.assertTrue(isSuccess);
-        Assertions.assertEquals("be better", StaticService.getById(entity.getId(), Entity.class).getName());
+        Assertions.assertEquals("be better", Db.getById(entity.getId(), Entity.class).getName());
     }
 
     @Test
     void testGetOne() {
         LambdaQueryWrapper<Entity> wrapper = Wrappers.lambdaQuery(Entity.class);
-        Assertions.assertThrows(MybatisPlusException.class, () -> StaticService.getOne(wrapper));
-        Entity one = StaticService.getOne(wrapper, false);
+        Assertions.assertThrows(MybatisPlusException.class, () -> Db.getOne(wrapper));
+        Entity one = Db.getOne(wrapper, false);
         Assertions.assertNotNull(one);
     }
 
@@ -162,50 +162,50 @@ class StaticServiceTest extends BaseDbTest<EntityMapper> {
     void testListByMap() {
         Map<String, Object> map = new HashMap<>();
         map.put("id", 1L);
-        List<Entity> list = StaticService.listByMap(map, Entity.class);
+        List<Entity> list = Db.listByMap(map, Entity.class);
         Assertions.assertEquals(1, list.size());
         Assertions.assertEquals("ruben", list.get(0).getName());
     }
 
     @Test
     void testByIds() {
-        List<Entity> list = StaticService.listByIds(Arrays.asList(1L, 2L), Entity.class);
+        List<Entity> list = Db.listByIds(Arrays.asList(1L, 2L), Entity.class);
         Assertions.assertEquals(2, list.size());
     }
 
     @Test
     void testGetMap() {
-        Map<String, Object> map = StaticService.getMap(Wrappers.lambdaQuery(Entity.class));
+        Map<String, Object> map = Db.getMap(Wrappers.lambdaQuery(Entity.class));
         Assertions.assertNotNull(map);
     }
 
     @Test
     void testList() {
-        List<Entity> list = StaticService.list(Wrappers.lambdaQuery(Entity.class));
+        List<Entity> list = Db.list(Wrappers.lambdaQuery(Entity.class));
         Assertions.assertEquals(2, list.size());
 
-        list = StaticService.list(Entity.class);
+        list = Db.list(Entity.class);
         Assertions.assertEquals(2, list.size());
     }
 
     @Test
     void testListMaps() {
-        List<Map<String, Object>> list = StaticService.listMaps(Wrappers.lambdaQuery(Entity.class));
+        List<Map<String, Object>> list = Db.listMaps(Wrappers.lambdaQuery(Entity.class));
         Assertions.assertEquals(2, list.size());
 
-        list = StaticService.listMaps(Entity.class);
+        list = Db.listMaps(Entity.class);
         Assertions.assertEquals(2, list.size());
     }
 
     @Test
     void testListObjs() {
-        List<Entity> list = StaticService.listObjs(Entity.class);
+        List<Entity> list = Db.listObjs(Entity.class);
         Assertions.assertEquals(2, list.size());
 
-        List<Long> objectList = StaticService.listObjs(Wrappers.lambdaQuery(Entity.class), Entity::getId);
+        List<Long> objectList = Db.listObjs(Wrappers.lambdaQuery(Entity.class), Entity::getId);
         Assertions.assertEquals(2, objectList.size());
 
-        List<String> names = StaticService.listObjs(Entity.class, Entity::getName);
+        List<String> names = Db.listObjs(Entity.class, Entity::getName);
         Assertions.assertArrayEquals(new String[]{"ruben", "chocolate"}, names.toArray());
     }
 
@@ -218,44 +218,44 @@ class StaticServiceTest extends BaseDbTest<EntityMapper> {
 
     @Test
     void testPageMaps() {
-        Page<Map<String, Object>> page = StaticService.pageMaps(new Page<>(1, 1), Entity.class);
+        Page<Map<String, Object>> page = Db.pageMaps(new Page<>(1, 1), Entity.class);
         Assertions.assertEquals(2, page.getTotal());
 
-        page = StaticService.pageMaps(new Page<>(1, 1), Wrappers.lambdaQuery(Entity.class));
+        page = Db.pageMaps(new Page<>(1, 1), Wrappers.lambdaQuery(Entity.class));
         Assertions.assertEquals(1, page.getRecords().size());
     }
 
     @Test
     void testPage() {
-        IPage<Entity> page = StaticService.page(new Page<>(1, 1), Entity.class);
+        IPage<Entity> page = Db.page(new Page<>(1, 1), Entity.class);
         Assertions.assertEquals(2, page.getTotal());
 
-        page = StaticService.page(new Page<>(1, 1), Wrappers.lambdaQuery(Entity.class));
+        page = Db.page(new Page<>(1, 1), Wrappers.lambdaQuery(Entity.class));
         Assertions.assertEquals(1, page.getRecords().size());
     }
 
     @Test
     void testChain() {
-        QueryChainWrapper<Entity> query = StaticService.query(Entity.class);
+        QueryChainWrapper<Entity> query = Db.query(Entity.class);
         List<Entity> list = query.eq("id", 1L).list();
         Assertions.assertEquals(1, list.size());
 
-        LambdaQueryChainWrapper<Entity> lambdaQuery = StaticService.lambdaQuery(Entity.class);
+        LambdaQueryChainWrapper<Entity> lambdaQuery = Db.lambdaQuery(Entity.class);
         list = lambdaQuery.eq(Entity::getId, 1L).list();
         Assertions.assertEquals(1, list.size());
 
-        UpdateChainWrapper<Entity> update = StaticService.update(Entity.class);
+        UpdateChainWrapper<Entity> update = Db.update(Entity.class);
         update.eq("id", 1L).set("name", "bee bee I'm a sheep").update();
         Assertions.assertEquals("bee bee I'm a sheep", lambdaQuery.eq(Entity::getId, 1L).one().getName());
 
-        LambdaUpdateChainWrapper<Entity> lambdaUpdate = StaticService.lambdaUpdate(Entity.class);
+        LambdaUpdateChainWrapper<Entity> lambdaUpdate = Db.lambdaUpdate(Entity.class);
         lambdaUpdate.eq(Entity::getId, 1L).set(Entity::getName, "be better").update();
         Assertions.assertEquals("be better", lambdaQuery.eq(Entity::getId, 1L).one().getName());
     }
 
     @Test
     void testGetObj() {
-        String name = StaticService.getObj(Wrappers.lambdaQuery(Entity.class).eq(Entity::getId, 1L), Entity::getName);
+        String name = Db.getObj(Wrappers.lambdaQuery(Entity.class).eq(Entity::getId, 1L), Entity::getName);
         Assertions.assertEquals("ruben", name);
     }