最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis自動(dòng)填充時(shí)間字段示例代碼

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

前言

對(duì)于實(shí)體中的created_on和updated_on來(lái)說(shuō),它沒(méi)有必要被開(kāi)發(fā)人員去干預(yù),因?yàn)樗呀?jīng)足夠說(shuō)明使用場(chǎng)景了,即在插入數(shù)據(jù)和更新數(shù)據(jù)時(shí),記錄當(dāng)前時(shí)間,這對(duì)于mybatis來(lái)說(shuō),通過(guò)攔截器是可以實(shí)現(xiàn)的,記得之前說(shuō)過(guò)在jpa中實(shí)現(xiàn)的方法,主要通過(guò)jpa的注解實(shí)現(xiàn)的,因?yàn)榻裉斓膍ybatis需要用到j(luò)ava的攔截器。

下面話不多說(shuō)了,來(lái)一起看看詳細(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;
}

定義攔截器,重寫(xiě)賦值的語(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à)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(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)系解讀

    這篇文章主要介紹了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)代碼

    SpringBoot動(dòng)態(tài)表操作服務(wù)的實(shí)現(xiàn)代碼

    在現(xiàn)代的應(yīng)用開(kāi)發(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ù)的配置方法

    Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫(kù)的配置方法

    Redis是目前業(yè)界使用最廣泛的內(nèi)存數(shù)據(jù)存儲(chǔ)。下面通過(guò)本文給大家介紹Spring Boot 與 Kotlin 使用Redis數(shù)據(jù)庫(kù)的配置方法,感興趣的朋友一起看看吧
    2018-01-01
  • java冷知識(shí):javac AbstractProcessor詳解

    java冷知識(shí):javac AbstractProcessor詳解

    這篇文章主要介紹了java冷知識(shí):javac AbstractProcessor詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java知識(shí)點(diǎn)歸納 —給Java新手的一些建議(新手必看)

    Java知識(shí)點(diǎn)歸納 —給Java新手的一些建議(新手必看)

    以下簡(jiǎn)單介紹了下我對(duì)于這些java基本知識(shí)點(diǎn)和技術(shù)點(diǎn)的一些看法和心得,這些內(nèi)容都源自于我這些年來(lái)使用java的一些總結(jié)
    2016-05-05
  • Java實(shí)現(xiàn)n位數(shù)字的全排列

    Java實(shí)現(xiàn)n位數(shù)字的全排列

    今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)n位數(shù)字的全排列,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • 程序包org.springframework不存在的解決辦法

    程序包org.springframework不存在的解決辦法

    這篇文章主要介紹了程序包org.springframework不存在的解決辦法,在使用IDEA創(chuàng)建SpringBoot項(xiàng)目時(shí),剛打開(kāi)無(wú)法正常運(yùn)行,本文通過(guò)圖文結(jié)合的方式給大家介紹的非常詳細(xì),具有一定參考價(jià)值,需要的朋友可以參考下
    2024-07-07
  • SpringMVC前端和后端數(shù)據(jù)交互總結(jié)

    SpringMVC前端和后端數(shù)據(jù)交互總結(jié)

    本篇文章主要介紹了SpringMVC前端和后端數(shù)據(jù)交互總結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Java稀疏數(shù)組詳細(xì)圖文教程

    Java稀疏數(shù)組詳細(xì)圖文教程

    當(dāng)一個(gè)數(shù)組中的大部分元素為相同的值,可使用稀疏數(shù)組來(lái)保存該數(shù)組,可以將稀疏數(shù)組看做是普通數(shù)組的壓縮,這篇文章主要給大家介紹了關(guān)于Java稀疏數(shù)組的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • springboot讀取application.yml報(bào)錯(cuò)問(wèn)題及解決

    springboot讀取application.yml報(bào)錯(cuò)問(wèn)題及解決

    這篇文章主要介紹了springboot讀取application.yml報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06

最新評(píng)論

遂平县| 通渭县| 安庆市| 富宁县| 泰来县| 潼南县| 宁晋县| 庆元县| 海南省| 武城县| 石屏县| 岳阳县| 米脂县| 交城县| 徐汇区| 周口市| 汕尾市| 九龙坡区| 呼伦贝尔市| 湖南省| 咸阳市| 教育| 林西县| 纳雍县| 安岳县| 桂平市| 东安县| 昭平县| 翼城县| 景东| 太白县| 克东县| 建始县| 肥东县| 横山县| 容城县| 洛川县| 巴彦淖尔市| 安顺市| 资溪县| 台中市|