Java自定义mybatis拦截器实现创建人等相关信息自动填充

本文主要是介绍Java自定义mybatis拦截器实现创建人等相关信息自动填充,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在实际项目开发中,我们可能需要在mapper层插入相应的数据,而这些数据在各个表基本都有,比如
创建时间,更新时间,创建人,更新人这些,但是又不想在每个业务中都去设置这些值,那么我们就可以使用mybatis拦截器实现数据自动填充。

一、如何实现?
1.首先添加mybatis相关依赖。

        <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.1</version></dependency>

2.自定义mybatis拦截器。

import cn.hutool.core.date.DateUtil;
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 org.apache.ibatis.session.defaults.DefaultSqlSession.StrictMap;
import org.springframework.stereotype.Component;import java.lang.reflect.Field;
import java.util.*;
@Slf4j
@Component
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class MybatisInterceptor implements Interceptor {private static  final String DATETYPE="java.util.Date";private static  final String STRINGTYPE="java.lang.String";@Overridepublic Object intercept(Invocation invocation) throws Throwable {MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];// 获取sql执行类型:insert、update、select、deleteSqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();Object parameter = invocation.getArgs()[1];if (parameter == null) {return invocation.proceed();}//获取当前登录用户idString userId = TokenUtil.getConcurrentUserId();log.debug(" userId is {}....",userId);// 当sql为新增或更新类型时,自动填充操作人相关信息if (SqlCommandType.INSERT == sqlCommandType || SqlCommandType.UPDATE == sqlCommandType) {replaceEntityProperty(parameter,userId,sqlCommandType);}return invocation.proceed();}private void replaceEntityProperty(Object parameter,String userId, SqlCommandType sqlCommandType ) {// StrictMapif (parameter instanceof StrictMap) {replaceStrictMap((StrictMap) parameter,userId,sqlCommandType);} else if (parameter instanceof Map) {replaceMap((Map) parameter,userId,sqlCommandType);} else {replace(parameter,userId,sqlCommandType);}}private void replace(Object parameter,String userId,SqlCommandType sqlCommandType ) {if(SqlCommandType.INSERT == sqlCommandType){Field[] fields = getAllFields(parameter);for (Field field : fields) {try {//先设置可访问 获取值校验field.setAccessible(true);Object o = field.get(parameter);//如果不为空 则跳过 为空才设置默认值//异步执行时 取不到用户信息 这时需要手动设置if(Objects.nonNull(o)){field.setAccessible(false);continue;}if ("deleted".equals(field.getName())) {field.set(parameter, Integer.valueOf("0"));field.setAccessible(false);}else if ("createdBy".equals(field.getName())) {field.set(parameter, userId);field.setAccessible(false);}else if ("createdTime".equals(field.getName())) {String type = field.getType().getName();if(DATETYPE.equals(type)){field.set(parameter, new Date());}else if(STRINGTYPE.equals(type)) {field.set(parameter, DateUtil.formatDateTime(new Date()));}field.setAccessible(false);}else {updateProperty(parameter, field, userId);}} catch (Exception e) {log.error("failed to insert data, exception = ", e);}}}else if(SqlCommandType.UPDATE == sqlCommandType){Field[] fields = getAllFields(parameter);for (Field field : fields) {try {//先设置可访问 获取值校验field.setAccessible(true);Object o = field.get(parameter);//如果不为空 则跳过 为空才设置默认值//异步执行时 取不到用户信息 这时需要手动设置if(Objects.nonNull(o)){field.setAccessible(false);continue;}//更新时只判断是否更新的属性updateProperty(parameter, field, userId);}catch (Exception e){log.error("failed to update data, exception = ", e);}}}}private void replaceStrictMap(StrictMap map, String userId, SqlCommandType sqlCommandType ) {if (map.containsKey("collection")) {Object collection = map.get("collection");for (Object t : (Collection) collection) {replace(t,userId,sqlCommandType);}} else if (map.containsKey("array")) {Object collection = map.get("array");for (Object t : (Object[]) collection) {replace(t,userId,sqlCommandType);}}}private void replaceMap(Map map,String userId, SqlCommandType sqlCommandType) {for (Object value : map.values()) {replace(value,userId,sqlCommandType);}}private void updateProperty(Object parameter, Field field, String userId) throws IllegalAccessException {if ("updatedBy".equals(field.getName())) {field.set(parameter, userId);field.setAccessible(false);}else if ("updatedTime".equals(field.getName())) {String type = field.getType().getName();if (DATETYPE.equals(type)) {field.set(parameter, new Date());} else if (STRINGTYPE.equals(type)) {field.set(parameter, DateUtil.formatDateTime(new Date()));}field.setAccessible(false);}else {//其他属性什么也不做field.setAccessible(false);}}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {}/*** 获取类的所有属性,包括父类*/private Field[] getAllFields(Object object) {Class<?> clazz = object.getClass();List<Field> fieldList = new ArrayList<>();while (clazz != null) {fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));clazz = clazz.getSuperclass();}Field[] fields = new Field[fieldList.size()];fieldList.toArray(fields);return fields;}
}

下面是TokenUtil类,用于获取当前登录用户id。

@Slf4j
public class TokenUtil {public static String getConcurrentUserId() {ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();if (Objects.isNull(requestAttributes)) {return "";} else {HttpServletRequest request = requestAttributes.getRequest();String userId = request.getHeader("x-userId");if (!StringUtils.isBlank(userId)) {return userId;} else {Object attribute = request.getAttribute("x-userId");return Objects.nonNull(attribute) ? attribute.toString() : "";}}}
}

这样就实现了在DAO层 创建人等相关信息自动填充。

这篇关于Java自定义mybatis拦截器实现创建人等相关信息自动填充的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/459509

相关文章

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四: