logback是boot默认的日志首选,个人觉得是最好的日志实现(比log4j好)
下边,在之前的代码基础上增加一个功能,该功能要实现mybatis的and or联查功能,并通过logback日志在控制台去查看输出的sql情况。
即实现如下sql的功能:
SELECT * FROM tb_user WHERE id = ? AND (username=? OR `password`= ?);
该语句需要用到xml格式。
注意:由本节开始,springboot采用了1.3.0.
1、com.xxx.firstboot.web.UserController
1 @ApiOperation("测试mybatis and or联查") 2 @ApiImplicitParams({ 3 @ApiImplicitParam(paramType="query",name="id",dataType="int",required=false, value="用户的id",defaultValue="1"), 4 @ApiImplicitParam(paramType="query",name="username",dataType="String",required=false,value="用户的姓名",defaultValue="zhaojigang"), 5 @ApiImplicitParam(paramType="query",name="password",dataType="String",required=false,value="用户的密码",defaultValue="wangna") 6 }) 7 @ApiResponses({ 8 @ApiResponse(code=400,message="请求参数没填好"), 9 @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对") 10 }) 11 @RequestMapping(value="/testMybatisAndOrUnion",method=RequestMethod.GET) 12 public User getUserByIdAndUsernameOrPassword(@RequestParam(name="id",required=false) Integer id, 13 @RequestParam(name="username",required=false) String username, 14 @RequestParam(name="password",required=false) String password) { 15 return userService.getUserByIdAndUsernameOrPassword(id, username, password); 16 }
注意:
2、com.xxx.firstboot.service.UserService
1 @Service 2 public class UserService { 3 4 private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class); 5 6 @Autowired 7 private UserDao userDao; 8 9 public User getUserByIdAndUsernameOrPassword(Integer id, String username, String password){ 10 User user = userDao.getUserByIdAndUsernameOrPassword(id, username, password); 11 LOGGER.info("getUserByIdAndUsernameOrPassword success! user:‘{}‘", user); 12 return user; 13 }
说明:
3、com.xxx.firstboot.dao.UserDao
1 /** 2 * 测试 mybatis and or联查 3 */ 4 public User getUserByIdAndUsernameOrPassword(Integer id, String username, String password){ 5 return userMapper.getUserByIdAndUsernameOrPassword(id, username, password); 6 }
4、com.xxx.firstboot.mapper.UserMapper
1 public User getUserByIdAndUsernameOrPassword(@Param("id") Integer id, @Param("username") String username, @Param("password") String password);
说明:
5、src/main/resources/mapper/UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 4 <!-- 指定工作空间,要与接口名相同,源代码没有去看,猜测应该是通过"这里的namespace.下边方法的id"来定位方法的 --> 5 <mapper namespace="com.xxx.firstboot.mapper.UserMapper"> 6 <resultMap type="User" id="userResultMap"> 7 <id property="id" column="id" /> 8 <result property="username" column="username" /> 9 <result property="password" column="password" /> 10 </resultMap> 11 12 <!-- SELECT * FROM tb_user WHERE id = 1 AND (username=‘‘ OR `password`=‘wangna‘); --> 13 <select id="getUserByIdAndUsernameOrPassword" resultMap="userResultMap"> 14 <![CDATA[ SELECT * FROM tb_user WHERE id = #{id}]]> 15 <trim prefix=" AND (" suffix=")" prefixOverrides="OR"> 16 <if test="username != null"><![CDATA[ username = #{username}]]></if> 17 <if test="password != null"><![CDATA[ OR password = #{password}]]></if> 18 </trim> 19 </select> 20 21 </mapper>
注意:trim的作用
6、src/main/resources/logback.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <include resource="org/springframework/boot/logging/logback/base.xml" /> 4 <!-- 1、logger 5 属性: 6 1)name:用来指定受此logger约束的某一个包或者具体的某一个类 7 2)level:用来设置打印级别,大小写无关(最常用的几种):DEBUG, INFO, WARN, ERROR 8 2、 9 --> 10 <!-- <logger name="org.springframework.web" level="DEBUG" /> --> 11 12 <!-- mybatis日志打印 --> 13 <!-- <logger name="com.ibatis" level="DEBUG" /> --> 14 <!-- <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="DEBUG" /> --> 15 <!-- <logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG" /> --> 16 <!-- <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" /> --> 17 18 <!-- <logger name="java.sql.Connection" level="DEBUG" /> --> 19 <!-- <logger name="java.sql.Statement" level="DEBUG" /> --> 20 <!-- <logger name="java.sql.PreparedStatement" level="DEBUG" /> --> 21 <!-- 这一句至关重要如果没有,就无法输出sql语句 --> 22 <logger name="com.xxx.firstboot.mapper" level="DEBUG"></logger> 23 </configuration>
注意:在boot中,想在控制台打印mybatis的sql语句,只需要配置下边这一句就好了。
<logger name="com.xxx.firstboot.mapper" level="DEBUG"></logger>
即将mapper接口打入logger就行。
但是在ssm中,可能就需要该行上边的7行了。(这个没有测过不清楚)
测试:
启动应用,打开swagger。
打印出的sql语句:SELECT * FROM tb_user WHERE id = ?
打印出的sql语句:SELECT * FROM tb_user WHERE id = ? AND ( username = ? )
打印出的sql语句:SELECT * FROM tb_user WHERE id = ? AND ( password = ? )
打印出的sql语句:SELECT * FROM tb_user WHERE id = ? AND ( username = ? OR password = ? )
根据测试可以发现,id是必须的,这也是我的项目的需求(而这里不传id,只是为了测试而已),如果id也不是必须,就要考虑trim的位置以及prefixOverrides的值(可以使用AND|OR这样的表示或)
参考自:
http://stackoverflow.com/questions/21718097/how-to-configure-logback-for-mybatis-to-print-my-sql
http://aub.iteye.com/blog/1896611 关于logback可以参考的博客
原文:http://www.cnblogs.com/java-zhao/p/5449152.html