浏览代码

精简注入方法优化

hubin 7 年之前
父节点
当前提交
7c47e7476a

+ 1 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/update/UpdateWrapper.java

@@ -43,6 +43,7 @@ public class UpdateWrapper<T> extends AbstractWrapper<T, String, UpdateWrapper<T
     private List<String> sqlSet = new ArrayList<>();
 
     public UpdateWrapper() {
+        // 如果无参构造函数,请注意实体 NULL 情况 SET 必须有否则 SQL 异常
         this(null);
     }
 

+ 1 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Update.java

@@ -35,8 +35,7 @@ public class Update extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.UPDATE;
-        // TODO 这里有个  前缀判断的问题
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, true, tableInfo, "ew.entity"),
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, true, tableInfo, "et."),
             this.sqlWhereEntityWrapper(tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);

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

@@ -100,13 +100,14 @@ public interface BaseMapper<T> {
 
     /**
      * <p>
-     * 根据 updateWrapper 条件,更新记录
+     * 根据 whereEntity 条件,更新记录
      * </p>
      *
+     * @param entity        实体对象 (set 条件值)
      * @param updateWrapper 实体对象封装操作类(可以为 null)
      * @return int
      */
-    Integer update(@Param("ew") Wrapper<T> updateWrapper);
+    Integer update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
 
     /**
      * <p>

+ 0 - 26
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java

@@ -54,16 +54,6 @@ public abstract class Model<T extends Model> implements Serializable {
         return SqlHelper.retBool(sqlSession().insert(sqlStatement(SqlMethod.INSERT_ONE), this));
     }
 
-    /**
-     * <p>
-     * 插入(所有字段插入)
-     * </p>
-     */
-    @Transactional(rollbackFor = Exception.class)
-    public boolean insertAllColumn() {
-        return SqlHelper.retBool(sqlSession().insert(sqlStatement(SqlMethod.INSERT_ONE_ALL_COLUMN), this));
-    }
-
     /**
      * <p>
      * 插入 OR 更新
@@ -141,22 +131,6 @@ public abstract class Model<T extends Model> implements Serializable {
         return SqlHelper.retBool(sqlSession().update(sqlStatement(SqlMethod.UPDATE_BY_ID), map));
     }
 
-    /**
-     * <p>
-     * 更新(所有字段更新)
-     * </p>
-     */
-    @Transactional(rollbackFor = Exception.class)
-    public boolean updateAllColumnById() {
-        if (StringUtils.checkValNull(pkVal())) {
-            throw new MybatisPlusException("updateAllColumnById primaryKey is null.");
-        }
-        // updateAllColumnById
-        Map<String, Object> map = new HashMap<>();
-        map.put("et", this);
-        return SqlHelper.retBool(sqlSession().update(sqlStatement(SqlMethod.UPDATE_ALL_COLUMN_BY_ID), map));
-    }
-
     /**
      * <p>
      * 执行 SQL 更新

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

@@ -140,10 +140,11 @@ public interface IService<T> {
      * 根据 whereEntity 条件,更新记录
      * </p>
      *
+     * @param entity 实体对象
      * @param wrapper 实体包装类 {@link Wrapper}
      * @return boolean
      */
-    boolean update(Wrapper<T> wrapper);
+    boolean update(T entity, Wrapper<T> wrapper);
 
     /**
      * <p>

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

@@ -241,8 +241,8 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public boolean update(Wrapper<T> wrapper) {
-        return retBool(baseMapper.update(wrapper));
+    public boolean update(T entity, Wrapper<T> wrapper) {
+        return retBool(baseMapper.update(entity, wrapper));
     }
 
     @Transactional(rollbackFor = Exception.class)

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -64,8 +64,8 @@ public class MysqlTestDataMapperTest {
 
     @Test
     public void update() {
-        TestData data = new TestData().setId(1L).setTestStr("123123");
-        testDataMapper.update(data, new UpdateWrapper<TestData>().eq("id", 1L));
+        testDataMapper.update(new TestData().setId(1L).setTestStr("123123"),
+            new UpdateWrapper<TestData>().eq("id", 1L));
     }
 
     @Test