java-mybaits-00402-Mapper-动态sql-if、where、foreach、sql片段
1、动态sql(重点)
1.1、If
<!-- 传递pojo综合查询用户信息 --> <select id="findUserList" parameterType="user" resultType="user"> select * from user where 1=1 <if test="id!=null and id!=''"> and id=#{id} </if> <if test="username!=null and username!=''"> and username like '%${username}%' </if> </select>
1.2、Where
<select id="findUserList" parameterType="user" resultType="user"> select * from user <where> <if test="id!=null and id!=''"> and id=#{id} </if> <if test="username!=null and username!=''"> and username like '%${username}%' </if> </where> </select>
1.3、foreach
collection:表示传入过来的参数的数据类型。该参数为必选。要做 foreach 的对象,作为入参时,List 对象默认用 list 代替作为键,数组对象有 array 代替作为键,Map 对象没有默认的键。当然在作为入参时可以使用 @Param(“keyName”) 来设置键,设置 keyName 后,list,array 将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:
如果 User 有属性 List ids。入参是 User 对象,那么这个 collection = “ids” 如果 User 有属性 Ids ids;其中 Ids 是个对象,Ids 有个属性 List id;入参是 User 对象,那么 collection = “ids.id”
如果传入的是单参数且参数类型是一个 List 的时候,collection 属性值为 list
如果传入的是单参数且参数类型是一个 array 数组的时候,collection 的属性值为 array
如果传入的参数是多个的时候,我们就需要把它们封装成一个 Map 了,当然单参数也可以封装成 map。
item: 循环体中的具体对象。支持属性的点路径访问,如 item.age,item.info.details。具体说明:在 list 和数组中是其中的对象,在 map 中是 value,该参数为必选。(它是每一个元素进行迭代时的别名)
index:在 list 和数组中,index 是元素的序号;在 map 中,index 是元素的 key。
open:表示该语句以什么开始
close:表示该语句以什么结束
separator:表示在每次进行迭代之间以什么符号作为分隔符
1.3.1、通过pojo传递list
- 需求
- 在pojo中定义list属性ids存储多个用户id,并添加getter/setter方法
![](https://images2015.cnblogs.com/blog/292888/201705/292888-20170510210028957-17305780.png)
mapper.xml
<if test="ids!=null and ids.size>0"> <foreach collection="ids" open=" and id in(" close=")" item="id" separator="," > #{id} </foreach> </if>
测试代码:
List<Integer> ids = new ArrayList<Integer>(); ids.add(1);//查询id为1的用户 ids.add(10); //查询id为10的用户 queryVo.setIds(ids); List<User> list = userMapper.findUserList(queryVo);
1.3.2、传递单个List
<select id="selectUserByList" parameterType="java.util.List" resultType="com.lhx.mybatis.po.User"> select * from user <where> <!-- 传递List,List中是pojo --> <if test="list!=null"> <foreach collection="list" item="item" open="and id in(" separator="," close=")"> #{item.id} </foreach> </if> </where> </select>
mapper接口
public List<User> selectUserByList(List<User> userlist) throws Exception;
测试类
public void testselectUserByList() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); // 构造查询条件List List<User> userlist = new ArrayList<User>(); User user = new User(); user.setId(1); userlist.add(user); user = new User(); user.setId(2); userlist.add(user); // 传递userlist列表查询用户列表 List<User> list = userMapper.selectUserByList(userlist); System.out.println(list.size()+""); // 关闭session session.close(); }
1.3.3、传递单个数组(数组中是pojo):
<!-- 传递数组综合查询用户信息 --> <select id="selectUserByArrayPojo" parameterType="Object[]" resultType="com.lhx.mybatis.po.User"> select * from user <where> <!-- 传递数组 --> <if test="array!=null"> <foreach collection="array" index="index" item="item" open="and id in(" separator="," close=")"> #{item.id} </foreach> </if> </where> </select>
mapper接口
public List<User> selectUserByArrayPojo(Object[] userlist) throws Exception;
测试代码
public void testselectUserByArray() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); // 构造查询条件List Object[] userlist = new Object[2]; User user = new User(); user.setId(1); userlist[0] = user; user = new User(); user.setId(2); userlist[1] = user; // 传递user对象查询用户列表 List<User> list = userMapper.selectUserByArrayPojo(userlist); // 关闭session session.close(); }
1.3.4、传递单个数组(数组中是字符串类型):
<!-- 传递数组综合查询用户信息 --> <select id="selectUserByArray" parameterType="Object[]" resultType="com.lhx.mybatis.po.User"> select * from user <where> <!-- 传递数组 --> <if test="array!=null"> <foreach collection="array" index="index" item="item" open="and id in(" separator="," close=")"> #{item} </foreach> </if> </where> </select>
mapper接口
public List<User> selectUserByArray(Object[] userlist) throws Exception;
测试代码
public void testselectUserByArray() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); // 构造查询条件List Object[] userlist = new Object[2]; userlist[0] = "1"; userlist[1] = "2"; // 传递user对象查询用户列表 List<User> list = userMapper.selectUserByArray(userlist); // 关闭session session.close(); }
1.4、Sql片段
<!-- 传递pojo综合查询用户信息 --> <select id="findUserList" parameterType="user" resultType="user"> select * from user <where> <if test="id!=null and id!=''"> and id=#{id} </if> <if test="username!=null and username!=''"> and username like '%${username}%' </if> </where> </select>
<sql id="query_user_where"> <if test="id!=null and id!=''"> and id=#{id} </if> <if test="username!=null and username!=''"> and username like '%${username}%' </if> </sql>
<select id="findUserList" parameterType="user" resultType="user"> select * from user <where> <include refid="query_user_where"/> </where> </select>
相关文章
- MySQL_(Java)使用preparestatement解决SQL注入的问题
- 【Mybatis异常】Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
- JAVA中如何高效的实现SQL的like语法?
- Java实现 蓝桥杯 算法训练 求和求平均值
- Java实现 LeetCode 230 二叉搜索树中第K小的元素
- Java实现第九届蓝桥杯递增三元组
- java.sql.SQLException: ORA-28001: 口令已经失效
- java.sql.SQLException: Zero date value prohibited
- oozie java api提交作业
- Atitit mybatis的扩展使用sql udf,js java等语言 目录 1.1. 默认,mybatis使用xml,sql等语言来书写业务流程1 2. 使用java扩展函数1 2.1.
- Atitit 数据join 的原理与java实现 Atitit join表连接的原理与实现 13、SQL Server 表连接的三种方式 (1) Merge Join (2) Nested
- Atitit 获取数据库表主键功能的实现 数据库模块 艾提拉attilax总结 package com.attilax.sql; import java.sql.Connection; im
- Atitit.sql ast 表达式 语法树 语法 解析原理与实现 java php c#.net js python
- Atitit.操作注册表 树形数据库 注册表的历史 java版本类库总结
- atitit.java解析sql语言解析器解释器的实现
- 《SQL必知必会》拾遗 原创 Django Java工程师成长日记
- Cause: java.sql.SQLSyntaxErrorException: Unknown column ‘***_id‘ in ‘field list‘
- Java_IO流(精讲)包含练习题及答案
- Java_画图软件
- java+sql实现mysql数据表的数据结转
- 【 华为OD机试 2023】 箱子之字形摆放(C++ Java JavaScript Python)
- Java学习路线-41:数据库及SQL/MySQL基础
- java.sql.SQLSyntaxErrorException问题常见解决方案:比如Table xxx doesn‘t exist;Unknown column ‘xxx‘ in ‘where...
- Windows和linux下分别区分mysql大小写的问题以及解决java.sql.SQLSyntaxErrorException: Table ‘xxx‘ doesn‘t exist的错误
- Java豆瓣电影爬虫——模拟登录的前世今生与验证码的爱恨情仇
- java.text.NumberFormat使用方法
- 《SQL必知必会》拾遗 原创 Django Java工程师成长日记