欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2019年01月18日 08:35:17   作者:張占嶺  
這篇文章主要給大家介紹了關(guān)于mybatis自動填充時間字段的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

對于實體中的created_on和updated_on來說,它沒有必要被開發(fā)人員去干預,因為它已經(jīng)足夠說明使用場景了,即在插入數(shù)據(jù)和更新數(shù)據(jù)時,記錄當前時間,這對于mybatis來說,通過攔截器是可以實現(xiàn)的,記得之前說過在jpa中實現(xiàn)的方法,主要通過jpa的注解實現(xiàn)的,因為今天的mybatis需要用到j(luò)ava的攔截器。

下面話不多說了,來一起看看詳細的介紹吧

定義兩個注解

@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());
 }

解決是我們所預想的,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
)

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Java8中使用流方式查詢數(shù)據(jù)庫的方法

    Java8中使用流方式查詢數(shù)據(jù)庫的方法

    這篇文章主要介紹了Java8中使用流方式查詢數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Git和Maven的子模塊簡單實踐

    Git和Maven的子模塊簡單實踐

    今天小編就為大家分享一篇關(guān)于Git和Maven的子模塊簡單實踐,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java實戰(zhàn)之酒店人事管理系統(tǒng)的實現(xiàn)

    Java實戰(zhàn)之酒店人事管理系統(tǒng)的實現(xiàn)

    這篇文章主要介紹了如何用Java實現(xiàn)酒店人事管理系統(tǒng),文中采用的技術(shù)有:JSP、Spring、SpringMVC、MyBatis等,感興趣的小伙伴可以學習一下
    2022-03-03
  • Java實現(xiàn)簡單學生管理系統(tǒng)

    Java實現(xiàn)簡單學生管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單學生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Java中BigInteger類的使用方法詳解(全網(wǎng)最新)

    Java中BigInteger類的使用方法詳解(全網(wǎng)最新)

    這篇文章主要介紹了Java中BigInteger類的使用方法詳解,常用最全系列,本章作為筆記使用,內(nèi)容比較全面,但常用的只有:構(gòu)造函數(shù),基本運算以及compareTo(),intValue(),setBit(),testBit()方法,需要的朋友可以參考下
    2023-05-05
  • SpringBoot集成七牛云OSS的示例詳解

    SpringBoot集成七牛云OSS的示例詳解

    OSS的英文全稱是Object?Storage?Service,翻譯成中文就是對象存儲服務(wù),官方一點解釋就是對象存儲是一種使用HTTP?API存儲和檢索非結(jié)構(gòu)化數(shù)據(jù)和元數(shù)據(jù)對象的工具,本文給大家詳細介紹了SpringBoot集成七牛云OSS的示例,需要的朋友可以參考下
    2023-11-11
  • Java項目開啟遠程調(diào)試的方法步驟(tomcat、springboot)

    Java項目開啟遠程調(diào)試的方法步驟(tomcat、springboot)

    這篇文章主要介紹了Java項目開啟遠程調(diào)試的方法步驟(tomcat、springboot),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • SpringBoot實現(xiàn)接口版本控制的示例代碼

    SpringBoot實現(xiàn)接口版本控制的示例代碼

    這篇文章主要介紹了springboot如何實現(xiàn)接口版本控制,接口版本控制,比如微服務(wù)請求中某個接口需要升級,正常做法是升級我們的版本,文中有詳細的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下
    2024-03-03
  • 如何在java中使用SFTP協(xié)議安全的傳輸文件

    如何在java中使用SFTP協(xié)議安全的傳輸文件

    這篇文章主要介紹了如何在java中使用SFTP協(xié)議安全的傳輸文件,幫助大家更好的理解和使用JSch,感興趣的朋友可以了解下
    2020-10-10
  • Java中StringBuilder與StringBuffer使用及源碼解讀

    Java中StringBuilder與StringBuffer使用及源碼解讀

    我們前面學習的String就屬于不可變字符串,因為理論上一個String字符串一旦定義好,其內(nèi)容就不可再被改變,但實際上,還有另一種可變字符串,包括StringBuilder和StringBuffer兩個類,那可變字符串有什么特點,又怎么使用呢,接下來就請大家跟我一起來學習吧
    2023-05-05

最新評論