1. 程式人生 > >spring mybatis設定SQL語句列印

spring mybatis設定SQL語句列印

在使用mybatis進行開發的時候,由於可以動態拼接sql,這樣大大方便了我們。但是也有一定的問題,當我們動態sql拼接的塊很多的時候,我們要想從*mapper.xml中直接找出完整的sql就會非常的難,這個時候經常會需要把組合之後的完整sql調試出來比較好。下面來看兩種調試出sql的兩種方式

解決方案
方案1: 
網上說的比較多的,之前也是這麼用的一種方式 
1:首先將ibatis log4j執行級別調到DEBUG可以在控制檯打印出ibatis執行的sql語句 
2:新增如下語句

###顯示SQL語句部分
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.Java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG


方案2: 

如果是application.properties

logging.level.com.neo.mapper=debug

若果是application.yml

logging:
  level:
    com.test.mapper: DEBUG

com.test.mapper是 mybatis介面及對映檔案包。

application.properties 改成 application.yml

yml檔案的好處,天然的樹狀結構,一目瞭然

方案3: 

application.properties

server.port=8090

server.session-timeout=30

server.context-path=

server.tomcat.max-threads=0

server.tomcat.uri-encoding=UTF-8


spring.datasource.url = jdbc:mysql://localhost:3306/newbirds

spring.datasource.username = root

spring.datasource.password = mymysql

spring.datasource.driverClassName = com.mysql.jdbc.Driver

# Specify the DBMS

spring.jpa.database = MYSQL

# Show or not log for each sql query

spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)

spring.jpa.hibernate.ddl-auto = update

# Naming strategy

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy


# stripped before adding them to the entity manager)

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

application.yml


server:

port: 8090

session-timeout: 30

tomcat.max-threads: 0

tomcat.uri-encoding: UTF-8


spring:

datasource:

url : jdbc:mysql://localhost:3306/newbirds

username : root

password : mymysql

driverClassName : com.mysql.jdbc.Driver

jpa:

database : MYSQL

show-sql : true

hibernate:

ddl-auto : update

naming-strategy : org.hibernate.cfg.ImprovedNamingStrategy

properties:

hibernate:

dialect : org.hibernate.dialect.MySQL5Dialect

注意點:

1,原有的key,例如spring.jpa.properties.hibernate.dialect,按“.”分割,都變成樹狀的配置

2,key後面的冒號,後面一定要跟一個空格

3,把原有的application.properties刪掉。然後一定要執行一下  maven -X clean install

方案4: 

springboot對logback的支援是非常好的,不需要任何配置,只需要在resource下加logback.xml就可以實現功能
直接貼程式碼:

<?xml version="1.0"?>

<configuration>

    <property name="logPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS}  %-5level [%thread][%c.%M:%L]- %msg%n"/>
    <property name="logHome" value="${log_path}/log"/>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logHome}/file.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logHome}/file.log.%d.%i.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>15 MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>


    <appender name="baselog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logHome}/base.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logHome}/base.log.%d.%i.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>15 MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="springlog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logHome}/spring.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logHome}/spring.log.%d.%i.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20 MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="sqllog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logHome}/sqllog.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logHome}/sql.log.%d.%i.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20 MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="convertErrorlog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logHome}/convertErrorlog.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logHome}/convertErrorlog.log.%d.%i.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20 MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <appender name="cachelog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logHome}/cachelog.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logHome}/cachelog.log.%d.%i.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20 MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="businessLogger" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logHome}/business.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logHome}/business.log.%d.%i.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20 MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <MaxHistory>5</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="dataLogger" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logHome}/data.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logHome}/data.log.%d.%i.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20 MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <MaxHistory>5</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <root level="WARN">
        <appender-ref ref="FILE"/>
    </root>
    <logger name="com.test" level="WARN" additivity="false">
        <appender-ref ref="baselog"/>
    </logger>
    <logger name="com.smile" level="DEBUG" additivity="false">
        <appender-ref ref="sqllog"/>
    </logger>
    <logger name="java.sql" level="WARN" additivity="false">
        <appender-ref ref="sqllog"/>
    </logger>
    <logger name="org.springframework" level="WARN" additivity="false">
        <appender-ref ref="springlog"/>
    </logger>
    <logger name="o.springframework" level="WARN" additivity="false">
        <appender-ref ref="springlog"/>
    </logger>

    <logger name="ERROR_LOGGER" level="WARN" additivity="false">
        <appender-ref ref="convertErrorlog"/>
    </logger>

    <logger name="CACHE_LOGGER" level="WARN" additivity="false">
        <appender-ref ref="cachelog"/>
    </logger>

    <logger name="business_logger" level="WARN" additivity="false">
        <appender-ref ref="businessLogger"/>
    </logger>

    <logger name="data_logger" level="WARN" additivity="false">
        <appender-ref ref="dataLogger"/>
    </logger>

</configuration>

		
<!-- name包必須能夠掃描到所以類,包括啟動類 -->
    <logger name="com.smile" level="debug" additivity="false">
        <appender-ref ref="smile" />
    </logger>


其中值得注意的是:你配置檔案中的包名要能夠掃描到你的所有類,包括啟動類。
否則,你的日誌檔案是生成了,但是不會向裡面寫入內容
啟動程式列印日誌,日誌成功生成:

方案5: 

spring mvc mybatis配置檔案:

<?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="cacheEnabled" value="true" />
        <!-- 查詢時,關閉關聯物件及時載入以提高效能 -->
        <setting name="lazyLoadingEnabled" value="false" />
        <!-- 設定關聯物件載入的形態,此處為按需載入欄位(載入欄位由SQL指定),不會載入關聯表的所有欄位,以提高效能 -->
        <setting name="aggressiveLazyLoading" value="false" />
        <!-- 對於位置的SQL查詢,允許返回不同的結果集以達到通用的效果 -->
        <setting name="multipleResultSetsEnabled" value="true" />
        <!-- 允許使用列標籤代替列明 -->
        <setting name="useColumnLabel" value="true" />
        <!-- 允許使用自定義的主鍵值(比如由程式生成的UUID 32位編碼作為鍵值), 資料表的pk生成策略將被覆蓋 -->
        <setting name="useGeneratedKeys" value="true" />
        <!-- 給予被巢狀的resultMap以欄位-屬性的對映支援 -->
        <setting name="autoMappingBehavior" value="PARTIAL" />
        <!-- 對於批量更新操作快取SQL以提高效能 -->
        <setting name="defaultExecutorType" value="REUSE" />
        <!-- 資料庫超過25000秒仍未響應則超時 -->
        <setting name="defaultStatementTimeout" value="25000" />
        <!-- 列印查詢語句 -->
        <!-- <setting name="logImpl" value="STDOUT_LOGGING" /> -->
    </settings>
</configuration>

 

<setting name="logImpl" value="STDOUT_LOGGING" />

如果將它註釋,你的sql就可以列印進日誌

如果將它放開,你的sql就列印到了控制檯