foreach标签
foreach属性主要有item,index,collection,open,separator,close。
1、item表示集合中每一个元素进行迭代时的别名,
2、index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
3、open表示该语句以什么开始,
4、separator表示在每次进行迭代之间以什么符号作为分隔符,
5、close表示以什么结束,
6、collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样:
a、如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
b、如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
c、如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
具体讲解如下
1. findByIds(List ids)
如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
<select id="findByIdsMap" resultMap="BaseResultMap"> Select <include refid="Base_Column_List" /> from jria where ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
2:findByIds(Long[] ids)
如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
<select id="findByIdsMap" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from tabs where ID in <foreach item="item" index="index" collection="array" open="(" separator="," close=")"> #{item} </foreach> </select>
3. findBy
当查询的参数有多个时:
这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称
<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>
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; }
static List<String> statsList(){ List<String> statusList = new ArrayList<String>(); statusList.add("SUCCESS"); statusList.add("DUE"); statusList.add("OVER"); return statusList; }
java.lang.IllegalArgumentException: invalid comparison: java.util.ArrayList and java.lang.String
分析原因发现是对test的判断存在错误。
错误代码如下:
<if test="bankIds != null and bankIds != ''"> and bank_id in <foreach item="item" index="index" collection="bankIds" open="(" separator="," close=")"> #{item} </foreach> </if>
正确代码如下:
<if test="bankIds != null and bankIds.size > 0"> and bank_id in <foreach item="item" index="index" collection="bankIds" open="(" separator="," close=")"> #{item} </foreach> </if>
MyBatis 占位符#和 $ 的区别
#符号将传入的数据都当做一个字符串,会对自动传入的数据加一个双引号;
$ 符号将传入的数据直接显示生成 SQL 中;
#符号存在预编译的过程对问号赋值,防止 SQL 注入;
$ 符号是直译的方式,一般用在 order by ${列名}语句中;
能用#号就不要用 $ 符号,$符号主要用于order by