代码问题七:mybatis的常用条件循环等

mybatis的if判断

1
2
3
4
5
6
<select id="queryCategoryInst" parameterType="Map" resultType="Map">
select t.* from report_category_inst t where 1=1
<if test="cust_id != null and cust_id !=''">
and cust_id = #{cust_id}
</if>
</select>

mybatis的if..else判断。

1
2
3
4
5
6
7
8
9
10
11
<select id="queryReportCategory" resultType="ReportCategory">
select a.* from report_category a where 1=1
<choose>
<when test="report_type != null and report_type != ''">
and report_type = 'M'
</when>
<otherwise>
and report_type is null
</otherwise>
</choose>
</select>

mybatis的循环遍历

1
2
3
4
5
6
7
8
9
10
<select id="queryPublicAttr" parameterType="Map" resultType="Map">
select t.* from report_subscript_attr t
where 1=1
<if test="report_subscript_ids != null">
and report_subscript_id in
<foreach collection="report_subscript_ids" item="id" index="index" open="(" separator="," close=")">
#{id}
</foreach>
</if>
</select>