springboot单独在指定地方输出sql

一般线上项目都是将日志进行关闭,因为mybatis日志打印,时间长了,会占用大量的内存,如果我想在我指定的地方进行打印sql情况,怎么玩呢!

下面这个场景:

某天线上的项目出bug了,日志打印出来是更新成功的,但是数据库的数据却没有更新,这时候我想给执行这条sql打印出来,方便查看,但是一旦开启mybatis的日志打印功能,就回去全局打印所有的日志,我只想在我想打印的地方打印sql,这时候怎么办呢!见下面的列子:

我想在下面红色箭头处打印这句sql

这时候怎么做呢,咱们可以这样,先将SqlSessionFactory对象注入进来,

如下图:

java 复制代码
private final SqlSessionFactory sqlSessionFactory;

    @Autowired
    public LoginService(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

然后在需要打印的地方加上下面的代码即可:

java 复制代码
// 获取执行的SQL语句并打印
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            Configuration sqlSessionConfiguration = sqlSession.getConfiguration();
            BoundSql boundSql = sqlSessionConfiguration
                    .getMappedStatement("com.green.testlocalhost.mapper.AdminUserMapper.selectOneByExample")
                    .getSqlSource()
                    .getBoundSql(userExample);
            String sql = StringUtils.getExecSql(sqlSessionConfiguration, boundSql);
            System.out.println("本次执行的sql语句::" + sql);
        }

注意:com.green.testlocalhost.mapper.AdminUserMapper.selectOneByExample 这个是StatementId,也就是你主动调用db框架的那个类或者接口的包路径(com.green.testlocalhost.mapper.AdminUserMapper),然后在路径后面拼接调用的方法(selectOneByExample )即可

用到的工具方法如下:

java 复制代码
/**
     * 获取执行的sql语句
     * @param configuration
     * @param boundSql
     * @return
     */
    public static String getExecSql(Configuration configuration, BoundSql boundSql) {
        try {
            Object parameterObject = boundSql.getParameterObject();
            List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
            String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
            if (!parameterMappings.isEmpty() && parameterObject != null) {
                TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
                if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                    sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
                } else {
                    MetaObject metaObject = configuration.newMetaObject(parameterObject);
                    for (ParameterMapping parameterMapping : parameterMappings) {
                        String propertyName = parameterMapping.getProperty();
                        Object obj;
                        if (metaObject.hasGetter(propertyName)) {
                            obj = metaObject.getValue(propertyName);
                            sql = sql.replaceFirst("\\?", getParameterValue(obj));
                        } else if (boundSql.hasAdditionalParameter(propertyName)) {
                            obj = boundSql.getAdditionalParameter(propertyName);
                            sql = sql.replaceFirst("\\?", getParameterValue(obj));
                        }
                    }
                }
            }
            return sql;
        } catch (Exception var11) {
            return "";
        }
    }

    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static String getParameterValue(Object obj) {
        String value;
        if (obj instanceof String) {
            value = "'" + obj + "'";
        } else if (obj instanceof Date) {
            value = "'" + SIMPLE_DATE_FORMAT.format(obj) + "'";
        } else if (obj != null) {
            value = obj.toString();
        } else {
            value = "";
        }
        return value;
    }

这样就可以在你指定的代码地方打印sql而不是全局打印sql了

相关推荐
郑州光合科技余经理20 分钟前
本地生活服务系统:成品模块和定制接口怎么划界
java·前端·人工智能·后端·系统架构·php·ai编程
润乾软件4 小时前
SQLazy:分组累计求和
sql·sqlazy
朦胧之6 小时前
PostgreSQL 数据库笔记
人工智能·后端
user_admin_god7 小时前
数据采集/交换系统设计经验
spring boot·spring·策略模式
IT_陈寒8 小时前
Redis缓存雪崩把我坑惨了,这次长记性了
前端·人工智能·后端
LucianaiB9 小时前
用 WorkBuddy 研究腾讯、阿里和 DeepSeek,我没敢直接说「AI 很赚钱」
后端
用户8356290780519 小时前
使用 Python 管理 PDF 属性和元数据
后端·python
分支预测失败9 小时前
RISC-V 上下文切换实战:从 Trap 入口到 Linux 进程调度
后端·嵌入式
学代码的CJY10 小时前
Linux 重定向、管道与环境变量
linux·spring boot·spring
用户83562907805110 小时前
使用 Python 在 Word 文档中创建自定义图表
后端·python