亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MyBatis攔截器如何自動(dòng)設(shè)置創(chuàng)建時(shí)間和修改時(shí)間

 更新時(shí)間:2025年02月11日 11:09:36   作者:倫敦城下的小鞋匠  
文章介紹了如何通過實(shí)現(xiàn)MyBatis的Interceptor接口,在實(shí)體類中自動(dòng)設(shè)置創(chuàng)建時(shí)間和修改時(shí)間,從而提高開發(fā)效率

前言

在日常的插入和修改的時(shí)候要頻繁的插入時(shí)間,浪費(fèi)時(shí)間,可以通過實(shí)現(xiàn)mybatis的 Intercepts注解來實(shí)現(xiàn),獲取實(shí)體,并且在實(shí)體里面插入日期

一、實(shí)現(xiàn)Interceptor接口,并寫相關(guān)邏輯

package com.ruoyi.common.filter;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Properties;


/**
 *  Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)   攔截執(zhí)行器的方法
	ParameterHandler (getParameterObject, setParameters)	攔截參數(shù)的處理
	ResultSetHandler (handleResultSets, handleOutputParameters)	攔截結(jié)果集的處理
	StatementHandler (prepare, parameterize, batch, update, query) 攔截Sql語法構(gòu)建的處理
 * @author Administrator
 *
 */
@Intercepts({@Signature(type = Executor.class,method = "update",args = {MappedStatement.class,Object.class})})
@Component
public class HandleTimeInterceptor implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		 Object[] args = invocation.getArgs();
		 MappedStatement mappedStatement = (MappedStatement) args[0];
		 SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
		 if(sqlCommandType== SqlCommandType.UPDATE) {
			 if(args[1] instanceof BaseEntity) {
				BaseEntity baseEntity = (BaseEntity) args[1];
				baseEntity.setUpdateTime(new Date());
				String userId="";
				 try
				 {
					 userId= SecurityUtils.getUserId().toString();
				 }catch (Exception e){
					// throw new BaseException("當(dāng)前沒有登錄人");

				 }
				baseEntity.setUpdateBy(userId);
			 }
		 }else if(sqlCommandType==SqlCommandType.INSERT) {
			 if(args[1] instanceof BaseEntity) {
				BaseEntity baseEntity = (BaseEntity) args[1];
				baseEntity.setCreateTime(new Date());
				String userId="";
				try
				{
				 userId= SecurityUtils.getUserId().toString();
				}catch (Exception e){
					//throw new BaseException("當(dāng)前沒有登錄人");
				}
				baseEntity.setCreateBy(userId);
			}
		 }

		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
	}
}

二、將插件注冊到mybatis 的配置文件 mybatis-config.xml

<?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>
    <!-- 全局參數(shù) -->
    <settings>
        <!-- 使全局的映射器啟用或禁用緩存 -->
        <setting name="cacheEnabled"             value="true"   />
        <!-- 允許JDBC 支持自動(dòng)生成主鍵 -->
        <setting name="useGeneratedKeys"         value="true"   />
        <!-- 配置默認(rèn)的執(zhí)行器.SIMPLE就是普通執(zhí)行器;REUSE執(zhí)行器會(huì)重用預(yù)處理語句(prepared statements);BATCH執(zhí)行器將重用語句并執(zhí)行批量更新 -->
        <setting name="defaultExecutorType"      value="SIMPLE" />
		<!-- 指定 MyBatis 所用日志的具體實(shí)現(xiàn) -->
        <setting name="logImpl"                  value="SLF4J"  />
        <!-- 使用駝峰命名法轉(zhuǎn)換字段 -->
		 <setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
    <plugins>
        <plugin interceptor="com.ruoyi.common.filter.HandleTimeInterceptor"></plugin>

    </plugins>
</configuration>

總結(jié)

然后就可以實(shí)現(xiàn)在實(shí)體里面自動(dòng)設(shè)置創(chuàng)建時(shí)間和修改時(shí)間

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于Java中的dozer對象轉(zhuǎn)換問題

    關(guān)于Java中的dozer對象轉(zhuǎn)換問題

    Dozer是Java?Bean到Java?Bean映射器,它以遞歸方式將數(shù)據(jù)從一個(gè)對象復(fù)制到另一個(gè)對象,這篇文章主要介紹了Java中的dozer對象轉(zhuǎn)換的操作方法,需要的朋友可以參考下
    2022-08-08
  • SpringCloud動(dòng)態(tài)配置注解@RefreshScope與@Component的深度解析

    SpringCloud動(dòng)態(tài)配置注解@RefreshScope與@Component的深度解析

    在現(xiàn)代微服務(wù)架構(gòu)中,動(dòng)態(tài)配置管理是一個(gè)關(guān)鍵需求,本文將為大家介紹Spring Cloud中相關(guān)的注解@RefreshScope與@Component的使用,需要的小伙伴可以參考下
    2025-04-04
  • 解決@RequestBody搭配@Data的大坑

    解決@RequestBody搭配@Data的大坑

    這篇文章主要介紹了解決@RequestBody搭配@Data的大坑,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java實(shí)現(xiàn)InputStream的任意拷貝方式

    Java實(shí)現(xiàn)InputStream的任意拷貝方式

    這篇文章主要介紹了Java實(shí)現(xiàn)InputStream的任意拷貝方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • springboot yml中profiles的巧妙用法(小白必看多環(huán)境配置)

    springboot yml中profiles的巧妙用法(小白必看多環(huán)境配置)

    這篇文章主要介紹了springboot yml中profiles的巧妙用法,非常適合多環(huán)境配置場景,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • Spring @Bean注解的使用場景與案例實(shí)現(xiàn)

    Spring @Bean注解的使用場景與案例實(shí)現(xiàn)

    隨著SpringBoot的流行,我們現(xiàn)在更多采用基于注解式的配置從而替換掉了基于XML的配置,所以本篇文章我們主要探討基于注解的@Bean以及和其他注解的使用
    2023-03-03
  • Mybatis實(shí)現(xiàn)批量操作8種小結(jié)

    Mybatis實(shí)現(xiàn)批量操作8種小結(jié)

    本文對Mybatis的五種批處理方式進(jìn)行了性能測試,包括批量新增和批量修改,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • spring IOC中三種依賴注入方式

    spring IOC中三種依賴注入方式

    這篇文章主要介紹了spring IOC中三種依賴注入方式,Spring使用注入方式,為什么使用注入方式,這系列問題實(shí)際歸結(jié)起來就是一句話,Spring的注入和IoC(本人關(guān)于IoC的闡述)反轉(zhuǎn)控制是一回事
    2021-08-08
  • java list常用方法總結(jié)

    java list常用方法總結(jié)

    這篇文章主要介紹了java list常用方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 詳解Java8與Runtime.getRuntime().availableProcessors()

    詳解Java8與Runtime.getRuntime().availableProcessors()

    這篇文章主要介紹了詳解Java8與Runtime.getRuntime().availableProcessors(),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評論