当前位置: 代码网 > it编程>编程语言>Java > MyBatis拦截器如何自动设置创建时间和修改时间

MyBatis拦截器如何自动设置创建时间和修改时间

2025年02月12日 Java 我要评论
前言在日常的插入和修改的时候要频繁的插入时间,浪费时间,可以通过实现mybatis的 intercepts注解来实现,获取实体,并且在实体里面插入日期一、实现interceptor接口,并写相关逻辑p

前言

在日常的插入和修改的时候要频繁的插入时间,浪费时间,可以通过实现mybatis的 intercepts注解来实现,获取实体,并且在实体里面插入日期

一、实现interceptor接口,并写相关逻辑

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)   拦截执行器的方法
	parameterhandler (getparameterobject, setparameters)	拦截参数的处理
	resultsethandler (handleresultsets, handleoutputparameters)	拦截结果集的处理
	statementhandler (prepare, parameterize, batch, update, query) 拦截sql语法构建的处理
 * @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("当前没有登录人");

				 }
				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("当前没有登录人");
				}
				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>
    <!-- 全局参数 -->
    <settings>
        <!-- 使全局的映射器启用或禁用缓存 -->
        <setting name="cacheenabled"             value="true"   />
        <!-- 允许jdbc 支持自动生成主键 -->
        <setting name="usegeneratedkeys"         value="true"   />
        <!-- 配置默认的执行器.simple就是普通执行器;reuse执行器会重用预处理语句(prepared statements);batch执行器将重用语句并执行批量更新 -->
        <setting name="defaultexecutortype"      value="simple" />
		<!-- 指定 mybatis 所用日志的具体实现 -->
        <setting name="logimpl"                  value="slf4j"  />
        <!-- 使用驼峰命名法转换字段 -->
		 <setting name="mapunderscoretocamelcase" value="true"/>
	</settings>
    <plugins>
        <plugin interceptor="com.ruoyi.common.filter.handletimeinterceptor"></plugin>

    </plugins>
</configuration>

总结

然后就可以实现在实体里面自动设置创建时间和修改时间

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com