国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - mybatis自動填充時間字段示例代碼

mybatis自動填充時間字段示例代碼

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

這篇文章主要給大家介紹了關于mybatis自動填充時間字段的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

對于實體中的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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 在线日韩欧美 | www.av在线| 成人在线视频网站 | 黄色av网站在线免费观看 | 中文字幕视频一区 | 麻豆激情 | 蜜桃一区 | 狠狠狠狠狠狠 | 欧美成人免费在线 | 国产综合亚洲精品一区二 | 精品福利一区二区三区 | 91视频专区 | 日韩色| 久久综合九色综合欧美狠狠 | 亚洲视频区 | 亚洲 欧美 日韩 丝袜 另类 | 99久久99| 日韩国产欧美一区 | 国产片在线看 | 亚洲国产精品久久 | 久久精品91 | 免费观看一区二区三区毛片软件 | 成人综合区一区 | 欧美激情小视频 | av激情在线 | 欧美激情在线精品一区二区三区 | 视频一区二区三 | 日本精品一区二区三区视频 | 亚洲精品成人免费 | 日本高清视频在线 | 国产精品日韩在线观看 | 黄色av影视 | 日韩成人在线一区 | 日韩电影免费在线观看 | 国产精品女教师av久久 | www.av在线播放 | 中文字幕在线观看日韩 | sis001亚洲原创区 | 精品久久久久久久 | 中文字幕欧美激情 | 日韩一区精品 |