深圳安嘉建设有限公司网站网站域名是什
news/
2025/9/23 0:20:27/
文章来源:
深圳安嘉建设有限公司网站,网站域名是什,合肥建设学校网站,小众设计公司logo** 1.实现了Interceptor接口#xff0c;并实现了两个拦截方法#xff1a;update和query。当Mybatis执行update或query语句时#xff0c;会自动调用intercept法。intercept方法首先获取当前执行的SQL语句#xff0c;并计算执行该SQL语句所需的时间。然后#xff0c;它将执行…** 1.实现了Interceptor接口并实现了两个拦截方法update和query。当Mybatis执行update或query语句时会自动调用intercept法。intercept方法首先获取当前执行的SQL语句并计算执行该SQL语句所需的时间。然后它将执行的结果返回给Mybatis。此外此插件还会将执行时间大于1000毫秒、5000毫秒和10000毫秒的SQL语句记录到日志文件中。**
package com.wyh.subject.infra.config;import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.Properties;Intercepts({Signature(type Executor.class, method update, args {MappedStatement.class,Object.class}),Signature(type Executor.class, method query, args {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})})
public class SqlStatementInterceptor implements Interceptor {public static final Logger log LoggerFactory.getLogger(sys-sql); //定义一个log的静态变量来记录日志Overridepublic Object intercept(Invocation invocation) throws Throwable {long startTime System.currentTimeMillis();try {return invocation.proceed();} finally {long timeConsuming System.currentTimeMillis() - startTime;log.info(执行SQL:{}ms, timeConsuming);if (timeConsuming 999 timeConsuming 5000) {log.info(执行SQL大于1s:{}ms, timeConsuming);} else if (timeConsuming 5000 timeConsuming 10000) {log.info(执行SQL大于5s:{}ms, timeConsuming);} else if (timeConsuming 10000) {log.info(执行SQL大于10s:{}ms, timeConsuming);}}}Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}Overridepublic void setProperties(Properties properties) {}
}2.打印sql日志将数据填入sql中的
package com.wyh.subject.infra.config;import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;/*** 打印sql日志将数据填入sql中的*/
public class MybatisPlusAllSqlLog implements InnerInterceptor {public static final Logger log LoggerFactory.getLogger(sys-sql);Overridepublic void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {logInfo(boundSql, ms, parameter);}Overridepublic void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {BoundSql boundSql ms.getBoundSql(parameter);logInfo(boundSql, ms, parameter);}private static void logInfo(BoundSql boundSql, MappedStatement ms, Object parameter) {try {log.info(parameter parameter);// 获取到节点的id,即sql语句的idString sqlId ms.getId();log.info(sqlId sqlId);// 获取节点的配置Configuration configuration ms.getConfiguration();// 获取到最终的sql语句String sql getSql(configuration, boundSql, sqlId);log.info(完整的sql:{}, sql);} catch (Exception e) {log.error(异常:{}, e.getLocalizedMessage(), e);}}// 封装了一下sql语句使得结果返回完整xml路径下的sql语句节点id sql语句public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId) {return sqlId : showSql(configuration, boundSql);}// 进行的替换public static String showSql(Configuration configuration, BoundSql boundSql) {// 获取参数Object parameterObject boundSql.getParameterObject();ListParameterMapping parameterMappings boundSql.getParameterMappings();// sql语句中多个空格都用一个空格代替String sql boundSql.getSql().replaceAll([\\s], );if (!CollectionUtils.isEmpty(parameterMappings) parameterObject ! null) {// 获取类型处理器注册器类型处理器的功能是进行java类型和数据库类型的转换TypeHandlerRegistry typeHandlerRegistry configuration.getTypeHandlerRegistry();// 如果根据parameterObject.getClass(可以找到对应的类型则替换if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {sql sql.replaceFirst(\\?,Matcher.quoteReplacement(getParameterValue(parameterObject)));} else {// MetaObject主要是封装了originalObject对象提供了get和set的方法用于获取和设置originalObject的属性值,主要支持对JavaBean、Collection、Map三种类型对象的操作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(\\?,Matcher.quoteReplacement(getParameterValue(obj)));} else if (boundSql.hasAdditionalParameter(propertyName)) {// 该分支是动态sqlObject obj boundSql.getAdditionalParameter(propertyName);sql sql.replaceFirst(\\?,Matcher.quoteReplacement(getParameterValue(obj)));} else {// 打印出缺失提醒该参数缺失并防止错位sql sql.replaceFirst(\\?, 缺失);}}}}return sql;}// 如果参数是String则添加单引号 如果是日期则转换为时间格式器并加单引号 对参数是null和不是null的情况作了处理private static String getParameterValue(Object obj) {String value;if (obj instanceof String) {value obj.toString() ;} else if (obj instanceof Date) {DateFormat formatter DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.CHINA);value formatter.format(new Date()) ;} else {if (obj ! null) {value obj.toString();} else {value ;}}return value;}}package com.wyh.subject.infra.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MybatisConfiguration {/*** 配置MybatisPlus的sql日志配置将替换* return*/Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor mybatisPlusInterceptor new MybatisPlusInterceptor();mybatisPlusInterceptor.addInnerInterceptor(new MybatisPlusAllSqlLog());return mybatisPlusInterceptor;}}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/910860.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!