SpringBoot整合MyBatis和MyBatis-Plus請求后不打印sql日志的問題解決
問題發(fā)現(xiàn)
在整合springBoot+myBatis時,發(fā)現(xiàn)請求不打印sql日志,示例代碼如下:
@RestController public class MyController { @Autowired ProductMapper productMapper; @GetMapping("/test") public void test() { System.out.println("進來了"); productMapper.list(); System.out.println("執(zhí)行完畢"); } } @Mapper public interface ProductMapper { List<Product> list(); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.ProductMapper"> <resultMap id="BaseResultMap" type="com.example.demo.domain.Product"> <id property="id" column="id" jdbcType="INTEGER"/> <result property="productName" column="product_name" jdbcType="VARCHAR"/> <result property="number" column="number" jdbcType="INTEGER"/> </resultMap> <sql id="Base_Column_List"> id,product_name,number </sql> <select id="list" resultMap="BaseResultMap"> select * from product </select> </mapper>
執(zhí)行結(jié)果如圖:
問題解決
然后使用本地main
方法訪問SqlSession的時候又可以打印日志
@RunWith(SpringRunner.class) @SpringBootTest @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) public class Test { public static void main(String[] args) throws IOException { // 創(chuàng)建配置文件輸入流 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); // 根據(jù)配置文件創(chuàng)建 SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try(SqlSession sqlSession = sqlSessionFactory.openSession();) { ProductMapper mapper = sqlSession.getMapper(ProductMapper.class); mapper.list(); } } }
執(zhí)行結(jié)果如圖
百度后發(fā)現(xiàn),本地訪問通過加載mybatis-config.xml
,會默認打印日志。
springBoot需要手動開啟日志才行,具體配置如下:
#默認使用 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis.configuration.log-impl=org.apache.ibatis.logging.log4j.Log4jImpl
如果你使用log4j,需要創(chuàng)建log4j. properties
文件:
#將等級為DEBUG的日志信息輸出到console和file這兩個目的地,console和file的定義在下面的代碼 log4j.rootLogger=DEBUG,console,file #控制臺輸出的相關(guān)設(shè)置 log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout #定義日志輸出的格式模式 log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n #文件輸出的相關(guān)設(shè)置 log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/main.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=DEBUG log4j.appender.file.layout=org.apache.log4j.PatternLayout #定義日志輸出的格式模式 log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n #日志輸出級別 log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
執(zhí)行結(jié)果如圖
如果你使用的是MyBatis-Plus,也需要手動開啟日志,配置如下:
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.log4j.Log4jImpl
到此這篇關(guān)于SpringBoot整合MyBatis和MyBatis-Plus請求后不打印sql日志的問題解決的文章就介紹到這了,更多相關(guān)SpringBoot不打印sql日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot下mybatis-plus如何打印sql日志和參數(shù)到日志文件
- springboot下mybatis-plus開啟打印sql日志的配置指南
- mybatis-plus開啟sql日志打印的三種方法
- Mybatis-Plus打印sql日志兩種方式
- MyBatis-Plus如何關(guān)閉SQL日志打印詳解
- mybatis-plus如何修改日志只打印SQL語句不打印查詢結(jié)果
- MyBatis-Plus使用sl4j日志打印SQL的代碼詳解
- 服務(wù)性能優(yōu)化之mybatis-plus開啟與關(guān)閉SQL日志打印方法
- Mybatis-Plus通過配置在控制臺打印執(zhí)行日志的實現(xiàn)
相關(guān)文章
SparkSQL讀取hive數(shù)據(jù)本地idea運行的方法詳解
這篇文章主要介紹了SparkSQL讀取hive數(shù)據(jù)本地idea運行的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Java?swing實現(xiàn)應(yīng)用程序?qū)?shù)據(jù)庫的訪問問題
這篇文章主要介紹了Java?swing實現(xiàn)應(yīng)用程序?qū)?shù)據(jù)庫的訪問,本次實驗需要做一個GUI界面和一個連接查詢功能,在論壇上借鑒了其他大佬獲取網(wǎng)站內(nèi)容的部分代碼,然后自己做了一個及其簡陋的swing界面,算是把這個實驗完成了,需要的朋友可以參考下2022-09-09SpringBoot實現(xiàn)Read Through模式的操作過程
Read Through模式通常是指一種緩存策略,其中當應(yīng)用程序嘗試讀取數(shù)據(jù)時,緩存系統(tǒng)首先被檢查以查看數(shù)據(jù)是否已經(jīng)存在于緩存中,這篇文章主要介紹了SpringBoot實現(xiàn)Read Through模式,需要的朋友可以參考下2024-07-07SpringBoot返回Json對象報錯(返回對象為空{(diào)})
本文主要介紹介紹了SpringBoot返回Json對象報錯(返回對象為空{(diào)}),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01IDEA 單元測試創(chuàng)建方法詳解(2020.03版本親測)
這篇文章主要介紹了IDEA 單元測試創(chuàng)建方法詳解(2020.03版本親測),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10詳解全局事務(wù)注解@GlobalTransactional的識別
這篇文章主要為大家介紹了詳解全局事務(wù)注解@GlobalTransactional的識別源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12jasypt SaltGenerator接口定義方法源碼解讀
這篇文章主要為大家介紹了jasypt SaltGenerator接口定義方法源碼解讀,,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09