溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mybatis Interceptor對UpdateTime自動處理的實現(xiàn)方法

發(fā)布時間:2020-09-28 02:01:56 來源:腳本之家 閱讀:220 作者:三不猴 欄目:編程語言

前言

一般數(shù)據(jù)庫的表結(jié)構(gòu)都會有update_time,修改時間,因為這個字段基本與業(yè)務沒有太大關(guān)聯(lián),因此開發(fā)過程中經(jīng)常會忘記設置這兩個字段的值,本插件就是來解決這個問題。同樣的想生成id,create_time等操作都是可以以同樣的方式解決。想折騰的同學還可以通過這中方式自己寫個分頁插件。

閑話少說上代碼。

1. 先寫一個自定義注解標注是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標注這是個mybatis插件,@Signature標注要攔截的操作

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時將當前時間加入
 */
@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)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI