Jelajahi Sumber

重命名新增方法.

nieqiurong 1 tahun lalu
induk
melakukan
3934b08861

+ 16 - 16
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/mapper/BaseMapper.java

@@ -169,8 +169,8 @@ public interface BaseMapper<T> extends Mapper<T> {
      *
      * @param idList 主键ID列表或实体列表(不能为 null 以及 empty)
      */
-    default int deleteBatchIds(@Param(Constants.COLL) Collection<?> idList) {
-        return deleteBatchIds(idList, true);
+    default int deleteByIds(@Param(Constants.COLL) Collection<?> idList) {
+        return deleteByIds(idList, true);
     }
 
     /**
@@ -180,7 +180,7 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param useFill     逻辑删除下是否填充
      * @since 3.5.7
      */
-    default int deleteBatchIds(@Param(Constants.COLL) Collection<?> collections, boolean useFill) {
+    default int deleteByIds(@Param(Constants.COLL) Collection<?> collections, boolean useFill) {
         MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
         Class<?> entityClass = GenericTypeUtils.resolveTypeArguments(getClass(), BaseMapper.class)[0];
         SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
@@ -453,8 +453,8 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param entityList 实体对象集合
      * @since 3.5.7
      */
-    default List<BatchResult> saveBatch(Collection<T> entityList) {
-        return saveBatch(entityList, Constants.DEFAULT_BATCH_SIZE);
+    default List<BatchResult> insert(Collection<T> entityList) {
+        return insert(entityList, Constants.DEFAULT_BATCH_SIZE);
     }
 
     /**
@@ -464,7 +464,7 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param batchSize  插入批次数量
      * @since 3.5.7
      */
-    default List<BatchResult> saveBatch(Collection<T> entityList, int batchSize) {
+    default List<BatchResult> insert(Collection<T> entityList, int batchSize) {
         MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
         MybatisBatch.Method<T> method = new MybatisBatch.Method<>(mybatisMapperProxy.getMapperInterface());
         SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory(mybatisMapperProxy);
@@ -477,8 +477,8 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param entityList 实体对象集合
      * @since 3.5.7
      */
-    default List<BatchResult> updateBatchById(Collection<T> entityList) {
-        return updateBatchById(entityList, Constants.DEFAULT_BATCH_SIZE);
+    default List<BatchResult> updateById(Collection<T> entityList) {
+        return updateById(entityList, Constants.DEFAULT_BATCH_SIZE);
     }
 
     /**
@@ -488,7 +488,7 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param batchSize  插入批次数量
      * @since 3.5.7
      */
-    default List<BatchResult> updateBatchById(Collection<T> entityList, int batchSize) {
+    default List<BatchResult> updateById(Collection<T> entityList, int batchSize) {
         MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
         MybatisBatch.Method<T> method = new MybatisBatch.Method<>(mybatisMapperProxy.getMapperInterface());
         SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory(mybatisMapperProxy);
@@ -501,8 +501,8 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param entityList 实体对象集合
      * @since 3.5.7
      */
-    default List<BatchResult> saveOrUpdateBatch(Collection<T> entityList) {
-        return saveOrUpdateBatch(entityList, Constants.DEFAULT_BATCH_SIZE);
+    default List<BatchResult> insertOrUpdate(Collection<T> entityList) {
+        return insertOrUpdate(entityList, Constants.DEFAULT_BATCH_SIZE);
     }
 
     /**
@@ -512,13 +512,13 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param batchSize  插入批次数量
      * @since 3.5.7
      */
-    default List<BatchResult> saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
+    default List<BatchResult> insertOrUpdate(Collection<T> entityList, int batchSize) {
         MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
         Class<?> entityClass = GenericTypeUtils.resolveTypeArguments(getClass(), BaseMapper.class)[0];
         TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
         String keyProperty = tableInfo.getKeyProperty();
         String statement = mybatisMapperProxy.getMapperInterface().getName() + StringPool.DOT + SqlMethod.SELECT_BY_ID.getMethod();
-        return saveOrUpdateBatch(entityList, (sqlSession, entity) -> {
+        return insertOrUpdate(entityList, (sqlSession, entity) -> {
             Object idVal = tableInfo.getPropertyValue(entity, keyProperty);
             return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(statement, entity));
         }, batchSize);
@@ -530,8 +530,8 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param entityList 实体对象集合
      * @since 3.5.7
      */
-    default List<BatchResult> saveOrUpdateBatch(Collection<T> entityList, BiPredicate<BatchSqlSession, T> insertPredicate) {
-        return saveOrUpdateBatch(entityList, insertPredicate, Constants.DEFAULT_BATCH_SIZE);
+    default List<BatchResult> insertOrUpdate(Collection<T> entityList, BiPredicate<BatchSqlSession, T> insertPredicate) {
+        return insertOrUpdate(entityList, insertPredicate, Constants.DEFAULT_BATCH_SIZE);
     }
 
     /**
@@ -541,7 +541,7 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param batchSize  插入批次数量
      * @since 3.5.7
      */
-    default List<BatchResult> saveOrUpdateBatch(Collection<T> entityList, BiPredicate<BatchSqlSession, T> insertPredicate, int batchSize) {
+    default List<BatchResult> insertOrUpdate(Collection<T> entityList, BiPredicate<BatchSqlSession, T> insertPredicate, int batchSize) {
         MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
         MybatisBatch.Method<T> method = new MybatisBatch.Method<>(mybatisMapperProxy.getMapperInterface());
         SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory(mybatisMapperProxy);

+ 2 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/IService.java

@@ -156,7 +156,7 @@ public interface IService<T> {
         if (CollectionUtils.isEmpty(list)) {
             return false;
         }
-        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
+        return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
     }
 
     /**
@@ -171,7 +171,7 @@ public interface IService<T> {
         if (CollectionUtils.isEmpty(list)) {
             return false;
         }
-        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list, useFill));
+        return SqlHelper.retBool(getBaseMapper().deleteByIds(list, useFill));
     }
 
     /**

+ 2 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.java

@@ -299,12 +299,12 @@ public abstract class ServiceImpl<M extends BaseMapper<T>, T> implements IServic
 
     @Override
     public boolean removeBatchByIds(Collection<?> list, int batchSize) {
-        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
+        return SqlHelper.retBool(getBaseMapper().deleteByIds(list));
     }
 
     @Override
     public boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
-        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list, useFill));
+        return SqlHelper.retBool(getBaseMapper().deleteByIds(list, useFill));
     }
 
 }

+ 4 - 4
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/Db.java

@@ -87,7 +87,7 @@ public class Db {
             return false;
         }
         Class<T> entityClass = getEntityClass(entityList);
-        List<BatchResult> batchResults = SqlHelper.execute(entityClass, baseMapper -> baseMapper.saveBatch(entityList, batchSize));
+        List<BatchResult> batchResults = SqlHelper.execute(entityClass, baseMapper -> baseMapper.insert(entityList, batchSize));
         return batchResults.stream().flatMapToInt(r -> IntStream.of(r.getUpdateCounts())).allMatch(i -> i > 0);
     }
 
@@ -111,7 +111,7 @@ public class Db {
             return false;
         }
         Class<T> entityClass = getEntityClass(entityList);
-        List<BatchResult> batchResults = SqlHelper.execute(entityClass, baseMapper -> baseMapper.saveOrUpdateBatch(entityList, batchSize));
+        List<BatchResult> batchResults = SqlHelper.execute(entityClass, baseMapper -> baseMapper.insertOrUpdate(entityList, batchSize));
         return batchResults.stream().flatMapToInt(r -> IntStream.of(r.getUpdateCounts())).allMatch(i -> i > 0);
     }
 
@@ -194,7 +194,7 @@ public class Db {
      */
     public static <T> boolean updateBatchById(Collection<T> entityList, int batchSize) {
         Class<T> entityClass = getEntityClass(entityList);
-        List<BatchResult> batchResults = SqlHelper.execute(entityClass, baseMapper -> baseMapper.updateBatchById(entityList, batchSize));
+        List<BatchResult> batchResults = SqlHelper.execute(entityClass, baseMapper -> baseMapper.updateById(entityList, batchSize));
         return batchResults.stream().flatMapToInt(r -> IntStream.of(r.getUpdateCounts())).allMatch(i -> i > 0);
     }
 
@@ -205,7 +205,7 @@ public class Db {
      * @param entityClass 实体类
      */
     public static <T> boolean removeByIds(Collection<? extends Serializable> list, Class<T> entityClass) {
-        return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.retBool(baseMapper.deleteBatchIds(list)));
+        return SqlHelper.execute(entityClass, baseMapper -> SqlHelper.retBool(baseMapper.deleteByIds(list)));
     }
 
     /**

+ 11 - 11
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserMapperTest.java

@@ -57,18 +57,18 @@ class H2UserMapperTest extends BaseTest {
     @Test
     void testMapperSaveBatch() {
         var list = List.of(new H2User("秋秋1"), new H2User("秋秋2"));
-        List<BatchResult> batchResults = userMapper.saveBatch(list);
+        List<BatchResult> batchResults = userMapper.insert(list);
         Assertions.assertEquals(2, batchResults.get(0).getUpdateCounts().length);
     }
 
     @Test
     void testMapperUpdateBatch() {
         var list = List.of(new H2User("秋秋1"), new H2User("秋秋2"));
-        userMapper.saveBatch(list);
+        userMapper.insert(list);
         for (H2User h2User : list) {
             h2User.setName("test" + 1);
         }
-        List<BatchResult> batchResults = userMapper.updateBatchById(list);
+        List<BatchResult> batchResults = userMapper.updateById(list);
         Assertions.assertEquals(2, batchResults.get(0).getUpdateCounts().length);
     }
 
@@ -246,7 +246,7 @@ class H2UserMapperTest extends BaseTest {
         for (int i = 0; i < batchSize; i++) {
             h2UserList.add(new H2User(Long.valueOf(140000 + i), "test" + i));
         }
-        List<BatchResult> batchResults = userMapper.saveOrUpdateBatch(h2UserList);
+        List<BatchResult> batchResults = userMapper.insertOrUpdate(h2UserList);
         Assertions.assertEquals(batchSize, batchResults.size());
         // 使用共享的sqlSession,等于每次都是刷新了,批次总结果集就等于数据大小了
         Assertions.assertEquals(batchSize, batchResults.size());
@@ -263,7 +263,7 @@ class H2UserMapperTest extends BaseTest {
         for (int i = 0; i < batchSize; i++) {
             h2UserList.add(new H2User(Long.valueOf(40000 + i), "test" + i));
         }
-        List<BatchResult> batchResults = userMapper.saveOrUpdateBatch(h2UserList,((sqlSession, h2User) -> userMapper.selectById(h2User.getTestId()) == null));
+        List<BatchResult> batchResults = userMapper.insertOrUpdate(h2UserList,((sqlSession, h2User) -> userMapper.selectById(h2User.getTestId()) == null));
         // 没有使用共享的sqlSession,由于都是新增返回还是一个批次
         int[] updateCounts = batchResults.get(0).getUpdateCounts();
         Assertions.assertEquals(batchSize, updateCounts.length);
@@ -278,7 +278,7 @@ class H2UserMapperTest extends BaseTest {
         var h2UserList = List.of(new H2User(id, "testSaveOrUpdateBatchMapper3"), new H2User(id, "testSaveOrUpdateBatchMapper3-1"));
         // 由于没有共享一个sqlSession,第二条记录selectById的时候第一个sqlSession的数据还没提交,会执行插入导致主键冲突.
         Assertions.assertThrowsExactly(PersistenceException.class, () -> {
-            userMapper.saveOrUpdateBatch(h2UserList, ((sqlSession, h2User) -> userMapper.selectById(h2User.getTestId()) == null));
+            userMapper.insertOrUpdate(h2UserList, ((sqlSession, h2User) -> userMapper.selectById(h2User.getTestId()) == null));
         });
     }
 
@@ -288,7 +288,7 @@ class H2UserMapperTest extends BaseTest {
         var h2UserList = List.of(new H2User(id, "testSaveOrUpdateBatchMapper4"), new H2User(id, "testSaveOrUpdateBatchMapper4-1"));
         var mapperMethod = new MybatisBatch.Method<H2User>(H2UserMapper.class);
         // 共享一个sqlSession,每次selectById都会刷新一下,第二条记录为update.
-        var batchResults = userMapper.saveOrUpdateBatch(h2UserList,
+        var batchResults = userMapper.insertOrUpdate(h2UserList,
             ((sqlSession, h2User) -> sqlSession.selectList(mapperMethod.get("selectById").getStatementId(), h2User.getTestId()).isEmpty()));
         var updateCounts = batchResults.get(0).getUpdateCounts();
         for (int updateCount : updateCounts) {
@@ -299,14 +299,14 @@ class H2UserMapperTest extends BaseTest {
 
     @Test
     void testRemoveByIds() {
-        Assertions.assertEquals(userMapper.deleteBatchIds(List.of(666666661, "2")), userMapper.deleteBatchIds(List.of(666666661, "2"), false));
+        Assertions.assertEquals(userMapper.deleteByIds(List.of(666666661, "2")), userMapper.deleteByIds(List.of(666666661, "2"), false));
         H2User h2User = new H2User("testRemoveByIds");
         userMapper.insert(h2User);
-        userMapper.deleteBatchIds(List.of(h2User));
+        userMapper.deleteByIds(List.of(h2User));
         Assertions.assertNotNull(userMapper.getById(h2User.getTestId()).getLastUpdatedDt());
         h2User = new H2User("testRemoveByIds");
         userMapper.insert(h2User);
-        userMapper.deleteBatchIds(List.of(h2User), false);
+        userMapper.deleteByIds(List.of(h2User), false);
         Assertions.assertNull(userMapper.getById(h2User.getTestId()).getLastUpdatedDt());
     }
 
@@ -408,7 +408,7 @@ class H2UserMapperTest extends BaseTest {
         Assertions.assertTrue(count > 1);
 
         // 批量删除
-        Assertions.assertEquals(count, userMapper.deleteBatchIds(h2UserList.stream().map(SuperEntity::getTestId).collect(toList())));
+        Assertions.assertEquals(count, userMapper.deleteByIds(h2UserList.stream().map(SuperEntity::getTestId).collect(toList())));
 
         // 更新
         h2User = new H2User();

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/aop/MultiAopTest.java

@@ -39,7 +39,7 @@ public class MultiAopTest {
         );
         demoService.save(new Demo());
         demoService.saveBatch(List.of(new Demo()));
-        demoService.getBaseMapper().saveBatch(List.of(new Demo()));
+        demoService.getBaseMapper().insert(List.of(new Demo()));
     }
 
 }

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/aop/NoAopTest.java

@@ -39,7 +39,7 @@ public class NoAopTest {
         );
         demoService.save(new Demo());
         demoService.saveBatch(List.of(new Demo()));
-        demoService.getBaseMapper().saveBatch(List.of(new Demo()));
+        demoService.getBaseMapper().insert(List.of(new Demo()));
     }
 
 

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/aop/SingleAopTest.java

@@ -39,7 +39,7 @@ public class SingleAopTest {
         );
         demoService.save(new Demo());
         demoService.saveBatch(List.of(new Demo()));
-        demoService.getBaseMapper().saveBatch(List.of(new Demo()));
+        demoService.getBaseMapper().insert(List.of(new Demo()));
     }
 
 }

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/logicdel/LogicDelTest.java

@@ -59,7 +59,7 @@ public class LogicDelTest extends BaseDbTest<EntityMapper> {
             Entity entity2 = new Entity();
             entity2.setName("测试根据实体主键批量删除");
             mapper.insert(entity2);
-            assertThat(mapper.deleteBatchIds(Arrays.asList(entity1.getId(), entity2.getId()))).isEqualTo(2);
+            assertThat(mapper.deleteByIds(Arrays.asList(entity1.getId(), entity2.getId()))).isEqualTo(2);
         });
 
         doTest(mapper -> {
@@ -72,7 +72,7 @@ public class LogicDelTest extends BaseDbTest<EntityMapper> {
             List<Entity> entityList = new ArrayList<>();
             entityList.add(entity1);
             entityList.add(entity2);
-            assertThat(mapper.deleteBatchIds(entityList)).isEqualTo(2);
+            assertThat(mapper.deleteByIds(entityList)).isEqualTo(2);
             entityList.forEach(entity -> {
                 Assertions.assertEquals("聂秋秋", entity.getDeleteBy());
             });