Bläddra i källkod

fix: #IQ65E gitee: 当表没有主键,自动填充失效bug

yuxiaobin 6 år sedan
förälder
incheckning
23a5d108ba

+ 14 - 7
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/MybatisDefaultParameterHandler.java

@@ -128,7 +128,7 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
                     } else {
                     } else {
                         /*
                         /*
                          * 非表映射类不处理
                          * 非表映射类不处理
-						 */
+                         */
                         objList.add(parameter);
                         objList.add(parameter);
                     }
                     }
                 }
                 }
@@ -202,12 +202,13 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
      */
      */
     protected static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo,
     protected static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo,
                                          MappedStatement ms, Object parameterObject) {
                                          MappedStatement ms, Object parameterObject) {
+        MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
         if (null == tableInfo || StringUtils.isEmpty(tableInfo.getKeyProperty()) || null == tableInfo.getIdType()) {
         if (null == tableInfo || StringUtils.isEmpty(tableInfo.getKeyProperty()) || null == tableInfo.getIdType()) {
             /* 不处理 */
             /* 不处理 */
+            autoFill(ms.getSqlCommandType(), metaObjectHandler, metaObject);
             return parameterObject;
             return parameterObject;
         }
         }
         /* 自定义元对象填充控制器 */
         /* 自定义元对象填充控制器 */
-        MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
         if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
         if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
             if (tableInfo.getIdType().getKey() >= 2) {
             if (tableInfo.getIdType().getKey() >= 2) {
                 Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
                 Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
@@ -222,15 +223,21 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
                     }
                     }
                 }
                 }
             }
             }
-            // 插入填充
+        }
+        autoFill(ms.getSqlCommandType(), metaObjectHandler, metaObject);
+        return metaObject.getOriginalObject();
+    }
+
+    private static void autoFill(SqlCommandType commandType, MetaObjectHandler metaObjectHandler, MetaObject metaObject) {
+        if (commandType == SqlCommandType.INSERT) {
             if (metaObjectHandler.openInsertFill()) {
             if (metaObjectHandler.openInsertFill()) {
                 metaObjectHandler.insertFill(metaObject);
                 metaObjectHandler.insertFill(metaObject);
             }
             }
-        } else if (ms.getSqlCommandType() == SqlCommandType.UPDATE && metaObjectHandler.openUpdateFill()) {
-            // 更新填充
-            metaObjectHandler.updateFill(metaObject);
+        } else if (commandType == SqlCommandType.UPDATE) {
+            if (metaObjectHandler.openUpdateFill()) {
+                metaObjectHandler.updateFill(metaObject);
+            }
         }
         }
-        return metaObject.getOriginalObject();
     }
     }
 
 
     @SuppressWarnings({"rawtypes", "unchecked"})
     @SuppressWarnings({"rawtypes", "unchecked"})

+ 28 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/h2/H2MetaObjectHandlerTest.java

@@ -18,9 +18,12 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.test.h2.base.H2Test;
 import com.baomidou.mybatisplus.test.h2.base.H2Test;
 import com.baomidou.mybatisplus.test.h2.entity.mapper.H2UserMetaobjMapper;
 import com.baomidou.mybatisplus.test.h2.entity.mapper.H2UserMetaobjMapper;
+import com.baomidou.mybatisplus.test.h2.entity.mapper.H2UserMetaobjNoPkMapper;
 import com.baomidou.mybatisplus.test.h2.entity.persistent.H2UserMetaObj;
 import com.baomidou.mybatisplus.test.h2.entity.persistent.H2UserMetaObj;
