From fd703ad12a11e5bbb1714dde30d93928ecaa6291 Mon Sep 17 00:00:00 2001 From: Caratacus <330952486@qq.com> Date: Wed, 21 Nov 2018 17:50:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=A1=AB=E5=85=85=E5=8F=98?= =?UTF-8?q?=E6=9B=B4,=E5=85=BC=E5=AE=B9=E4=B9=8B=E5=89=8D=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/MybatisDefaultParameterHandler.java | 5 +- .../core/handlers/MetaObjectHandler.java | 72 +++++++++++++++++-- .../test/h2/H2MetaObjectHandler.java | 13 ++-- .../test/mysql/MysqlMetaObjectHandler.java | 13 ++-- .../postgres/PostgresMetaObjectHandler.java | 13 ++-- 5 files changed, 85 insertions(+), 31 deletions(-) diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisDefaultParameterHandler.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisDefaultParameterHandler.java index 9e8b903f13..8454dfbb44 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisDefaultParameterHandler.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisDefaultParameterHandler.java @@ -37,7 +37,6 @@ import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandlerRegistry; -import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.baomidou.mybatisplus.core.metadata.TableInfo; @@ -209,10 +208,10 @@ protected static Object populateKeys(MetaObjectHandler metaObjectHandler, TableI if (metaObjectHandler != null) { if (isInsert && metaObjectHandler.openInsertFill()) { // 插入填充 - metaObjectHandler.insertFill(metaObject, FieldFill.INSERT); + metaObjectHandler.insertFill(metaObject); } else if (!isInsert) { // 更新填充 - metaObjectHandler.updateFill(metaObject, FieldFill.UPDATE); + metaObjectHandler.updateFill(metaObject); } } return metaObject.getOriginalObject(); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MetaObjectHandler.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MetaObjectHandler.java index d6c73c5bc6..ceeb4a9d26 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MetaObjectHandler.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MetaObjectHandler.java @@ -46,21 +46,78 @@ public interface MetaObjectHandler { * 插入元对象字段填充(用于插入时对公共字段的填充) * * @param metaObject 元对象 - * @param fieldFill 填充策略枚举 3.0.7添加,之前版本升级传递该参数至MetaObjectHandler#setFieldValByName - * @see com.baomidou.mybatisplus.core.handlers.MetaObjectHandler#setFieldValByName - * @since 3.0.7 */ - void insertFill(MetaObject metaObject, FieldFill fieldFill); + void insertFill(MetaObject metaObject); /** * 更新元对象字段填充(用于更新时对公共字段的填充) * * @param metaObject 元对象 - * @param fieldFill 填充策略枚举 3.0.7添加,之前版本升级传递该参数至MetaObjectHandler#setFieldValByName - * @see com.baomidou.mybatisplus.core.handlers.MetaObjectHandler#setFieldValByName + */ + void updateFill(MetaObject metaObject); + + /** + *

+ * Common method to set value for java bean. + *

+ *

+ * 如果包含前缀 et 使用该方法,否则可以直接 metaObject.setValue(fieldName, fieldVal); + *

+ * + * @param fieldName java bean property name + * @param fieldVal java bean property value + * @param metaObject meta object parameter + * @see MetaObjectHandler#setInsertFieldValByName(String, Object, MetaObject) 替代之前版本insert填充 + * @see MetaObjectHandler#setUpdateFieldValByName(String, Object, MetaObject) 替代之前版本update填充 + * @since 3.0.7 + */ + @Deprecated + default MetaObjectHandler setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) { + if (Objects.nonNull(fieldVal)) { + if (metaObject.hasSetter(fieldName) && metaObject.hasGetter(fieldName)) { + metaObject.setValue(fieldName, fieldVal); + } else if (metaObject.hasGetter(Constants.ENTITY)) { + Object et = metaObject.getValue(Constants.ENTITY); + if (et != null) { + MetaObject etMeta = SystemMetaObject.forObject(et); + if (etMeta.hasSetter(fieldName)) { + etMeta.setValue(fieldName, fieldVal); + } + } + } + } + return this; + } + + /** + *

+ * 替代之前版本insert填充 + *

+ * + * @param fieldName + * @param fieldVal + * @param metaObject + * @return * @since 3.0.7 */ - void updateFill(MetaObject metaObject, FieldFill fieldFill); + default MetaObjectHandler setInsertFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) { + return setFieldValByName(fieldName, fieldVal, metaObject, FieldFill.INSERT); + } + + /** + *

+ * 替代之前版本update填充 + *

+ * + * @param fieldName + * @param fieldVal + * @param metaObject + * @return + * @since 3.0.7 + */ + default MetaObjectHandler setUpdateFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) { + return setFieldValByName(fieldName, fieldVal, metaObject, FieldFill.UPDATE); + } /** *

@@ -121,6 +178,7 @@ default Object getFieldValByName(String fieldName, MetaObject metaObject) { * @param metaObject * @param fieldFill 填充策略 * @return + * @since 3.0.7 */ default boolean isFill(String fieldName, MetaObject metaObject, FieldFill fieldFill) { TableInfo tableInfo = metaObject.hasGetter(MP_OPTLOCK_ET_ORIGINAL) ? TableInfoHelper.getTableInfo(metaObject.getValue(MP_OPTLOCK_ET_ORIGINAL).getClass()) : TableInfoHelper.getTableInfo(metaObject.getOriginalObject().getClass()); diff --git a/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2MetaObjectHandler.java b/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2MetaObjectHandler.java index 0e1e063e4c..80f2ad68b4 100644 --- a/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2MetaObjectHandler.java +++ b/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2MetaObjectHandler.java @@ -19,7 +19,6 @@ import org.apache.ibatis.reflection.MetaObject; -import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; @@ -37,7 +36,7 @@ public class H2MetaObjectHandler implements MetaObjectHandler { * 测试 user 表 name 字段为空自动填充 */ @Override - public void insertFill(MetaObject metaObject, FieldFill fieldFill) { + public void insertFill(MetaObject metaObject) { System.out.println("*************************"); System.out.println("insert fill"); System.out.println("*************************"); @@ -47,19 +46,19 @@ public void insertFill(MetaObject metaObject, FieldFill fieldFill) { System.out.println("testType=" + testType); if (testType == null) { //测试实体没有的字段,配置在公共填充,不应该set到实体里面 - this.setFieldValByName("testType1", 3, metaObject, fieldFill); - this.setFieldValByName("testType", 3, metaObject, fieldFill); + this.setInsertFieldValByName("testType1", 3, metaObject); + this.setInsertFieldValByName("testType", 3, metaObject); } } @Override - public void updateFill(MetaObject metaObject, FieldFill fieldFill) { + public void updateFill(MetaObject metaObject) { System.out.println("*************************"); System.out.println("update fill"); System.out.println("*************************"); //测试实体没有的字段,配置在公共填充,不应该set到实体里面 - this.setFieldValByName("lastUpdatedDt1", new Timestamp(System.currentTimeMillis()), metaObject, fieldFill); - this.setFieldValByName("lastUpdatedDt", new Timestamp(System.currentTimeMillis()), metaObject, fieldFill); + this.setUpdateFieldValByName("lastUpdatedDt1", new Timestamp(System.currentTimeMillis()), metaObject); + this.setUpdateFieldValByName("lastUpdatedDt", new Timestamp(System.currentTimeMillis()), metaObject); } } diff --git a/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlMetaObjectHandler.java b/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlMetaObjectHandler.java index 3d9195a2ea..3f0c96f17b 100644 --- a/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlMetaObjectHandler.java +++ b/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlMetaObjectHandler.java @@ -19,7 +19,6 @@ import org.apache.ibatis.reflection.MetaObject; -import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; /** @@ -36,7 +35,7 @@ public class MysqlMetaObjectHandler implements MetaObjectHandler { * 测试 user 表 name 字段为空自动填充 */ @Override - public void insertFill(MetaObject metaObject, FieldFill fieldFill) { + public void insertFill(MetaObject metaObject) { System.out.println("*************************"); System.out.println("insert of mysql fill"); System.out.println("*************************"); @@ -45,19 +44,19 @@ public void insertFill(MetaObject metaObject, FieldFill fieldFill) { System.out.println("createDatetime=" + createDatetime); if (createDatetime == null) { //测试实体没有的字段,配置在公共填充,不应该set到实体里面 - this.setFieldValByName("createDatetime1", LocalDateTime.now(), metaObject, fieldFill) - .setFieldValByName("createDatetime", LocalDateTime.now(), metaObject, fieldFill); + this.setInsertFieldValByName("createDatetime1", LocalDateTime.now(), metaObject) + .setInsertFieldValByName("createDatetime", LocalDateTime.now(), metaObject); } } @Override - public void updateFill(MetaObject metaObject, FieldFill fieldFill) { + public void updateFill(MetaObject metaObject) { System.out.println("*************************"); System.out.println("update of mysql fill"); System.out.println("*************************"); //测试实体没有的字段,配置在公共填充,不应该set到实体里面 - this.setFieldValByName("updateDatetime1", LocalDateTime.now(), metaObject, fieldFill) - .setFieldValByName("updateDatetime", LocalDateTime.now(), metaObject, fieldFill); + this.setUpdateFieldValByName("updateDatetime1", LocalDateTime.now(), metaObject) + .setUpdateFieldValByName("updateDatetime", LocalDateTime.now(), metaObject); } } diff --git a/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/PostgresMetaObjectHandler.java b/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/PostgresMetaObjectHandler.java index d13f9000d4..12f2f21758 100644 --- a/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/PostgresMetaObjectHandler.java +++ b/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgres/PostgresMetaObjectHandler.java @@ -19,7 +19,6 @@ import org.apache.ibatis.reflection.MetaObject; -import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; /** @@ -36,7 +35,7 @@ public class PostgresMetaObjectHandler implements MetaObjectHandler { * 测试 user 表 name 字段为空自动填充 */ @Override - public void insertFill(MetaObject metaObject, FieldFill fieldFill) { + public void insertFill(MetaObject metaObject) { System.out.println("*************************"); System.out.println("insert of postgres fill"); System.out.println("*************************"); @@ -45,19 +44,19 @@ public void insertFill(MetaObject metaObject, FieldFill fieldFill) { System.out.println("createDatetime=" + createDatetime); if (createDatetime == null) { //测试实体没有的字段,配置在公共填充,不应该set到实体里面 - this.setFieldValByName("createDatetime1", LocalDateTime.now(), metaObject, fieldFill) - .setFieldValByName("createDatetime", LocalDateTime.now(), metaObject, fieldFill); + this.setInsertFieldValByName("createDatetime1", LocalDateTime.now(), metaObject) + .setInsertFieldValByName("createDatetime", LocalDateTime.now(), metaObject); } } @Override - public void updateFill(MetaObject metaObject, FieldFill fieldFill) { + public void updateFill(MetaObject metaObject) { System.out.println("*************************"); System.out.println("update of postgres fill"); System.out.println("*************************"); //测试实体没有的字段,配置在公共填充,不应该set到实体里面 - this.setFieldValByName("updateDatetime1", LocalDateTime.now(), metaObject, fieldFill) - .setFieldValByName("updateDatetime", LocalDateTime.now(), metaObject, fieldFill); + this.setUpdateFieldValByName("updateDatetime1", LocalDateTime.now(), metaObject) + .setUpdateFieldValByName("updateDatetime", LocalDateTime.now(), metaObject); } }