怎么在mybatis中對時間字段進(jìn)行自動填充

怎么在mybatis中對時間字段進(jìn)行自動填充?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)公司一直在為企業(yè)提供服務(wù),多年的磨煉,使我們在創(chuàng)意設(shè)計,全網(wǎng)整合營銷推廣到技術(shù)研發(fā)擁有了開發(fā)經(jīng)驗。我們擅長傾聽企業(yè)需求,挖掘用戶對產(chǎn)品需求服務(wù)價值,為企業(yè)制作有用的創(chuàng)意設(shè)計體驗。核心團(tuán)隊擁有超過10年以上行業(yè)經(jīng)驗,涵蓋創(chuàng)意,策化,開發(fā)等專業(yè)領(lǐng)域,公司涉及領(lǐng)域有基礎(chǔ)互聯(lián)網(wǎng)服務(wù)成都服務(wù)器托管成都app開發(fā)、手機(jī)移動建站、網(wǎng)頁設(shè)計、網(wǎng)絡(luò)整合營銷。

定義兩個注解

@Retention(RetentionPolicy.RUNTIME)
@Target( {ElementType.FIELD})
public @interface CreatedOnFuncation {

 String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target( {ElementType.FIELD})
public @interface UpdatedOnFuncation {

 String value() default "";
}

使用這兩個注解

@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;

/**
 * 時間攔截器.
 */
@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被自動賦上值了。

userinfo:UserInfo
(
id=1085780948955959297, 
name=lind, 
email=test@sina.com, 
createdOn=2019-01-17T14:08:45.665,
updatedOn=2019-01-17T14:08:45.665
)

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

網(wǎng)站欄目:怎么在mybatis中對時間字段進(jìn)行自動填充
URL地址:http://muchs.cn/article20/jiodjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站收錄、定制開發(fā)、軟件開發(fā)品牌網(wǎng)站制作、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作