激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

香港云服务器
服務(wù)器之家 - 編程語言 - Java教程 - mybatis自動(dòng)填充時(shí)間字段示例代碼

mybatis自動(dòng)填充時(shí)間字段示例代碼

2021-07-10 10:36張占嶺 Java教程

這篇文章主要給大家介紹了關(guān)于mybatis自動(dòng)填充時(shí)間字段的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

對(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

延伸 · 閱讀

精彩推薦
1061
主站蜘蛛池模板: 日本看片一区二区三区高清 | 黄色av网站免费 | 国产精品久久亚洲 | 中文有码一区二区 | 黄色特级片黄色特级片 | 国产精品18久久久久久久 | 极品大长腿啪啪高潮露脸 | 国产v综合v亚洲欧美久久 | 国产伊人色 | 久久久午夜电影 | 成人av一二三区 | 国产精品免费大片 | 精品国产一区二区三区久久久蜜月 | 成人做爰高潮片免费视频美国 | freexxxx性女hd性吃奶 | 国产精品无码久久久久 | 国产成人午夜精品 | 91九色精品国产 | 欧美一级毛片欧美一级成人毛片 | 宅男噜噜噜66国产在线观看 | 欧美日韩亚洲国产精品 | 中国字幕av| 在线成人一区 | 久久久无码精品亚洲日韩按摩 | 亚洲影视综合网 | 欧美福利视频一区二区三区 | 久久久久久片 | 美女视频免费一区二区 | 91久久久久久久久久 | 一边吃奶一边插下面 | 日本爽快片100色毛片视频 | 国产一级αv片免费观看 | 午夜视频福利 | 91麻豆精品国产91久久久无需广告 | 曰本三级日本三级日本三级 | 色网站在线免费观看 | 免费一级毛片在线播放视频 | 精品无码久久久久久国产 | 一级毛片免费在线 | 欧美精品一区二区久久 | 免费一级a毛片在线播放视 日日草夜夜操 |