本文適用於SpringBoot2.5.4,Mybatis2.2.0,mybatis-plus3.0.5版本,不保證在其它版本的適用性。
與本文相關的姊妹篇:https://www.cnblogs.com/heyang78/p/15369564.html
本文例程:https://files.cnblogs.com/files/heyang78/redisCache_mybatisplus_printsql_211006.rar
使用LambdaQueryWrapper后,簡單查詢是方便了,但有時需要把SQL顯示出來,要做到這一步在yml文件里加入相應配置就好了。
server: port: 8080 myenv: name: '開發環境' spring: datasource: url: jdbc:oracle:thin:@127.0.0.1:1521:orclhy78 username: luna password: 1234 driver-class-name: oracle.jdbc.OracleDriver #mybatis-plus配置控制台打印完整帶參數SQL語句 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
有了這個設置后,控制台就有sql輸出了:
有請求來到,其路徑=/searchEmpsByAge Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@e73a277] was not registered for synchronization because synchronization is not active JDBC Connection [HikariProxyConnection@1331295536 wrapping oracle.jdbc.driver.T4CConnection@ab2e6d2] will not be managed by Spring ==> Preparing: SELECT id,name,age FROM emp WHERE age = ? ==> Parameters: 42(Integer) <== Columns: ID, NAME, AGE <== Row: 3, 李逵, 42 <== Row: 1, 2222, 42 <== Row: 2, 3333, 42 <== Total: 3 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@e73a277] 2021-10-06 16:58:16.750 WARN 24752 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=2h32s442ms568µs700ns).
參考文章:
