mybatis自動(dòng)填充時(shí)間字段示例代碼
前言
對(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è)注解
@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è)注解
@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;
}
定義攔截器,重寫賦值的語(yǔ)句
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 語(yǔ)句插入 createTime
field.setAccessible(true);
field.set(parameter, LocalDateTime.now());
}
}
if (field.getAnnotation(UpdatedOnFuncation.class) != null) { // insert 或 update 語(yǔ)句插入 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è)試用例
@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)賦上值了。
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ì)腳本之家的支持。
相關(guān)文章
JDK?version和class?file?version(Class編譯版本號(hào))對(duì)應(yīng)關(guān)系解讀
這篇文章主要介紹了JDK?version和class?file?version(Class編譯版本號(hào))對(duì)應(yīng)關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot動(dòng)態(tài)表操作服務(wù)的實(shí)現(xiàn)代碼
在現(xiàn)代的應(yīng)用開發(fā)中,尤其是在數(shù)據(jù)庫(kù)設(shè)計(jì)不斷變化的情況下,動(dòng)態(tài)操作數(shù)據(jù)庫(kù)表格成為了不可或缺的一部分,在本篇文章中,我們將以一個(gè)典型的動(dòng)態(tài)表操作服務(wù)為例,詳細(xì)介紹如何在 Spring Boot 中使用 JdbcTemplate 實(shí)現(xiàn)動(dòng)態(tài)表管理,需要的朋友可以參考下2025-01-01
Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫(kù)的配置方法
Redis是目前業(yè)界使用最廣泛的內(nèi)存數(shù)據(jù)存儲(chǔ)。下面通過本文給大家介紹Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫(kù)的配置方法,感興趣的朋友一起看看吧2018-01-01
java冷知識(shí):javac AbstractProcessor詳解
這篇文章主要介紹了java冷知識(shí):javac AbstractProcessor詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java知識(shí)點(diǎn)歸納 —給Java新手的一些建議(新手必看)
以下簡(jiǎn)單介紹了下我對(duì)于這些java基本知識(shí)點(diǎn)和技術(shù)點(diǎn)的一些看法和心得,這些內(nèi)容都源自于我這些年來使用java的一些總結(jié)2016-05-05
Java實(shí)現(xiàn)n位數(shù)字的全排列
今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)n位數(shù)字的全排列,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
程序包org.springframework不存在的解決辦法
這篇文章主要介紹了程序包org.springframework不存在的解決辦法,在使用IDEA創(chuàng)建SpringBoot項(xiàng)目時(shí),剛打開無法正常運(yùn)行,本文通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),具有一定參考價(jià)值,需要的朋友可以參考下2024-07-07
SpringMVC前端和后端數(shù)據(jù)交互總結(jié)
本篇文章主要介紹了SpringMVC前端和后端數(shù)據(jù)交互總結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
springboot讀取application.yml報(bào)錯(cuò)問題及解決
這篇文章主要介紹了springboot讀取application.yml報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

