标题发现
在整合springBoot+myBatis时,发现请求不打印sql日志,示例代码如下:
- @RestController
- public class MyController {
- @Autowired
- ProductMapper productMapper;
- @GetMapping("/test")
- public void test() {
- System.out.println("进来了");
- productMapper.list();
- System.out.println("执行完毕");
- }
- }
- @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>
复制代码 实行结果如图:
标题解决
然后使用本地main方法访问SqlSession的时间又可以打印日志
- @RunWith(SpringRunner.class)
- @SpringBootTest
- @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
- public class Test {
- public static void main(String[] args) throws IOException {
- // 创建配置文件输入流
- InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
- // 根据配置文件创建 SqlSessionFactory
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- try(SqlSession sqlSession = sqlSessionFactory.openSession();) {
- ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
- mapper.list();
- }
- }
- }
复制代码 实行结果如图
百度后发现,本地访问通过加载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,必要创建log4j. properties文件:
- #将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
- log4j.rootLogger=DEBUG,console,file
- #控制台输出的相关设置
- 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
- #文件输出的相关设置
- 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
复制代码 实行结果如图
如果你使用的是MyBatis-Plus,也必要手动开启日志,配置如下:
- mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.log4j.Log4jImpl
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |