if
<!-- resultType为HashMap -->
<!-- 别名作为map的key -->
<!-- 动态SQL -->
<!-- 在where后面加1=1或者使用<where>标签解决and问题,<where>标签会自动处理第一个and -->
<select id="queryAllPersonByNameOrAgeWithSQLTag" parameterType="Person" resultType="HashMap" >
select id "id",name "name" from Person
<where>
<if test="name!=null and name!='' ">
and name=#{name}
</if>
<if test="age!=null and age!=0 ">
and age=#{age}
</if>
</where>
</select>
for
<select id="queryAllPersonByNameOrAgeWithSQLTag" parameterType="Person" resultType="HashMap" >
select id "id",name "name" from Person
<where>
<if test="ids!=null and ids.size>0 ">
<!-- open里面开头空格不能省略 -->
<foreach collection="ids" open=" and stuno in (" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
4是分隔符
输入参数为集合属性、集合、数组、对象数组
如果输入参数是简单类型数组
,例如:parameterType=“int[]”
则 test="array!=null and array.length>0 ",即输入参数名固定为array
,与传入参数名无关
如果输入参数是集合
,例如:parameterType=“list”
则 test="list!=null and list.size>0 ",即输入参数名固定为list
,与传入参数名无关
如果输入参数是对象数组
,则parameterType=“Object[]”,foreach里面还是array
SQL片段,对于重复率比较高的sql片段,可以提取出来