【記錄】spring/springboot 配置mybatis打印sql


======================springboot mybatis 打印sql==========================================

方式 一:

########################################################
###配置打印sql
########################################################
logging:
level:
com.threefivework.mymall.dao.mapper: DEBUG //包路徑為mapper文件包路徑

方式二:

在application.yml(.properties)中增加配置,在控制台打印sql:

mybatis configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 

參考類:org.apache.ibatis.session.Configuration
log-impl指定的值為org.apache.ibatis.logging.Log接口的某個實現類

 
======================spring mybatis 打印sql==========================================

網上說mybatis的早前版本配置打印sql還比較簡單,在3.0.6之后配置方式修改了。

 

現在的spring-mybatis.xml配置如下:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:conf/mybatis-config.xml"></property>
        <!-- 自動掃描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/cyber/vip/dao/*.xml"></property>
    </bean>

mybatis-config.xml:

復制代碼
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration   
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 打印查詢語句 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    
    <!-- mapper已經在spring-mybatis.xml中的sqlSessionFactory配置,這里不再需要配置 -->
<!--     <mappers> -->
<!--         <mapper resource="com/a/b/c/dao/BusinessInfoDaoMapper.xml" /> -->
<!--     </mappers> -->
</configuration>
復制代碼

 

不錯,打印SQL只需要加一個setting就可以了。

mybatis的日志打印方式比較多,SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING,可以根據自己的需要進行配置

 
settings的更多參數可以參考官網文檔: http://www.mybatis.org/mybatis-3/zh/configuration.html#settings


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM