操作步骤
- 自建
com.baomidou.mybatisplus.core.metadata.TableFieldInfo
类,注意包名不可变
- 修改
getInsertSqlPropertyMaybeIf
方法,复用com.baomidou.mybatisplus.annotation.TableField
注解的update
参数
修改方法
public String getInsertSqlPropertyMaybeIf(final String prefix) {
final String newPrefix = prefix == null ? EMPTY : prefix;
String sqlScript = getInsertSqlProperty(newPrefix);
if (withInsertFill) {
//modify begin
if (StringUtils.isNotBlank(update)) {
return update;
} else {
return sqlScript;
}
//modify end
}
return convertIf(sqlScript, newPrefix + property, insertStrategy);
}
实体类
public class BasePo {
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT, update = "now(3)")
private Date createTime;
/**
* 最后更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE, update = "now(3)")
private Date updateTime;
}
- 如果没有生效,可能需要注意类的加载顺序
update
中可以是任何数据库函数或者语句