mybatis自動(dòng)填充時(shí)間字段示例代碼
前言
對(duì)于實(shí)體中的created_on和updated_on來說,它沒有必要被開發(fā)人員去干預(yù),因?yàn)樗呀?jī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; }
定義攔截器,重寫賦值的語句
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; } }
添加測試用例
@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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java實(shí)戰(zhàn)之酒店人事管理系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何用Java實(shí)現(xiàn)酒店人事管理系統(tǒng),文中采用的技術(shù)有:JSP、Spring、SpringMVC、MyBatis等,感興趣的小伙伴可以學(xué)習(xí)一下2022-03-03Java實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Java中BigInteger類的使用方法詳解(全網(wǎng)最新)
這篇文章主要介紹了Java中BigInteger類的使用方法詳解,常用最全系列,本章作為筆記使用,內(nèi)容比較全面,但常用的只有:構(gòu)造函數(shù),基本運(yùn)算以及compareTo(),intValue(),setBit(),testBit()方法,需要的朋友可以參考下2023-05-05Java項(xiàng)目開啟遠(yuǎn)程調(diào)試的方法步驟(tomcat、springboot)
這篇文章主要介紹了Java項(xiàng)目開啟遠(yuǎn)程調(diào)試的方法步驟(tomcat、springboot),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10SpringBoot實(shí)現(xiàn)接口版本控制的示例代碼
這篇文章主要介紹了springboot如何實(shí)現(xiàn)接口版本控制,接口版本控制,比如微服務(wù)請求中某個(gè)接口需要升級(jí),正常做法是升級(jí)我們的版本,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03Java中StringBuilder與StringBuffer使用及源碼解讀
我們前面學(xué)習(xí)的String就屬于不可變字符串,因?yàn)槔碚撋弦粋€(gè)String字符串一旦定義好,其內(nèi)容就不可再被改變,但實(shí)際上,還有另一種可變字符串,包括StringBuilder和StringBuffer兩個(gè)類,那可變字符串有什么特點(diǎn),又怎么使用呢,接下來就請大家跟我一起來學(xué)習(xí)吧2023-05-05