前言
对于实体中的created_on和updated_on来说,它没有必要被开发人员去干预,因为它已经足够说明使用场景了,即在插入数据和更新数据时,记录当前时间,这对于mybatis来说,通过拦截器是可以实现的,记得之前说过在jpa中实现的方法,主要通过jpa的注解实现的,因为今天的mybatis需要用到java的拦截器。
下面话不多说了,来一起看看详细的介绍吧
定义两个注解
1
2
3
4
5
6
7
8
9
10
11
12
|
@retention (retentionpolicy.runtime)
@target ( {elementtype.field})
public @interface createdonfuncation {
string value() default "" ;
}
@retention (retentionpolicy.runtime)
@target ( {elementtype.field})
public @interface updatedonfuncation {
string value() default "" ;
}
|
使用这两个注解
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@getter
@builder (tobuilder = true )
@tostring
public class userinfo {
private long id;
private string name;
private string email;
@createdonfuncation
private localdatetime createdon;
@updatedonfuncation
private localdatetime updatedon;
}
|
定义拦截器,重写赋值的语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package com.lind.basic.mybatis;
import com.baomidou.mybatisplus.extension.handlers.abstractsqlparserhandler;
import java.lang.reflect.field;
import java.time.localdatetime;
import java.util.properties;
import lombok.data;
import lombok.equalsandhashcode;
import lombok.experimental.accessors;
import org.apache.ibatis.logging.log;
import org.apache.ibatis.logging.logfactory;
import org.apache.ibatis.mapping.mappedstatement;
import org.apache.ibatis.mapping.sqlcommandtype;
import org.apache.ibatis.plugin.interceptor;
import org.apache.ibatis.plugin.intercepts;
import org.apache.ibatis.plugin.invocation;
import org.apache.ibatis.plugin.plugin;
import org.apache.ibatis.plugin.signature;
/**
* 时间拦截器.
*/
@equalsandhashcode (callsuper = true )
@data
@accessors (chain = true )
@intercepts ( {
@signature (
type = org.apache.ibatis.executor.executor. class ,
method = "update" ,
args = {mappedstatement. class , object. class })})
public class createupdatetimeinterceptor extends abstractsqlparserhandler implements interceptor {
private static final log logger = logfactory.getlog(com.baomidou.mybatisplus.extension.plugins.sqlexplaininterceptor. class );
private properties properties;
@override
public object intercept(invocation invocation) throws throwable {
mappedstatement mappedstatement = (mappedstatement) invocation.getargs()[ 0 ];
// 获取 sql 命令
sqlcommandtype sqlcommandtype = mappedstatement.getsqlcommandtype();
// 获取参数
object parameter = invocation.getargs()[ 1 ];
// 获取私有成员变量
field[] declaredfields = parameter.getclass().getdeclaredfields();
for (field field : declaredfields) {
if (field.getannotation(createdonfuncation. class ) != null ) {
if (sqlcommandtype.insert.equals(sqlcommandtype)) { // insert 语句插入 createtime
field.setaccessible( true );
field.set(parameter, localdatetime.now());
}
}
if (field.getannotation(updatedonfuncation. class ) != null ) { // insert 或 update 语句插入 updatetime
if (sqlcommandtype.insert.equals(sqlcommandtype) || sqlcommandtype.update.equals(sqlcommandtype)) {
field.setaccessible( true );
field.set(parameter, localdatetime.now());
}
}
}
return invocation.proceed();
}
@override
public object plugin(object target) {
if (target instanceof org.apache.ibatis.executor.executor) {
return plugin.wrap(target, this );
}
return target;
}
@override
public void setproperties(properties prop) {
this .properties = prop;
}
}
|
添加测试用例
1
2
3
4
5
6
7
8
9
|
@test
public void insert() {
userinfo userinfo = userinfo.builder()
.name( "lind" )
.email( "test@sina.com" )
.build();
userinfomapper.insert(userinfo);
system.out.println( "userinfo:" + userinfo.tostring());
}
|
解决是我们所预想的,created_on和updated_on被自动赋上值了。
1
2
3
4
5
6
7
8
|
userinfo:userinfo
(
id= 1085780948955959297 ,
name=lind,
email=test @sina .com,
createdon= 2019 - 01 -17t14: 08 : 45.665 ,
updatedon= 2019 - 01 -17t14: 08 : 45.665
)
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:http://www.cnblogs.com/lori/p/10281976.html