Skip to content

Commit

Permalink
自动填充变更,兼容之前版本
Browse files Browse the repository at this point in the history
  • Loading branch information
Caratacus committed Nov 21, 2018
1 parent 59decae commit fd703ad
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
* <p>
* Common method to set value for java bean.
* </p>
* <p>
* 如果包含前缀 et 使用该方法,否则可以直接 metaObject.setValue(fieldName, fieldVal);
* </p>
*
* @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;
}

/**
* <p>
* 替代之前版本insert填充
* </p>
*
* @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);
}

/**
* <p>
* 替代之前版本update填充
* </p>
*
* @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);
}

/**
* <p>
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.ibatis.reflection.MetaObject;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;


Expand All @@ -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("*************************");
Expand All @@ -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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.ibatis.reflection.MetaObject;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;

/**
Expand All @@ -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("*************************");
Expand All @@ -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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.ibatis.reflection.MetaObject;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;

/**
Expand All @@ -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("*************************");
Expand All @@ -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);
}
}

0 comments on commit fd703ad

Please sign in to comment.