mybatis学习笔记之动态sql
程序员文章站
2022-05-02 23:39:29
mybatis学习笔记(8)-动态sql
标签: mybatis
mybatis核心,对sql语句进行灵活操作,通过表达式进行判断,对s...
mybatis学习笔记(8)-动态sql
标签: mybatis
mybatis核心,对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。
if判断
mapper.xmlSELECT * FROM user AND user.sex=#{userCustom.sex} AND user.username LIKE '%${userCustom.username}%' SELECT count(*) FROM user AND user.sex=#{userCustom.sex} AND user.username LIKE '%${userCustom.username}%'
1.注释掉testFindUserList()
方法中的userCustom.setUsername("张三");
//由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中 userCustom.setSex("1"); //userCustom.setUsername("张三"); userQueryVo.setUserCustom(userCustom);
输出
DEBUG [main] - Checking to see if class com.iot.mybatis.mapper.UserMapper matches criteria [is assignable to Object] DEBUG [main] - Checking to see if class com.iot.mybatis.mapper.UserMapperTest matches criteria [is assignable to Object] DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 352359770. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1500955a] DEBUG [main] - ==> Preparing: SELECT * FROM user WHERE user.sex=? DEBUG [main] - ==> Parameters: 1(String) DEBUG [main] - <== Total: 6 [User [id=10, username=张三, sex=1, birthday=Thu Jul 10 00:00:00 CST 2014, address=北京市], User [id=16, username=张小明, sex=1, birthday=null, address=河南郑州], User [id=22, username=陈小明, sex=1, birthday=null, address=河南郑州], User [id=24, username=张三丰, sex=1, birthday=null, address=河南郑州], User [id=25, username=陈小明, sex=1, birthday=null, address=河南郑州], User [id=28, username=王小军, sex=1, birthday=Tue Feb 23 00:00:00 CST 2016, address=河南郑州]]
可以看到sql语句为reparing: SELECT * FROM user WHERE user.sex=?
,没有username的部分
2.userQueryVo
设为null,则userCustom
为null
//List list = userMapper.findUserList(userQueryVo); List list = userMapper.findUserList(null);
输出
DEBUG [main] - ==> Preparing: SELECT * FROM user DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 9 [User [id=1, username=王五, sex=2, birthday=null, address=null], User [id=10, username=张三, sex=1, birthday=Thu Jul 10 00:00:00 CST 2014, address=北京市], User [id=16, username=张小明, sex=1, birthday=null, address=河南郑州], User [id=22, username=陈小明, sex=1, birthday=null, address=河南郑州], User [id=24, username=张三丰, sex=1, birthday=null, address=河南郑州], User [id=25, username=陈小明, sex=1, birthday=null, address=河南郑州], User [id=26, username=王五, sex=null, birthday=null, address=null], User [id=27, username=王大军, sex=2, birthday=Tue Feb 23 00:00:00 CST 2016, address=河南郑州], User [id=28, username=王小军, sex=1, birthday=Tue Feb 23 00:00:00 CST 2016, address=河南郑州]]
可以看到sql语句变为了SELECT * FROM user
sql片段(重点)
将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。
定义sql片段AND user.sex = #{userCustom.sex} AND user.username LIKE '%${userCustom.username}%'引用sql片段
SELECT * FROM user
foreach标签
向sql传递数组或List,mybatis使用foreach解析
在用户查询列表和查询总数的statement中增加多个id输入查询。两种方法,sql语句如下:
SELECT * FROM USER WHERE id=1 OR id=10 OR id=16
SELECT * FROM USER WHERE id IN(1,10,16)
一个使用OR,一个使用IN
在输入参数类型中添加List ids
public class UserQueryVo { //传入多个id private List ids; getter、setter方法 。。。 }
id=#{user_id} 每个遍历需要拼接的串 #{user_id} -->测试代码
在testFindUserList
中加入
//传入多个id List ids = new ArrayList(); ids.add(1); ids.add(10); ids.add(16); //将ids通过userQueryVo传入statement中 userQueryVo.setIds(ids);
上一篇: C/C++[codeup 202六] - 日期问题
下一篇: sshpass的使用方法