spring boot mybatis拦截器
mybaits拦截器
package com.chinamobile.scm.masterdata.interceptor;import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.stereotype.Component;import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;@Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),@Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),@Signature(type = StatementHandler.class, method = "batch", args = { Statement.class })})
@Slf4j
@Component
public class ExecutorInterceptor implements Interceptor {/*** 根目录*/public String sqllogpath="/Users/yh/sqllog";@Overridepublic Object intercept(Invocation invocation) throws Throwable {log.error("来了老弟:");Object target = invocation.getTarget();StatementHandler statementHandler = (StatementHandler)target;BoundSql boundSql = statementHandler.getBoundSql();String sql = showSql(boundSql);//boundSql.getSql();Object parameterObject = boundSql.getParameterObject();List<ParameterMapping> parameterMappingList = boundSql.getParameterMappings();System.out.println(sql);String updatestr= JSON.toJSONString(boundSql);writeFile(updatestr,sqllogpath);return invocation.proceed();}@Overridepublic Object plugin(Object o) {return Plugin.wrap(o, this);}@Overridepublic void setProperties(Properties properties) {}private String getParameterValue(Object obj) {//to_timestamp(to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')String value = null;if (obj instanceof String) {value = "'" + obj.toString() + "'";} else if (obj instanceof java.sql.Timestamp) {DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);value = "to_timestamp(to_char(" + formatter.format(obj) + ",'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')";}else if (obj instanceof Date) {DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);value = "'" + formatter.format(obj) + "'";
// System.out.println(value);} else {if (obj != null) {value = obj.toString();} else {value = "";}}return value;}public String showSql(BoundSql boundSql) {Object parameterObject = boundSql.getParameterObject();List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();String sql = boundSql.getSql().replaceAll("[\\s]+", " ");if (parameterMappings.size() > 0 && parameterObject != null) {//TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
// if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
// sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
//
// } else {Configuration configuration=new Configuration();MetaObject metaObject = configuration.newMetaObject(parameterObject);for (ParameterMapping parameterMapping : parameterMappings) {String propertyName = parameterMapping.getProperty();if (metaObject.hasGetter(propertyName)) {Object obj = metaObject.getValue(propertyName);sql = sql.replaceFirst("\\?", getParameterValue(obj));} else if (boundSql.hasAdditionalParameter(propertyName)) {Object obj = boundSql.getAdditionalParameter(propertyName);sql = sql.replaceFirst("\\?", getParameterValue(obj));}}// }}return sql;}public void writeFile(String data,String resultfilepath){try {Date date = new Date();String path=resultfilepath+"/"+new SimpleDateFormat("yyyy-MM-dd/").format(date);File f = new File(path);if(!f.exists()){f.mkdirs(); //创建目录}String filepath=path+ System.currentTimeMillis()+".json";File file = new File(filepath);if(!file.exists()){file.createNewFile();}FileWriter fw = null;//true:表示是追加的标志fw = new FileWriter(file, true);fw.write(data);fw.close();} catch (Exception e) {e.printStackTrace();}finally{}}
}
拦截器启动生效
package com.chinamobile.scm.masterdata.interceptor;import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Properties;@Configuration
public class MybatisInterceptorConfig {@Beanpublic String myInterceptor(SqlSessionFactory sqlSessionFactory) {sqlSessionFactory.getConfiguration().addInterceptor(new ExecutorInterceptor());
// sqlSessionFactory.getConfiguration().addInterceptor(executorInterceptor);
// sqlSessionFactory.getConfiguration().addInterceptor(new ParamInterceptor());
// sqlSessionFactory.getConfiguration().addInterceptor(new ResultInterceptor());return "interceptor";}
}
spring boot mybatis拦截器相关推荐
- Spring Boot+Redis+拦截器+自定义Annotation实现接口自动幂等
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 前言 在实际的开发项目中,一个对外暴露的接口往往会面临很多次请求, ...
- Spring Boot笔记-拦截器相关(用户权限方面)
Spring Boot拦截器的使用 使用注解@Configuration 继承WebMvcConfigurerAdapter 重写addInterceptors添加需要拦截器地址: 运行截图如下: 后 ...
- Spring boot自定义拦截器和拦截器重定向配置简单介绍~!
大家好: 本文简单介绍一下用于权限控制的Spring boot拦截器配置,拦截器重定向问题. 开发工具:jdk1.8 idea2017(付费版,网上找的破解教程) 1,首先使用idea创建一个Sp ...
- Spring Boot 添加拦截器的配置方式
在进行 Java Web 开发的时候我们经常会使用到过滤器,例如日志的记录.权限的验证等功能.以前使用 Spring MVC 的时候需要在 web.xml 中配置过滤器,现在使用 Spring Boo ...
- 一步步教你mybatis分页,mybatis分页拦截器 使用,mybatis拦截器分页
mybatis 分页详解.mybatis分页查询,mybatis分页拦截器使用.struts2下mybatis分页 mybatis默认是支持分页的,内部通过创建可滚动的Result ...
- 关于Mybatis拦截器的使用
关于Mybatis拦截器的使用 1 Mybatis拦截器的使用 1 自定义拦截器 1 Interceptor接口 2 @Intercepts注解 3 @Signature注解 2 注册拦截器 3 拦截 ...
- Spring 事务 以及拦截器的前后关系实验 Mybatis 日志拦截
背景:当一个线程中,如果需要拦截所有当SQL日志,然后统一发送到一个同步器,就可以实现多个数据库实现同步主库,在进行红绿上线,或者灰度部署时候,可以实现生产库与测试库实时同步,从而达到实时可切换的效果 ...
- PageHelper 在 Spring Boot + MyBatis 中合理且规范的使用方法
点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 一. 开发准备 1. 开发工具 IntelliJ IDEA ...
- Spring Boot + Mybatis 实现动态数据源
动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...
最新文章
- 2019微生物组—宏基因组分析专题培训开课啦!
- nginx 带宽_Nginx优化配置,轻松应对十万并发
- Javascript获取当月的天数
- 由laravel 5.5无法获取url中的参数引发的apache的.htaccess文件问题
- lisp 线性标注自动避让_自动化数据增强:实践、理论和新方向
- 阿里巴巴分布式服务框架 Dubbo
- python函数后面有多个括号怎么理解?
- java文本区显示在右边_怎么让文本区从右边开始显示文本
- Java的原始字符串文字
- 消息中间件系列(八):Kafka、RocketMQ、RabbitMQ等的优劣势比较
- Activity 半透明样式
- JavaScript深入之从原型到原型链
- jdk说明文档_给JDK报了一个P4的Bug,结果居然……
- java的 jre是什么_Java中JRE介绍,JRE是什么
- Ubuntu使用WakeOnLan远程开机
- Java多线程基础(十三)——Thread-Specific Storage(ThreadLocal)模式
- 运算放大器---转换速率(slew rate)
- 快捷指令通知运行html,快捷指令怎样运行这段,一个书签
- Linux 启动优化实战-2.41 秒启动应用!
- python作业网站_怒刷python作业-WEB资讯专栏-DMOZ中文网站分类目录-免费收录各类优秀网站的中文网站目录....