动态sql的标签
If、where、trim、set、choose(when, otherwise)、foreach
If标签
-
if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之标签中的内容不会执行
-
在where后面添加一个恒成立条件1=1,不影响结果,1=1可以用来对where进行连接。
-
如果没有1=1,比如:no!=null的话,此时的语句是select id ,no,name,gender from student where and no = ?,and name = ?,and gender=?
-
1=1的作用是:select id,no,name,gender from student where 1=1 and no = ?,and name = ?,and gender=?
-
<select id="findStudent" parameterType="Student" resultType="Student">
select id,no,name,gender from student
where 1=1
<if test="no!=null">
and no = #{no}
</if>
<if test="name!=null">
and name = #{name}
</if>
<if test="gender!=null">
and gender = #{gender}
</if>
</select>
where标签
- where和if一般结合使用:
- 元素会进行判断,如果它包含的标签中有返回值的话,它就插入一个 ‘where’。
- 此外,如果标签返回的内容是以 AND 或 OR 开头,它会剔除掉 AND 或 OR。
<select id="findStudent" parameterType="Student" resultType="Student">
select id,no,name,gender from student
<where>
<if test="no!=null">
no = #{no}
</if>
<if test="name!=null">
and name = #{name}
</if>
<if test="gender!=null">
and gender = #{gender}
</if>
</where>
</select>
trim标签
-
trim用于去掉或添加标签中的内容
-
常用属性
-
prefix:在trim标签中的内容的前面添加某些内容
-
suffix:在trim标签中的内容的后面添加某些内容
-
prefixOverrides:在trim标签中的内容的前面删掉某些内容
-
suffixOverrides:在trim标签中的内容的后面删掉某些内容
-
<select id="findStudent" parameterType="Student" resultType="Student">
select id,no,name,gender from student
<trim prefix="where" prefixOverrides="and|or">
<if test="no!=null">
no = #{no}
</if>
<if test="name!=null">
and name = #{name}
</if>
<if test="gender!=null">
and gender = #{gender}
</if>
</trim>
</select>
set标签
- set标签中如果有成立的,就会添加set标签。如果以逗号结尾,就会删除结尾的逗号
<update id="updateStudent" parameterType="Student">
update student
<set>
<if test="name!=null">
name = #{name},
</if>
<if test="gender!=null">
gender = #{gender},
</if>
<if test="grade!=null">
gradeid = #{gradeid},
</if>
</set>
where id = #{id}
</update>
set标签在trim标签中的使用
-
prefix:在trim标签中的内容的前面添加某些内容
-
suffixOverrides:在trim标签中的内容的后面删掉某些内容
<update id="updateStudent" parameterType="student">
update student
<trim prefix="set" suffixOverrides=",">
<if test="name!=null">
name = #{name},
</if>
<if test="gender!=null">
gender = #{gender},
</if>
<if test="grade">
gradeid = #{grade.id},
</if>
</trim>
where id = #{id}
</update>
choose(when, otherwise)
choose、when、otherwise
相当于if...else if..else
- when至少要有一个,otherwise至多只有一个
<select id="findStudent" parameterType="Student" resultType="Student">
select id,no,name,gender from student
<trim prefix="where" prefixOverrides="and|or">
<choose>
<when test="name!=null">
name = #{name}
</when>
<otherwise>
name = '张三'
</otherwise>
</choose>
</trim>
</select>
@Test
public void find(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao1 mapper = sqlSession.getMapper(StudentDao1.class);
Student student = new Student();
student.setName("赵甜");
List<Student> studentList = mapper.findStudent(student);
System.out.println(studentList);
sqlSession.close();
}
@Test
public void find(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao1 mapper = sqlSession.getMapper(StudentDao1.class);
Student student = new Student();
student.setName(null);
List<Student> studentList = mapper.findStudent(student);
System.out.println(studentList);
sqlSession.close();
}
foreach标签
- foreach 元素的属性主要有 item,collection,open,separator,close。
- item 表示集合中每一个元素进行迭代时的别名;
- open 表示该语句以什么开始;
- separator 表示在每次进行迭代之间以什么符号作为分隔符;
- close 表示以什 么结束;
- 在使用 foreach 的时候最关键的也是最容易出错的就是 collection 属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的。
- 如果传入的是单参数且参数类型是一个 List 的时候,collection 属 性值为 list
- 如果传入的是单参数且参数类型是一个 array 数组的时候, collection 的属性值为 array
<select id="findSomeStudent" resultType="com.cheng.model.Student">
select id,no,name,gender from student
where no in
<foreach collection="list" item="no" open="(" separator="," close=")">
#{no}
</foreach>
</select>
@Test
public void finds(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao1 mapper = sqlSession.getMapper(StudentDao1.class);
List<Integer> list = new ArrayList<>();
list.add(100);
list.add(101);
List<Student> student = mapper.findSomeStudent(list);
System.out.println(student);
sqlSession.close();
}
<select id="findSomeStudent1" resultType="com.cheng.model.Student">
select id,no,name,gender from student
where no in
<foreach collection="array" item="no" open="(" separator="," close=")">
#{no}
</foreach>
</select>
@Test
public void find1(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao1 mapper = sqlSession.getMapper(StudentDao1.class);
Integer[] arr = new Integer[]{100,101};
List<Student> student = mapper.findSomeStudent1(arr);
System.out.println(student);
sqlSession.close();
}