当前位置: 首页> 财经> 股票 > 江西省地图_免费wap建站的网址是什么了_无锡seo_关键词点击价格查询

江西省地图_免费wap建站的网址是什么了_无锡seo_关键词点击价格查询

时间:2025/7/12 2:34:41来源:https://blog.csdn.net/m0_64637029/article/details/145018709 浏览次数:1次
江西省地图_免费wap建站的网址是什么了_无锡seo_关键词点击价格查询

文章目录

    • 1.common-mybatis-plus-starter
        • 1.目录
        • 2.引入依赖
        • 3.SqlBeautyInterceptor.java SQL优化器
        • 4.MybatisPLusAutoConfiguration.java 自动配置类,根据条件注入SQL优化器
        • 5.spring.factories 指定自动配置类
    • 2.common-mybatis-plus-starter-demo
        • 1.application.yml 开启sql优化器
        • 2.测试

1.common-mybatis-plus-starter

1.目录

CleanShot 2024-10-23 at 23.50.28@2x

2.引入依赖
        <!-- SpringBoot自动配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><!-- SQL Formatter --><dependency><groupId>com.github.vertical-blank</groupId><artifactId>sql-formatter</artifactId><version>2.0.3</version></dependency><!-- jitpack.io仓库,用于下载SQL Formatter --><repositories><repository><id>jitpack.io</id><url>https://jitpack.io</url></repository></repositories>
3.SqlBeautyInterceptor.java SQL优化器
package com.sunxiansheng.mybatis.plus.inteceptor;import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.*;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.vertical_blank.sqlformatter.SqlFormatter;
import com.github.vertical_blank.sqlformatter.languages.Dialect;import java.sql.Statement;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;/*** SQL优化器:显示完整的SQL*/
@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})
})
public class SqlBeautyInterceptor implements Interceptor {private static final Logger logger = LoggerFactory.getLogger(SqlBeautyInterceptor.class);@Overridepublic Object intercept(Invocation invocation) throws Throwable {StatementHandler statementHandler = (StatementHandler) invocation.getTarget();// 通过 MetaObject 获取 delegate 对象MetaObject metaObject = SystemMetaObject.forObject(statementHandler);// 获取 MappedStatementMappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");// 获取 ConfigurationConfiguration configuration = mappedStatement.getConfiguration();long startTime = System.currentTimeMillis();try {return invocation.proceed();} finally {long endTime = System.currentTimeMillis();long sqlCost = endTime - startTime;BoundSql boundSql = statementHandler.getBoundSql();String sql = boundSql.getSql();Object parameterObject = boundSql.getParameterObject();// 创建 MetaObject 实例MetaObject metaObjectParam = configuration.newMetaObject(parameterObject);// 格式化 SQLString formattedSql = formatSql(sql, boundSql, metaObjectParam);// 日志级别为 INFO,输出格式化后的 SQLlogger.info("\n========================\nSQL:\n{}\n执行耗时: [{} ms]\n========================", formattedSql, sqlCost);}}@Overridepublic Object plugin(Object target) {// 使用 Plugin.wrap 生成代理对象return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {// 可以通过配置文件设置属性}/*** 格式化 SQL,将参数替换到 SQL 中,并使用 SQLFormatter 进行格式化** @param sql        原始 SQL* @param boundSql   绑定的 SQL* @param metaObject 参数对象的 MetaObject* @return 格式化后的 SQL*/private String formatSql(String sql, BoundSql boundSql, MetaObject metaObject) {if (sql == null || sql.trim().isEmpty()) {return "";}try {// 获取参数映射List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();if (parameterMappings != null && !parameterMappings.isEmpty()) {// 遍历参数映射,替换占位符for (ParameterMapping parameterMapping : parameterMappings) {String propertyName = parameterMapping.getProperty();Object value;if (metaObject.hasGetter(propertyName)) {value = metaObject.getValue(propertyName);} else if (boundSql.hasAdditionalParameter(propertyName)) {value = boundSql.getAdditionalParameter(propertyName);} else {// 对于无法获取值的参数,使用 "?" 替代value = "?";}sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(value)));}}// 使用 SQLFormatter 格式化 SQL,指定数据库方言(根据实际情况选择)String formattedSql = SqlFormatter.of(Dialect.MySql).format(sql);return formattedSql;} catch (Exception e) {logger.error("Error formatting SQL: ", e);return sql;}}/*** 获取参数值的字符串表示,处理字符串、日期等类型** @param obj 参数值* @return 字符串表示*/private String getParameterValue(Object obj) {if (obj == null) {return "null";}if (obj instanceof String) {return "'" + obj + "'";}if (obj instanceof Number) {return obj.toString();}if (obj instanceof java.util.Date || obj instanceof java.sql.Date || obj instanceof java.sql.Timestamp) {return "'" + obj.toString() + "'";}return "'" + obj.toString() + "'";}
}
4.MybatisPLusAutoConfiguration.java 自动配置类,根据条件注入SQL优化器
package com.sunxiansheng.mybatis.plus.config;import com.sunxiansheng.mybatis.plus.inteceptor.SqlBeautyInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** Description: MybatisPLus自动配置类** @Author sun* @Create 2024/10/23 23:00* @Version 1.0*/
@Configuration
public class MybatisPLusAutoConfiguration {/*** 当sql.beauty.enabled=true时,将SqlBeautyInterceptor实例注入容器** @return*/@Bean@ConditionalOnMissingBean@ConditionalOnProperty(prefix = "sql.beauty", value = "enabled", havingValue = "true")public SqlBeautyInterceptor sqlBeautyInterceptor() {return new SqlBeautyInterceptor();}
}
5.spring.factories 指定自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.mybatis.plus.config.MybatisPLusAutoConfiguration

2.common-mybatis-plus-starter-demo

1.application.yml 开启sql优化器
sql:beauty:enabled: true # 是否开启sql美化(自定义的)

CleanShot 2024-10-23 at 23.53.31@2x

2.测试

CleanShot 2024-10-23 at 23.54.38@2x

关键字:江西省地图_免费wap建站的网址是什么了_无锡seo_关键词点击价格查询

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: