mybatis查询sql中in条件使用(foreach)
2017-11-29 08:41
477 查看
foreach属性主要有item,index,collection,open,separator,close。
1、item表示集合中每一个元素进行迭代时的别名,
2、index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
3、open表示该语句以什么开始,
4、separator表示在每次进行迭代之间以什么符号作为分隔符,
5、close表示以什么结束,
6、collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
a、如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
b、如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
c、如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
java代码
1、item表示集合中每一个元素进行迭代时的别名,
2、index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
3、open表示该语句以什么开始,
4、separator表示在每次进行迭代之间以什么符号作为分隔符,
5、close表示以什么结束,
6、collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
a、如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
b、如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
c、如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
<select id="findBy" resultMap="RfCustomerMemMap" parameterType="java.util.Map"> SELECT <include refid="Column"/> FROM rfl_customer_mem a LEFT JOIN rfl_loan b ON a.member_no = b.loan_member_no WHERE a.member_no = #{memberNo} AND b.status IN <foreach collection="status" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> <if test="name != null and name != ''"> AND name = #{name} </if> <if test="idNumber != null and idNumber != ''"> AND id_number = #{idNumber} </if> <if test="mobileNo != null and mobileNo != ''"> AND mobile_no = #{mobileNo} </if> <if test="loanNo != null and loanNo != ''"> AND loan_no = #{loanNo} </if> order by a.id DESC <if test="offset > -1 and rows > -1"> limit #{offset},#{limit} </if> </select>
java代码
public List<LoanMerchantMemEntity> findMerchantMemBy(String merchantName, String merchantNo, String socialCreditCode, String loanNo, int offset, int limit) { List<LoanMerchantMemEntity> list = new ArrayList<LoanMerchantMemEntity>(); Map<String, Object> filter = new HashMap<String, Object>(); filter.put("merchantName", merchantName); filter.put("socialCreditCode", socialCreditCode); filter.put("status", statsList()); filter.put("loanNo", loanNo); filter.put("offset", offset); filter.put("limit", limit); filter.put("merchantNo", merchantNo); try { List<LoanMerchantMemEntity> row = loanMerchantMemDao.findBy(filter); } catch (Exception e) { LOGGER.error(filter, "查询企业会员信息异常", e); } return list; }
相关文章推荐
- mybatis查询sql中in条件使用(foreach)
- mybatis使用foreach实现sql的in查询
- mybatis使用in语句作为查询条件
- SQL使用in查询按条件顺序返回
- Mybatis中的SQL语句中的IN子句使用传入List作为包含条件
- Mybatis 多条件查询(模糊查询 使用in查询)
- mybatis sql in 查询(mybatis sql语句传入参数是list)mybatis中使用in查询时in怎么接收值
- MyBatis中的动态SQL,实现不确定条件的CRUD-----使用Map来传递查询的参数
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- MyBatis中forEach实现in条件下的集合参数SQL语句
- Mybatis 中sql 使用in(#{item})无法查询解决办法
- Mybatis 多条件查询(模糊查询 使用in查询)
- Mybatis查询实例,sql中的in在Mybatis中怎么写--以list为查询条件
- mybatis 中 查询sql使用in
- mybatis 的 In 查询,子查询 ,集合查询foreach 使用
- mybatis 查询多个条件IN的使用方法
- 如何在mybatis SQL查询中使用IN
- mybatis使用foreach语句实现IN查询
- mybatis in条件查询(foreach)
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma