mybatis 打印SQL语句

时间:2024-10-05 07:14:30

按照mybatics官网上来说,打印sql有两种办法。
方法一:在mybatis-中配置加一个setting

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-////DTD Config 3.0//EN"
        "/dtd/">
<configuration>
  <settings>
    ...
    <setting name="logImpl" value="LOG4J"/>
    ...
  </settings>
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

如果是spring集成mybatis的话,在sqlSessionFactory配置好configLocation属性

<bean id="sqlSessionFactory" class="">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:conf/"></property>
        <!-- 自动扫描文件 -->
        <property name="mapperLocations" value="classpath:com/jstudio/user/dao/*.xml"></property>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

方法二:(#位置请自行替换)

1)首先将ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句

=INFO,stdout

.#.dao=TRACE

#输出到控制台
=.
=
=.
=%d{yyyy-MM-dd HH:mm:ss,SSS} %p [%t] %C.%M(%L) | %m%n
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2)mybatics的sqlsession信息配置

package com.#.app;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
@Configuration
@ComponentScan("com.#")
@MapperScan("com.#.dao")
public class AppConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(getDataSource());
        return sqlSessionFactoryBean;
    }

    @Bean
    public DataSource getDataSource(){
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setUsername("#");
        driverManagerDataSource.setPassword("#");
        driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/#?useUnicode=true&useSSL=true");
        driverManagerDataSource.setDriverClassName("");

        return driverManagerDataSource;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

3)之后在main方法中执行

AnnotationConfigApplicationContext aa = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(aa.getBean(CityService.class).query());
  • 1
  • 2

发现还是无法打印。怎么解决呢?在前面加上

org.apache.ibatis.logging.LogFactory.useLog4JLogging();
  • 1

即:

org.apache.ibatis.logging.LogFactory.useLog4JLogging();
AnnotationConfigApplicationContext aa = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(aa.getBean(CityService.class).query());
  • 1
  • 2
  • 3

这样sql就可以打印在控制台上了。
在这里插入图片描述
但是如果不想在调用之前加呢?那我们也可以在sqlSessionFactoryBean()上进行变动,加上

//sql打印
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setLogImpl(Log4jImpl.class);
sqlSessionFactoryBean.setConfiguration(configuration);
  • 1
  • 2
  • 3
  • 4

即:

@Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //sql打印
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLogImpl(Log4jImpl.class);
        sqlSessionFactoryBean.setConfiguration(configuration);
        
        sqlSessionFactoryBean.setDataSource(getDataSource());
        return sqlSessionFactoryBean;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这样方法调用之前即使不加org.apache..useLog4JLogging();也是可以实现sql打印了,最终的效果是一样的。