if标签
有时SQL语句不一定是写死的,需要根据具体调用时传入的参数来确定选择条件,这时可以用到if标签。
<select id="queryPersonName" parameterType="entities.Person" resultType="String">
select name from person where 1=1
<if test="id!=null and id!=0">
and name = #{name}
</if>
<if test="age!=null and age!=0">
and age = #{age}
</if>
<if test="sex!=null and sex!=''" >
and sex = #{sex}
</if>
</select>
测试:
PersonMapper personMapper = getPersonMapper();
Person person = new Person();
person.setSex("male");
person.setAge(20);
//SQL语句为select name from person where 1=1 and age = 20 and sex = 'male'
List<String> names = personMapper.queryPersonName(person);
where标签
where 可以自动处理第一个 and,就不需要像上一例一样增加 1=1语句了。
<select id="queryPersonName" parameterType="entities.Person" resultType="String">
select name from person
<where>
<if test="id!=null and id!=0">
and name = #{name}
</if>
<if test="age!=null and age!=0">
and age = #{age}
</if>
<if test="sex!=null and sex!=''" >
and sex = #{sex}
</if>
</where>
</select>
sql、include标签
当某一些SQL语句需要重复使用时,可以将它们保存为SQL片段以便重复使用。
当需要使用时,通过include标签引入即可。
<sql id="where_if">
<where>
<if test="id!=null and id!=0">
and name = #{name}
</if>
<if test="age!=null and age!=0">
and age = #{age}
</if>
<if test="sex!=null and sex!=''" >
and sex = #{sex}
</if>
</where>
</sql>
<select id="queryPersonName" parameterType="entities.Person" resultType="String">
select name from person
<include refid="where_if"></include>
</select>
foreach标签
foreach 用于循环迭代,用于输入参数为数组或集合时。标签属性有collection、 item、open、close
当类型为数组时,还具有index属性,表示当前位置下标。
-
collection指定输入集合类型
迭代的类型:数组、对象数组、集合、属性
当为属性时,collection值为属性名
当为基本类型或字符串数组时,用array代替集合名
当为对象数组时,用array代替集合名,parameterType=“Object[]”
当为集合时,用list代替集合名 -
item指定每一对象的名字,值随意
-
open指定开始遍历前要加入拼接的字符
-
close指定迭代结束时要拼接的字符
-
separator指定遍历的两个对象中需要拼接的字符,一般为‘,’或者‘or’。
<select id="queryPersonNameByIdArray" parameterType="int[]" resultType="String">
select name from person
<where>
<if test="array!=null and array.length>0">
<foreach collection="array" item="id" open=" and id in (" separator="," close=")">
#{id}
</foreach>
</if>
</where>
</select>
测试类:
PersonMapper personMapper = getPersonMapper();
int[] id = {1,2,3};
//SQL语句为select name from person where id in (1,2,3)
List<String> names = personMapper.queryPersonNameByIdArray(id);
System.out.println(names);
对象数组:
<select id="queryPersonNameByPersonArray" parameterType="Object[]" resultType="String">
select name from person
<where>
<if test="array!=null and array.length>0">
<foreach collection="array" item="item" open=" and id in (" separator="," close=")">
#{item.id}
</foreach>
</if>
</where>
</select>
测试类:
PersonMapper personMapper = getPersonMapper();
Person[] persons = new Person[4];
for(int i=0;i<4;i++) {
persons[i] = new Person();
persons[i].setId(i+1);
}
List<String> names = personMapper.queryPersonNameByPersonArray(persons);
System.out.println(names);
trim标签
trim标签用于处理SQL语句拼接问题。功能比where标签更加强大。用到了四个属性
- prefix:用于在trim标签包含的语句前增加语句
- prefixOverrides:处理不合适的前缀(例如指定prefixOverrides="and"出现where and时,会自动处理掉第一个and)
- subfix:用于在trim标签包含的语句后增加语句
- subfixOverrides:处理不合适的后缀
<update id="updateStudentById" parameterType="student">
update student
<trim prefix="set" suffixOverrides=",">
<if test="sname != null">
sname=#{sname},
</if>
<if test="classid != 0">
classid=#{classid},
</if>
</trim>
<where>
sid=#{sid}
</where>
</update>