平时在建对象表的时候都会有最后修改时间,最后修改人这两个字段,对于这些大部分表都有的字段,每次在新增和修改的时候都要考虑到这几个字段有没有传进去,很麻烦。mybatisPlus有一个很好的解决方案。也就是公共字段自动填充的功能。一般满足下面条件的字段就可以使用此功能:
导包:只需要注意的一点就是,mybatisPlus是在2.0.6版本才支持的更新数据公共字段自动填充,之前都是只支持新增数据的时候可以使用。
如果是之前配置过MybatisPlus的同学只需要添加以下几个步骤:
insertFill()
新增数据时需要填充的字段设置和updateFill()
更新数据的时候需要填充的字段设置这两个方法:package io.z77z.util;
import java.util.Date;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import io.z77z.entity.SysUser;
/** mybatisplus自定义填充公共字段 ,即没有传的字段自动填充*/
@Component
public class MyMetaObjectHandler extends MetaObjectHandler {
//新增填充
@Override
public void insertFill(MetaObject metaObject) {
Object lastUpdateNameId = metaObject.getValue("lastUpdateNameId");
Object lastUpdateTime = metaObject.getValue("lastUpdateTime");
//获取当前登录用户
SysUser user = (SysUser)SecurityUtils.getSubject().getPrincipal();
if (null == lastUpdateNameId) {
metaObject.setValue("lastUpdateNameId", user.getId());
}
if (null == lastUpdateTime) {
metaObject.setValue("lastUpdateTime", new Date());
}
}
//更新填充
@Override
public void updateFill(MetaObject metaObject) {
insertFill(metaObject);
}
}
注意:getValue()方法的参数是pojo类里面的变量(驼峰的命名方式)。
// MP 全局配置,更多内容进入类看注释
GlobalConfiguration globalConfig = new GlobalConfiguration();
//配置公共字段自动填写
globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
也就是将刚刚写的公共字段填充的设置设置到MP全局配置的对象中。
/**
* 最后修改人Id
*/
@TableField(value="last_update_id",validate=FieldStrategy.NOT_EMPTY)
private String lastUpdateNameId;
/**
* 最后修改时间
*/
@TableField(value="last_update_time",validate=FieldStrategy.NOT_EMPTY)
private Date lastUpdateTime;
原因:因为调用更新和插入的方法时,会验证你所传的属性是否为空,来判断是否该更新和插入这个属性,这个就和公共字段自动填充相冲突了,所以需要这个注解来标识此属性不需要验证。不然在插入的时候就会填充失败。
如果之前没有使用过MbatisPlus,可以出门左转,链接:之前写过的一个博文,里面涉及到MybatisPlus的基础使用配置
//公共字段自动填充
//1.在mybatisplus的配置文件中公共字段生成类的bean
//2.实现IMetaObjectHandler类
//3.忽略对应字段的为空检测,在pojo类的属性上添加@TableField(value="last_update_name_id",validate=FieldStrategy.IGNORED)
@Test
public void publicTest(){
SysUser user = new SysUser();
user.setEmail("1093615728@qq.com");
user.setNickname("z77z");
user.setPswd("123123");
user.setStatus("1");
sysUserService.insert(user);
sysUserService.selectById(user.getId());
SysUser user1 = new SysUser();
user1.setPswd("123");
user1.setId(user.getId());
sysUserService.updateById(user1);
sysUserService.selectById(user.getId());
}
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.insert- ==> Preparing: INSERT INTO sys_user ( id, nickname, email, pswd, `status`,last_update_name_id, last_update_time ) VALUES ( ?, ?, ?, ?, ?,?, ? )
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.insert- ==> Parameters: 6634923de4a14b6ca3bac5fdf31563a8(String), z77z(String), 1093615728@qq.com(String), 123123(String), 1(String), 123(String), 2017-04-23 19:35:26.58(Timestamp)
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.insert- <== Updates: 1
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.selectById- ==> Preparing: SELECT id,nickname,email,pswd,last_login_time AS lastLoginTime,`status`,last_update_name_id AS lastUpdateNameId,create_name_id AS createNameId,last_update_time AS lastUpdateTime,create_time AS createTime FROM sys_user WHERE id=?
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.selectById- ==> Parameters: 6634923de4a14b6ca3bac5fdf31563a8(String)
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.selectById- <== Total: 1
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.updateById- ==> Preparing: UPDATE sys_user SET pswd=?, last_update_name_id=?, last_update_time=? WHERE id=?
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.updateById- ==> Parameters: 123(String), 123(String), 2017-04-23 19:35:26.637(Timestamp), 6634923de4a14b6ca3bac5fdf31563a8(String)
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.updateById- <== Updates: 1
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.selectById- ==> Preparing: SELECT id,nickname,email,pswd,last_login_time AS lastLoginTime,`status`,last_update_name_id AS lastUpdateNameId,create_name_id AS createNameId,last_update_time AS lastUpdateTime,create_time AS createTime FROM sys_user WHERE id=?
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.selectById- ==> Parameters: 6634923de4a14b6ca3bac5fdf31563a8(String)
2017/04/23-19:35:26 [main] DEBUG io.z77z.dao.SysUserMapper.selectById- <== Total: 1
本来是打算创建人和创建时间,也使用这种方法处理的,最后发现,如果将这两个字段也忽略为空的判断,也就是加上validate=FieldStrategy.NOT_EMPTY
,在更新数据的时候会将创建人和创建时间一起更新了,不传的话就会更新为空。所以本人觉得这个mybatisPlus这个公共字段自动填充功能是不错,但是在正真用的上的需求上面使用的时候还不够完善。
·······················································································································································
此项目下载地址:https://git.oschina.net/z77z/springboot_mybatisplus
·······················································································································································