自己做网站卖四川省级建设主管部门网站
- 作者: 五速梦信息网
- 时间: 2026年03月21日 04:59
当前位置: 首页 > news >正文
自己做网站卖,四川省级建设主管部门网站,chrome下载,做海报需要用到什么网站素材SpringBoot Mybatis-Plus 日志带参数 1 实现代码2 测试结果 在Spring Boot中#xff0c;MyBatis插件机制通过拦截器#xff08;Interceptor#xff09;来实现。拦截器允许开发人员在执行SQL语句的各个阶段#xff08;如SQL语句创建、参数处理、结果映射等#xff09;插入自… SpringBoot Mybatis-Plus 日志带参数 1 实现代码2 测试结果 在Spring Boot中MyBatis插件机制通过拦截器Interceptor来实现。拦截器允许开发人员在执行SQL语句的各个阶段如SQL语句创建、参数处理、结果映射等插入自定义逻辑。MyBatis的拦截器主要用于增强MyBatis的功能如日志记录、性能监控、实现分页、数据权限控制、SQL日志、动态SQL生成等在SQL执行之前后提供了四个拦截点在方法执行前后进行处理支持不同场景的功能扩展。 类作用Executor用于执行增/删/改/查等数据库操作。ParameterHandler用于处理 SQL 参数。ResultSetHandler用于处理结果集。StatementHandler用于处理 SQL 语句的创建和参数化。 Signature参数说明 参数参数说明type就是指定拦截器类型(Executor, ParameterHandler, StatementHandler, ResultSetHandler)。method是拦截器类型中的方法不是自己写的方法(update, query, prepare …)。args是method中方法的入参。 Executor接口中常见的方法 方法实现原理update执行插入、更新和删除操作。query执行查询操作返回结果集。flushStatements刷新批量执行的 SQL 语句。commit提交事务。rollback回滚事务。getTransaction获取当前事务对象。close关闭执行器。isClosed检查执行器是否已关闭。clearLocalCache清空本地缓存。 Mybatis拦截器的实现原理 方法实现原理注册拦截器拦截器需要在 MyBatis 配置文件中注册通常是在 mybatis-config.xml 中配置也可以通过 Java 配置类进行注册。实现 Interceptor 接口自定义拦截器需要实现 MyBatis 的 Interceptor 接口并重写 intercept 方法。这个方法会接收一个 Invocation 对象表示被拦截的方法调用。调用链MyBatis 在执行 SQL 操作时会按照拦截器的配置顺序依次调用每个拦截器的 intercept 方法。拦截器可以选择在方法执行前后进行处理或者直接修改方法参数和返回值。 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}) }) public class MyInterceptor implements Interceptor {… }1 实现代码 import java.text.DateFormat; import lombok.extern.slf4j.Slf4j;import cn.hutool.core.collection.CollUtil;import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Properties; import java.util.regex.Matcher;import java.sql.Connection;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component;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.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.reflection.DefaultReflectorFactory; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.SystemMetaObject; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.type.TypeHandlerRegistry;Slf4j Component ConditionalOnProperty(prefix mybatis-plus, name customer-log, havingValue true) Intercepts({Signature(type StatementHandler.class, method prepare, args {Connection.class, Integer.class})}) public class MybatisLogger implements Interceptor {Overridepublic Object intercept(Invocation invocation) throws Throwable {StatementHandler handler (StatementHandler) invocation.getTarget();MetaObject object MetaObject.forObject(handler,SystemMetaObject.DEFAULT_OBJECT_FACTORY,SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,new DefaultReflectorFactory());MappedStatement statement (MappedStatement) object.getValue(delegate.mappedStatement);log.info(SQL执行类: {}, statement.getId());log.info(SQL执行类型: {}, statement.getSqlCommandType().toString());BoundSql bound handler.getBoundSql();Configuration configuration statement.getConfiguration();String sql getFullSql(configuration, bound);log.info(SQL语句: {}, sql);long start System.currentTimeMillis();Object value invocation.proceed();long end System.currentTimeMillis();log.info(SQL耗时: {}, (end - start));return value;}Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}Overridepublic void setProperties(Properties properties) {// 可以通过MyBatis配置文件或注解传递属性}public String getFullSql(Configuration conf, BoundSql bound) {Object object bound.getParameterObject();ListParameterMapping list bound.getParameterMappings();String sql bound.getSql().replaceAll([\s], ).toLowerCase(Locale.ROOT);if (CollUtil.isNotEmpty(list) object ! null) {TypeHandlerRegistry type conf.getTypeHandlerRegistry();if (type.hasTypeHandler(object.getClass())) {sql sql.replaceFirst(\?, Matcher.quoteReplacement(getParaValue(object)));} else {MetaObject meta conf.newMetaObject(object);for (ParameterMapping parameterMapping : list) {String name parameterMapping.getProperty();if (meta.hasGetter(name)) {Object obj meta.getValue(name);sql sql.replaceFirst(\?, Matcher.quoteReplacement(getParaValue(obj)));} else if (bound.hasAdditionalParameter(name)) {Object obj bound.getAdditionalParameter(name);sql sql.replaceFirst(\?, Matcher.quoteReplacement(getParaValue(obj)));} else {sql sql.replaceFirst(\?, 缺失);}}}}return sql;}private String getParaValue(Object obj) {if (obj instanceof String) {return obj ;} else if (obj instanceof Date) {DateFormat formatter DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);return formatter.format(new Date()) ;} else {if (obj ! null) {return obj.toString();} else {return ;}}}}2 测试结果 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession264df4aa] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSessiond91483c] was not registered for synchronization because synchronization is not active JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl44d84b5a] will not be managed by Spring 2024-07-05 16:32:27.660 INFO 3264 — [nio-8221-exec-1] com.xu.hander.MybatisLogger : SQL执行类: com.xu.view.mapper.StudentMapper.selectOne 2024-07-05 16:32:27.660 INFO 3264 — [nio-8221-exec-1] com.xu.hander.MybatisLogger : SQL执行类型: SELECT 2024-07-05 16:32:27.660 INFO 3264 — [nio-8221-exec-1] com.xu.hander.MybatisLogger : SQL语句: select id,create_by,create_time,create_hour,create_date,update_by,update_time,tenant_id,user_id,cus_id,page,number,is_share,time,event from student where (cus_id 3 and page 3 and event 5) order by create_time desc limit 1 2024-07-05 16:32:27.663 INFO 3264 — [nio-8221-exec-1] com.xu.hander.MybatisLogger : SQL耗时: 3
相关文章
-
自己做网站卖产品怎么样如果只做p2p种子搜索网站
自己做网站卖产品怎么样如果只做p2p种子搜索网站
- 技术栈
- 2026年03月21日
-
自己做网站吗移动关闭流量自动续费
自己做网站吗移动关闭流量自动续费
- 技术栈
- 2026年03月21日
-
自己做网站可以揽业务吗公司系统软件
自己做网站可以揽业务吗公司系统软件
- 技术栈
- 2026年03月21日
-
自己做网站难吗wordpress 4.9 中文
自己做网站难吗wordpress 4.9 中文
- 技术栈
- 2026年03月21日
-
自己做网站内容读取太慢公司注册地址和实际不一样可以吗
自己做网站内容读取太慢公司注册地址和实际不一样可以吗
- 技术栈
- 2026年03月21日
-
自己做网站能做付费链接吗网络基础知识点
自己做网站能做付费链接吗网络基础知识点
- 技术栈
- 2026年03月21日
