方法1:
mybatis plus 里面QueryWrapper输出条件语句,会有很多个方法,它们各自的作用有哪些:
LambdaQueryWrapper<UserInfo> temp = new LambdaQueryWrapper<>();
temp.eq(UserInfo::getUserRole,1);
temp.like(UserInfo::getUserNick,"测试");
temp.eq(UserInfo::getAge,18);
//打印它的输出语句
log.info("测试的1:"+temp.getCustomSqlSegment());
log.info("测试的2:"+temp.getExpression());
log.info("测试的3:"+temp.getSqlSegment());
log.info("测试的4:"+temp.getSqlSet());
log.info("测试的5:"+temp.getParamAlias());
log.info("测试的6:"+temp.getSqlSelect());
log.info("测试的7:"+temp.getEntity());
log.info("测试的8:"+temp.getParamNameValuePairs());
log.info("测试的9:"+temp.getClass());
List<UserInfo> userInfoList22 = userInfoMapper.selectList(temp);
: Completed initialization in 1 ms
: 测试的1:WHERE userRole = #{ew.paramNameValuePairs.MPGENVAL1} AND userNick LIKE #{ew.paramNameValuePairs.MPGENVAL2} AND age = #{ew.paramNameValuePairs.MPGENVAL3}
: 测试的2:com.baomidou.mybatisplus.core.conditions.segments.MergeSegments@1aa99b38
: 测试的3:userRole = #{ew.paramNameValuePairs.MPGENVAL1} AND userNick LIKE #{ew.paramNameValuePairs.MPGENVAL2} AND age = #{ew.paramNameValuePairs.MPGENVAL3}
: 测试的4:null
: 测试的5:ew
: 测试的6:null
: 测试的7:null
: 测试的8:{MPGENVAL3=18, MPGENVAL2=%测试%, MPGENVAL1=1}
: 测试的9:class com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
可以看到输出结果,一般可以选择temp.getCustomSqlSegment() 和temp.getParamNameValuePairs() 打印出查询条件和参数
方法2:
在项目配置文件application.yml 中增加配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #mybatis-plus配置控制台打印完整带参数SQL语句
map-underscore-to-camel-case: false # 设置字段不使用下划线方式
效果展示:
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@13d38ace] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1354439565 wrapping com.mysql.jdbc.JDBC4Connection@3dc43376] will not be managed by Spring
==> Preparing: SELECT userId,userNick,address,mobile,userName,openId,unionId,password,age,userRole,isDelete,createTime,updateTime FROM userInfo WHERE userRole = ? AND userNick LIKE ? AND age = ?
==> Parameters: 1(Integer), %测试%(String), 18(Integer)
<== Total: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@13d38ace]
INFO 58464 --- [nio-8082-exec-2] c.c.w.u.s.impl.UserInfoServiceImpl : wrapper.toString():{}
INFO 58464 --- [nio-8082-exec-2] c.c.w.u.s.impl.UserInfoServiceImpl : wrapper.getSqlSelect():null
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ee75f18] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@140977064 wrapping com.mysql.jdbc.JDBC4Connection@3dc43376] will not be managed by Spring
==> Preparing: SELECT userId,userNick,address,mobile,userName,openId,unionId,password,age,userRole,isDelete,createTime,updateTime FROM userInfo WHERE (mobile REGEXP '^1') AND userRole = ?
==> Parameters: 1(Integer)
<== Columns: userId, userNick, address, mobile, userName, openId, unionId, password, age, userRole, isDelete, createTime, updateTime
<== Row: 1, 张三测试, 北京市朝阳区, 15566668888, 2, o11, u1, null, 0, 1, 0, 2022-06-06 16:01:00.0, 2022-06-06 16:01:00.0
<== Row: 2, 李四测试, 北京市朝阳区, 15566668888, 2, o12, u2, null, 0, 1, 0, 2022-06-06 16:02:44.0, 2022-06-06 16:02:44.0
<== Row: 3, 王五测试, 北京北京, 15566669999, 2, o12, u3, null, 0, 1, 0, 2022-06-06 16:08:08.0, 2022-06-06 16:08:08.0
<== Row: 4, 王六测试, 北京北京, 15566669999, 3, o14, u4, null, 0, 1, 0, 2022-06-06 16:17:33.0, 2022-06-06 16:17:33.0
<== Row: 5, 王七测试, 北京北京, 15566669999, 3, o15, u5, null, 0, 1, 0, 2022-06-06 16:23:05.0, 2022-06-06 16:23:05.0
<== Row: 6, 王七测试, 北京北京, 15566669999, 5, 016, u6, null, 0, 1, 0, 2022-06-06 16:24:02.0, 2022-06-06 16:24:02.0
<== Row: 8, 王八测试, 北京北京, 15566669999, 5, o18, u8, null, 0, 1, 0, 2022-06-06 16:34:49.0, 2022-06-06 16:34:49.0
<== Row: 10, 老九八测试, 北京海淀, 15566669999, 6, o20, u10, null, 0, 1, 0, 2022-06-06 17:03:20.0, 2022-06-06 17:03:20.0
<== Row: 14, 第11, 北京海淀, 15566669999, 1, null, null, null, 0, 1, 0, 2022-06-06 17:38:16.0, 2022-06-06 17:38:16.0
<== Total: 9
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ee75f18]