spring boot mybatis拦截器
生活随笔
收集整理的這篇文章主要介紹了
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拦截器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis动态更新xml文件后热部署
- 下一篇: Spring 事务 以及拦截器的前后关系