mybatis Interceptor對UpdateTime自動處理的實(shí)現(xiàn)方法
前言
一般數(shù)據(jù)庫的表結(jié)構(gòu)都會有update_time,修改時(shí)間,因?yàn)檫@個字段基本與業(yè)務(wù)沒有太大關(guān)聯(lián),因此開發(fā)過程中經(jīng)常會忘記設(shè)置這兩個字段的值,本插件就是來解決這個問題。同樣的想生成id,create_time等操作都是可以以同樣的方式解決。想折騰的同學(xué)還可以通過這中方式自己寫個分頁插件。
閑話少說上代碼。
1. 先寫一個自定義注解標(biāo)注是update_time
package com.zb.iscrm.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @Auther: 楊紅星 * @Date: 2018/11/28 09:38 * @Description: */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD}) public @interface UpdateTime { String value() default ""; }
2. 寫一個mybatis插件
使用@Intercepts標(biāo)注這是個mybatis插件,@Signature標(biāo)注要攔截的操作
package com.zb.iscrm.mybatisInterceptor; import com.zb.iscrm.annotation.UpdateTime; import com.zb.iscrm.utils.DateUtils; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.plugin.*; import java.lang.reflect.Field; import java.util.Properties; /** * @Auther: 楊紅星 * @Date: 2018/11/28 09:41 * @Description: mybatis插件 用于執(zhí)行Update時(shí)將當(dāng)前時(shí)間加入 */ @Slf4j @Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) }) public class UpdateTimeInterceptor implements Interceptor { @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]; if (parameter != null) { // 獲取成員變量 Field[] declaredFields = parameter.getClass().getDeclaredFields(); for (Field field : declaredFields) { if (field.getAnnotation(UpdateTime.class) != null) { // update 語句插入 updateTime if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) { field.setAccessible(true); if (field.get(parameter) == null) { field.set(parameter, DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS)); } } } } } //同樣的方式也可以在這里添加create_time或者是id的生成等處理 return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }
最后在mybatis的配置文件中注冊插件,然后就大功告成
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--插件注冊--> <plugins> <plugin interceptor="com.zb.iscrm.mybatisInterceptor.UpdateTimeInterceptor"/> </plugins> </configuration>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Mybatis中使用updateBatch進(jìn)行批量更新
- mybatis執(zhí)行批量更新batch update 的方法(oracle,mysql兩種)
- BootStrap+Mybatis框架下實(shí)現(xiàn)表單提交數(shù)據(jù)重復(fù)驗(yàn)證
- MyBatis-Spring配置的講解
- 深入了解MyBatis二級緩存
- Spring Boot整合Mybatis并完成CRUD操作的實(shí)現(xiàn)示例
- Mybatis Interceptor 攔截器的實(shí)現(xiàn)
- MyBatis多數(shù)據(jù)源的兩種配置方式
- MybatisPlus 多租戶架構(gòu)(Multi-tenancy)實(shí)現(xiàn)詳解
- MyBatis通過BATCH批量提交的方法
相關(guān)文章
Spring boot創(chuàng)建自定義starter的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring boot創(chuàng)建自定義starter的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Spring Boot整合mybatis(一)實(shí)例代碼
sprig-boot是一個微服務(wù)架構(gòu),加快了spring工程快速開發(fā),以及簡便了配置。接下來開始spring-boot與mybatis的整合2017-07-07springboot掃碼登錄的簡單實(shí)現(xiàn)
本文主要介紹基于SpringBoot + Vue + Android實(shí)現(xiàn)的掃碼登錄,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09詳解Spring boot Admin 使用eureka監(jiān)控服務(wù)
本篇文章主要介紹了詳解Spring boot Admin 使用eureka監(jiān)控服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12MybatisPlus處理四種表與實(shí)體的映射及id自增策略分析
在最近的工作中,碰到一個比較復(fù)雜的返回結(jié)果,發(fā)現(xiàn)簡單映射已經(jīng)解決不了這個問題了,只好去求助百度,學(xué)習(xí)mybatis表與實(shí)體的映射應(yīng)該怎么寫,將學(xué)習(xí)筆記結(jié)合工作碰到的問題寫下本文,供自身查漏補(bǔ)缺,同時(shí)已被不時(shí)之需2022-10-10