+import com.baomidou.mybatisplus.test.h2.entity.persistent.H2UserMetaObjNoPk;
 
 
 /**
 /**
  * <p>
  * <p>
@@ -36,6 +39,8 @@ public class H2MetaObjectHandlerTest extends H2Test {
 
 
     @Autowired
     @Autowired
     private H2UserMetaobjMapper userMapper;
     private H2UserMetaobjMapper userMapper;
+    @Autowired
+    H2UserMetaobjNoPkMapper userMetaobjNoPkMapper;
 
 
     @BeforeClass
     @BeforeClass
     public static void initDB() throws SQLException, IOException {
     public static void initDB() throws SQLException, IOException {
@@ -70,6 +75,29 @@ public class H2MetaObjectHandlerTest extends H2Test {
         assertUpdateFill(userDB.getLastUpdatedDt());
         assertUpdateFill(userDB.getLastUpdatedDt());
     }
     }
 
 
+    @Test
+    public void testAutoFill4NoPk() {
+        Long id = 10098L;
+        H2UserMetaObjNoPk user = new H2UserMetaObjNoPk();
+        user.setTestId(id);
+        user.setName("NO_PK").setAge(18);
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.DAY_OF_MONTH, -1);
+        user.setLastUpdatedDt(cal.getTime());
+        userMetaobjNoPkMapper.insert(user);
+
+        H2UserMetaObjNoPk dbUser = userMetaobjNoPkMapper.selectOne(new H2UserMetaObjNoPk().setTestId(id));
+        Assert.assertNotNull("should auto fill INSERT column", dbUser.getTestType());
+        EntityWrapper<H2UserMetaObjNoPk> ew = new EntityWrapper<>();
+        ew.eq("test_id", id);
+        H2UserMetaObjNoPk updateUser = new H2UserMetaObjNoPk().setName("TOMCAT");
+        userMetaobjNoPkMapper.update(updateUser, ew);
+        dbUser = userMetaobjNoPkMapper.selectOne(new H2UserMetaObjNoPk().setTestId(id));
+        Assert.assertEquals("name should be updated", "TOMCAT", dbUser.getName());
+        Assert.assertTrue("last update time should be current sysdate time", Math.abs(System.currentTimeMillis() - dbUser.getLastUpdatedDt().getTime()) < 1000);
+
+    }
+
     @Test
     @Test
     public void testMetaObjectHandlerNullInsert4Update() {
     public void testMetaObjectHandlerNullInsert4Update() {
         H2UserMetaObj user = new H2UserMetaObj();
         H2UserMetaObj user = new H2UserMetaObj();

+ 28 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/h2/entity/mapper/H2UserMetaobjNoPkMapper.java

@@ -0,0 +1,28 @@
+package com.baomidou.mybatisplus.test.h2.entity.mapper;
+
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Update;
+
+import com.baomidou.mybatisplus.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.h2.entity.persistent.H2UserMetaObjNoPk;
+
+/**
+ * <p>
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+public interface H2UserMetaobjNoPkMapper extends BaseMapper<H2UserMetaObjNoPk> {
+
+    @Insert(
+        "insert into h2user(name,version) values(#{name},#{version})"
+    )
+    int myInsertWithNameVersion(@Param("name") String name, @Param("version") int version);
+
+    @Update(
+        "update h2user set name=#{name} where test_id=#{id}"
+    )
+    int myUpdateWithNameId(@Param("id") Long id, @Param("name") String name);
+}

+ 111 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2UserMetaObjNoPk.java

@@ -0,0 +1,111 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.test.h2.entity.persistent;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+import com.baomidou.mybatisplus.annotations.TableField;
+import com.baomidou.mybatisplus.annotations.TableName;
+import com.baomidou.mybatisplus.annotations.Version;
+import com.baomidou.mybatisplus.enums.FieldFill;
+import com.baomidou.mybatisplus.test.h2.entity.SuSuperEntity;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * <p>
+ * 测试用户类
+ * </p>
+ *
+ * @author hubin sjy
+ */
+/* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@Data
+@Accessors(chain = true)
+@TableName("h2user")
+public class H2UserMetaObjNoPk extends SuSuperEntity implements Serializable {
+
+    /* 表字段注解,false 表中不存在的字段,可无该注解 默认 true */
+    @TableField(exist = false)
+    private static final long serialVersionUID = 1L;
+
+    /* 主键ID 注解,value 字段名,type 用户输入ID */
+    @TableField(value = "test_id")
+    private Long testId;
+
+    /* 测试忽略验证 */
+    private String name;
+
+    private Integer age;
+
+    /*BigDecimal 测试*/
+    private BigDecimal price;
+
+    /* 测试下划线字段命名类型, 字段填充 */
+    @TableField(value = "test_type", fill = FieldFill.INSERT)
+    private Integer testType;
+
+    private String desc;
+
+    @Version
+    private Integer version;
+//    @TableField(value = "last_updated_dt",fill = FieldFill.UPDATE)
+//    private Timestamp lastUpdatedDt;
+
+
+    public H2UserMetaObjNoPk() {
+
+    }
+
+    public H2UserMetaObjNoPk(String name) {
+        this.name = name;
+    }
+
+    public H2UserMetaObjNoPk(Integer testType) {
+        this.testType = testType;
+    }
+
+    public H2UserMetaObjNoPk(String name, Integer age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    public H2UserMetaObjNoPk(Long id, String name) {
+//        this.setId(id);
+        this.name = name;
+    }
+
+    public H2UserMetaObjNoPk(Long id, Integer age) {
+//        this.setId(id);
+        this.age = age;
+    }
+
+    public H2UserMetaObjNoPk(Long id, String name, Integer age, Integer testType) {
+//        this.setId(id);
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+    public H2UserMetaObjNoPk(String name, Integer age, Integer testType) {
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+}