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

相关文章

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件