前言
對于實體中的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" ) .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