SpringBoot整合MyBatis和MyBatis-Plus請(qǐng)求后不打印sql日志的問題解決
問題發(fā)現(xiàn)
在整合springBoot+myBatis時(shí),發(fā)現(xiàn)請(qǐng)求不打印sql日志,示例代碼如下:
@RestController
public class MyController {
@Autowired
ProductMapper productMapper;
@GetMapping("/test")
public void test() {
System.out.println("進(jìn)來了");
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的時(shí)候又可以打印日志
@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,會(huì)默認(rèn)打印日志。
springBoot需要手動(dòng)開啟日志才行,具體配置如下:
#默認(rèn)使用 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis.configuration.log-impl=org.apache.ibatis.logging.log4j.Log4jImpl
如果你使用log4j,需要?jiǎng)?chuàng)建log4j. properties文件:
#將等級(jí)為DEBUG的日志信息輸出到console和file這兩個(gè)目的地,console和file的定義在下面的代碼
log4j.rootLogger=DEBUG,console,file
#控制臺(tái)輸出的相關(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
#日志輸出級(jí)別
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,也需要手動(dòng)開啟日志,配置如下:
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.log4j.Log4jImpl
到此這篇關(guān)于SpringBoot整合MyBatis和MyBatis-Plus請(qǐng)求后不打印sql日志的問題解決的文章就介紹到這了,更多相關(guān)SpringBoot不打印sql日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(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通過配置在控制臺(tái)打印執(zhí)行日志的實(shí)現(xiàn)
相關(guān)文章
java使用list實(shí)現(xiàn)數(shù)據(jù)庫的like功能
這篇文章主要介紹了java使用list實(shí)現(xiàn)數(shù)據(jù)庫的like功能,需要的朋友可以參考下2014-04-04
SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼
在實(shí)際開發(fā)中,肯定會(huì)經(jīng)常遇到對(duì)參數(shù)字段進(jìn)行校驗(yàn)的場(chǎng)景,通常我們只能寫大量的if else來完成校驗(yàn)工作,而如果使用SpringBoot Validation則可以輕松的通過注解來完成,接下來小編給大家介紹下利用SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼,需要的朋友參考下吧2022-06-06
SpringBoot2.x設(shè)置Session失效時(shí)間及失效跳轉(zhuǎn)方式
這篇文章主要介紹了SpringBoot2.x設(shè)置Session失效時(shí)間及失效跳轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Spring Boot 核心模塊詳解(12 個(gè)模塊詳解及作用說明)
和 Spring 框架一樣,Spring Boot 框架也是由許多核心模塊組成的,每個(gè)模塊負(fù)責(zé)不同的功能點(diǎn),本文講著重于介紹 Spring Boot 相關(guān)的 12 個(gè)模塊的作用和功能,感興趣的朋友一起看看吧2025-04-04
IntellJ idea使用FileWatch實(shí)時(shí)編譯less文件的方法
這篇文章主要介紹了IntellJ idea使用FileWatch實(shí)時(shí)編譯less文件的相關(guān)資料,需要的朋友可以參考下2018-02-02
微服務(wù)架構(gòu)之使用RabbitMQ進(jìn)行異步處理方式
本文介紹了RabbitMQ的基本概念、異步調(diào)用處理邏輯、RabbitMQ的基本使用方法以及在Spring Boot項(xiàng)目中使用RabbitMQ解決高并發(fā)問題,RabbitMQ是一種流行的消息隊(duì)列實(shí)現(xiàn),支持異步通信,可以有效解耦應(yīng)用程序的不同部分,并將任務(wù)分發(fā)給多個(gè)消費(fèi)者2025-02-02
mybatis的insert插入后獲取自增id的方法詳解(從controller到mapper)
這篇文章主要介紹了mybatis的insert插入后獲取自增id的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10
使用EasyPoi輕松導(dǎo)入導(dǎo)出Excel文檔的方法示例
這篇文章主要介紹了使用EasyPoi輕松導(dǎo)入導(dǎo)出Excel文檔的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java解析http協(xié)議字符串的方法實(shí)現(xiàn)
本文主要介紹了Java解析http協(xié)議字符串的方法實(shí)現(xiàn),我們探討了如何使用Java解析HTTP協(xié)議字符串,并將其封裝成了一個(gè)HttpRequest類,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09

