已知有resultMap:
<resultMap type="com.briup.oneToMany.Course" id="coursesResult"> <id property="courseId" column="course_id"/> <result property="name" column="name"/> <result property="description" column="description"/> <result property="startDate" column="start_date"/> <result property="endDate" column="end_date"/> </resultMap>
1、<if>
if标签中嵌套了sql语句,如果满足条件就执行,否则不执行标签内的sql语句,例如:根据教师Id(必须存在)查找信息,可选选项是课程名称、开始时间和结束时间。
<select id="searchCourses" parameterType="map" resultMap="coursesResult"> select * from courses where tutor_id=#{tutorId} <if test="name != null"> and name like #{name} </if> <if test="startDate != null"> and start_date <![CDATA[>=]]> #{startDate} </if> <if test="endDate != null"> and end_date <![CDATA[<=]]> #{endDate} </if> </select>
测试代码:
map.put("tutorId", 1); map.put("name", "%My%");
2、<choose>
choose标签是根据类别来进行查询的,当满足when标签里面的test属性时,才会去执行when标签里面的sql语句,如果都不满足,则会执行otherwise标签里的sql语句。
<select id="searchCourses" parameterType="map" resultMap="coursesResult"> select * from courses <choose> <when test="searchBy == 'Tutor'"> where tutor_id = #{tutorId} </when> <when test="sreachBy == 'courseName'"> where name like #{courseName} </when> <otherwise> where start_date <![CDATA[>=]]> sysdate </otherwise> </choose> </select>
测试代码:
map.put("sreachBy", "Tutor"); map.put("tutorId", 1); map.put("sreachBy", "courseName"); map.put("courseName", "%My%");
3、<where>
where标签表示所有的查询条件都是可选的,在需要使用至少一种查询条件的情况下,可以直接使用where子句,如果有多个条件,可以添加and或者or。
<select id="searchCourses" parameterType="map" resultMap="coursesResult"> select * from courses <where> <if test="tutorId != null"> tutor_id = #{tutorId} </if> <if test="courseName != null"> and name like #{courseName} </if> </where> </select>
测试代码:
map.put("tutorId", 1); map.put("courseName", "%Java%");
4、<foreach>
foreach标签是用来迭代遍历一个数组或者集合的
第一种使用or的情况:
<select id="searchCourses" parameterType="map" resultMap="coursesResult"> select * from courses <if test="tutorIds != null"> <where> <foreach collection="tutorIds" item="tutorId"> or tutor_id = #{tutorId} </foreach> </where> </if> </select>
第二种使用in的情况:
<select id="searchCourses" parameterType="map" resultMap="coursesResult"> select * from courses <if test="tutorIds != null"> <where> tutor_id in <foreach collection="tutorIds" item="tutorId" open="(" close=")" separator=","> #{tutorId} </foreach> </where> </if> </select>
测试代码:
List<Integer> tutorIds = new ArrayList<Integer>(); tutorIds.add(1); tutorIds.add(2); map.put("tutorIds", tutorIds);
5、<trim>
trim和where类似,trim提供了添加 前缀/后缀 或者 移除 前缀/后缀
<select id="searchCourses" parameterType="map" resultMap="coursesResult"> select * from courses <trim prefix="where" prefixOverrides="and"> <if test="tutorId != null"> and tutor_id = #{tutorId} </if> <if test="courseName != null"> and name like #{courseName} </if> </trim> </select>
测试代码:
map.put("tutorId", 1); map.put("courseName", "%Java%");
6、<set>
set标签只是针对update更新语句使用的。
<set> <if test="name != null">name=#{name}</if> <if test="email != null">email=#{email}</if> <if test="phone != null">phone=#{phone}</if> </set>