通用Mapper如何输出执行的SQL语句?
来源:3-15 编写用户微服务与内容微服务-1

夏J
2019-07-02
不知道为什么我用这个方法是查不到数据的想看一下输出的执行的sql也没法看,所以老师帮我找一下有关打印通用
mapper打印日志的方法,麻烦老师了
this.userMapper.selectByPrimaryKey(id)
用这个方法倒可以
@Autowired(required = false)
private UserMapper userMapper;
public List<User> findById(Integer id) {
User user = new User();
user.setId(id);
// this.userMapper.selectByPrimaryKey(id)
return this.userMapper.select(user);
}
写回答
2回答
-
您好,很棒的问题哦!这其实是一个Mybatis基础问题。
要想看到SQL非常简单,只需要在application.yml添加如下内容即可:
logging: level: # 这样,这个包下的所有mapper都会打印出执行的SQL com.itmuch.usercenter.dao: debug
打印出来的日志如下:
2019-07-02 16:51:05.655 DEBUG 97433 --- [nio-8080-exec-4] c.i.u.d.u.UserMapper.selectByPrimaryKey : ==> Preparing: SELECT id,wx_id,wx_nickname,roles,avatar_url,create_time,update_time,bonus FROM user WHERE id = ? 2019-07-02 16:51:05.656 DEBUG 97433 --- [nio-8080-exec-4] c.i.u.d.u.UserMapper.selectByPrimaryKey : ==> Parameters: 1(Integer) 2019-07-02 16:51:05.658 DEBUG 97433 --- [nio-8080-exec-4] c.i.u.d.u.UserMapper.selectByPrimaryKey : <== Total: 1
022019-07-02 -
大目
2019-07-02
对了,这个问题还可以拓展一下。对于IDEA,可以安装Mybatis Log Plugin,让日志的显示更加的人性化,从而提升调试、排错的效率。
截图演示:
最后,附上个人总结的所有IDEA的学习资源,详见:
https://github.com/eacdy/awesome-idea#114-mybatis-log-plugin
022019-07-02
相似问题