前言
對(duì)于實(shí)體中的created_on和updated_on來說,它沒有必要被開發(fā)人員去干預(yù),因?yàn)樗呀?jīng)足夠說明使用場(chǎng)景了,即在插入數(shù)據(jù)和更新數(shù)據(jù)時(shí),記錄當(dāng)前時(shí)間,這對(duì)于mybatis來說,通過攔截器是可以實(shí)現(xiàn)的,記得之前說過在jpa中實(shí)現(xiàn)的方法,主要通過jpa的注解實(shí)現(xiàn)的,因?yàn)榻裉斓膍ybatis需要用到j(luò)ava的攔截器。
下面話不多說了,來一起看看詳細(xì)的介紹吧
定義兩個(gè)注解
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 "" ; } |
使用這兩個(gè)注解
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; /** * 時(shí)間攔截器. */ @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(); // 獲取參數(shù) 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; } } |
添加測(cè)試用例
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()); } |
解決是我們所預(yù)想的,created_on和updated_on被自動(dòng)賦上值了。
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 ) |
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:http://www.cnblogs.com/lori/p/10281976.html