一.應(yīng)用場(chǎng)景
平時(shí)在建對(duì)象表的時(shí)候都會(huì)有最后修改時(shí)間脑融,最后修改人這兩個(gè)字段均驶,對(duì)于這些大部分表都有的字段曹质,每次在新增和修改的時(shí)候都要考慮到這幾個(gè)字段有沒有傳進(jìn)去有梆,很麻煩赁炎。mybatisPlus有一個(gè)很好的解決方案。也就是公共字段自動(dòng)填充的功能钾腺。一般滿足下面條件的字段就可以使用此功能:
這個(gè)字段是大部分表都會(huì)有的徙垫。
這個(gè)字段的值是固定的,或則字段值是可以在后臺(tái)動(dòng)態(tài)獲取的放棒。
常用的就是last_update_time姻报,last_update_name這兩個(gè)字段。
二.配置MybatisPlus
導(dǎo)包:只需要注意的一點(diǎn)就是间螟,mybatisPlus是在2.0.6版本才支持的更新數(shù)據(jù)公共字段自動(dòng)填充吴旋,之前都是只支持新增數(shù)據(jù)的時(shí)候可以使用。
如果是之前配置過MybatisPlus的同學(xué)只需要添加以下幾個(gè)步驟:
- 繼承IMetaObjectHandler抽象類厢破,實(shí)現(xiàn)
insertFill()
新增數(shù)據(jù)時(shí)需要填充的字段設(shè)置和updateFill()
更新數(shù)據(jù)的時(shí)候需要填充的字段設(shè)置這兩個(gè)方法:
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自定義填充公共字段 ,即沒有傳的字段自動(dòng)填充*/
@Component
public class MyMetaObjectHandler extends MetaObjectHandler {
//新增填充
@Override
public void insertFill(MetaObject metaObject) {
Object lastUpdateNameId = metaObject.getValue("lastUpdateNameId");
Object lastUpdateTime = metaObject.getValue("lastUpdateTime");
//獲取當(dāng)前登錄用戶
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()方法的參數(shù)是pojo類里面的變量(駝峰的命名方式)荣瑟。
- 在mybatisplus的配置文件中公共字段生成類的bean:
// MP 全局配置,更多內(nèi)容進(jìn)入類看注釋
GlobalConfiguration globalConfig = new GlobalConfiguration();
//配置公共字段自動(dòng)填寫
globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
也就是將剛剛寫的公共字段填充的設(shè)置設(shè)置到MP全局配置的對(duì)象中摩泪。
- 填充的字段需要忽略驗(yàn)證笆焰,在表對(duì)象pojo類的對(duì)應(yīng)屬性上添加下面注解:
/**
* 最后修改人Id
*/
@TableField(value="last_update_id",validate=FieldStrategy.NOT_EMPTY)
private String lastUpdateNameId;
/**
* 最后修改時(shí)間
*/
@TableField(value="last_update_time",validate=FieldStrategy.NOT_EMPTY)
private Date lastUpdateTime;
原因:因?yàn)檎{(diào)用更新和插入的方法時(shí),會(huì)驗(yàn)證你所傳的屬性是否為空见坑,來判斷是否該更新和插入這個(gè)屬性嚷掠,這個(gè)就和公共字段自動(dòng)填充相沖突了,所以需要這個(gè)注解來標(biāo)識(shí)此屬性不需要驗(yàn)證荞驴。不然在插入的時(shí)候就會(huì)填充失敗不皆。
如果之前沒有使用過MbatisPlus,可以出門左轉(zhuǎn)熊楼,鏈接:之前寫過的一個(gè)博文霹娄,里面涉及到MybatisPlus的基礎(chǔ)使用配置
三.編寫測(cè)試類
//公共字段自動(dòng)填充
//1.在mybatisplus的配置文件中公共字段生成類的bean
//2.實(shí)現(xiàn)IMetaObjectHandler類
//3.忽略對(duì)應(yīng)字段的為空檢測(cè),在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());
}
四.測(cè)試日志
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
五.總結(jié)
本來是打算創(chuàng)建人和創(chuàng)建時(shí)間,也使用這種方法處理的项棠,最后發(fā)現(xiàn)悲雳,如果將這兩個(gè)字段也忽略為空的判斷,也就是加上validate=FieldStrategy.NOT_EMPTY
香追,在更新數(shù)據(jù)的時(shí)候會(huì)將創(chuàng)建人和創(chuàng)建時(shí)間一起更新了合瓢,不傳的話就會(huì)更新為空。所以本人覺得這個(gè)mybatisPlus這個(gè)公共字段自動(dòng)填充功能是不錯(cuò)透典,但是在正真用的上的需求上面使用的時(shí)候還不夠完善晴楔。
·······················································································································································
個(gè)人博客:http://z77z.oschina.io/
此項(xiàng)目下載地址:https://git.oschina.net/z77z/springboot_mybatisplus
·······················································································································································