MyBatis的动态SQL
if:利用if实现简单的条件选择。
choose(when,otherwise):相当于Java中的switch语句,通常与when和otherwise搭配。
where:简化SQL语句中的where的条件判断。
set:解决动态更新语句。
trim:灵活地去除多余的关键字。
foreach:迭代一个集合,通常用于in条件。
========================================================================================================================================================================================================
使用if+where实现多条件查询
1 <--演示如何灵活处理where and|or --> 2 <select id="getUserList" resultType="User"> 3 select * from smbms_user 4 <where> 5 <if test="userName != null and userName != ''"> 6 and userName like CONCAT ('%',#{userName},'%') 7 </if> 8 <if test="userRole != null"> 9 and userRole = #{userRole} 10 </if> 11 </where> 12 </select>
利用<if>元素实现简单的条件判断,<if>元素的test属性表示进入<if>内需要满足的条件。
<where>元素主要用来简化SQL语句中的where条件判断,并能智能处理and和or。<where>元素标签会自动识别其标签内是否有返回值,若有,就插入一个where。此外,若该标签返回的内容是以and或者or开头的,会自动剔除。
========================================================================================================================================================================================================
使用if+trim实现多条件查询
1 <--演示if+trim也可以达到if+where多条件查询 --> 2 <select id="getUserList" resultType="User"> 3 select * from smbms_user 4 <trim prefix="where" prefixOverrides="and | or"> 5 <if test="userName != null and userName != ''"> 6 and userName like CONCAT ('%',#{userName},'%') 7 </if> 8 <if test="userRole != null"> 9 and userRole = #{userRole} 10 </if> 11 </trim> 12 </select>
我们可以用<trim>元素来替代<where>元素。
<trim>有四个常用属性: prefix:前缀,作用是通过自动识别是否有返回值后,在trim包含的内容上加上前缀,如此处的where。
suffix:后缀,作用是在trim包含的内容上加上后缀。
prefixOverrides:对于trim包含内容的首部进行指定内容的忽略,如此处的and|or。
suffixOverrides:对于trim包含内容的尾部进行指定内容的忽略。
========================================================================================================================================================================================================
利用if+set实现更新操作
1 <!-- 修改用户信息 --> 2 <update id="modify" parameterType="User"> 3 update smbms_user 4 <set> 5 <if test="userCode != null">userCode=#{userCode},</if> 6 <if test="userName != null">userName=#{userName},</if> 7 <if test="userPassword != null">userPassword=#{userPassword},</if> 8 <if test="gender != null">gender=#{gender},</if> 9 <if test="birthday != null">birthday=#{birthday},</if> 10 <if test="phone != null">phone=#{phone},</if> 11 <if test="address != null">address=#{address},</if> 12 <if test="userRole != null">userRole=#{userRole},</if> 13 <if test="modifyBy != null">modifyBy=#{modifyBy},</if> 14 <if test="modifyDate != null">modifyDate=#{modifyDate},</if> 15 </set> 16 where id = #{id} 17 </update>
使用<set>元素标签不仅可以动态地配置set关键字,还可以提出追加到条件末尾的任何不想关的逗号(因为在update语句中使用<if>标签,若后面的if没有执行,则导致在语句末尾残留多余的逗号)。
========================================================================================================================================================================================================
利用if+set实现更新操作
1 <!-- 修改用户信息 --> 2 <update id="modify" parameterType="User"> 3 update smbms_user 4 <trim prefix="set" suffixOverrides="," suffix="where id = #{id}"> 5 <if test="userCode != null">userCode=#{userCode},</if> 6 <if test="userName != null">userName=#{userName},</if> 7 <if test="userPassword != null">userPassword=#{userPassword},</if> 8 <if test="gender != null">gender=#{gender},</if> 9 <if test="birthday != null">birthday=#{birthday},</if> 10 <if test="phone != null">phone=#{phone},</if> 11 <if test="address != null">address=#{address},</if> 12 <if test="userRole != null">userRole=#{userRole},</if> 13 <if test="modifyBy != null">modifyBy=#{modifyBy},</if> 14 <if test="modifyDate != null">modifyDate=#{modifyDate},</if> 15 </trim> 16 </update>
========================================================================================================================================================================================================
使用foreach完成复杂查询(foreach主要用在构建in条件中,它的主要属性有:item、index、collection、separator、close、open)
item:表示集合中每一个元素进行迭代时的别名。
index:指定一个名称,用于表示在迭代过程中,每次迭代到的位置。
open:表示该语句以什么开始(既然是in条件语句,所以必然是以"("开始)。
separator:表示在每次进行迭代之间以什么符号作为分隔符(既然是in条件语句,所以是以","作为分隔符)。
close:表示该语句以什么结束(既然是in条件语句,所以必然是以")"结束)。
collection:该属性必须指定,不同情况下,该属性的值是不一样的。(若入参为单参数且参数类型是一个List的时候,属性值为list;若入参为单参数且参数类型是一个数组的时候,属性值为array;若传入参数为多参数,就需要把它们封装为 一个Map进行处理,属性值为需要迭代的Map的key名)
1.入参为数组类型的foreach迭代
1 <!-- 根据用户角色列表,获取该角色列表下用户列表信息-foreach_array --> 2 <select id="getUserByRoleId_foreach_array" resultMap="userMapByRole"> 3 select * from smbms_user where userRole in 4 <foreach collection="array" item="roleIds" open="(" separator="," close=")"> 5 #{roleIds} 6 </foreach> 7 </select> 8 <resultMap type="User" id="userMapByRole"> 9 <id property="id" column="id"/> 10 <result property="userCode" column="userCode"/> 11 <result property="userName" column="userName"/> 12 </resultMap>
2.入参为List类型的foreach迭代
1 <!-- 根据用户角色列表,获取该角色列表下用户列表信息-foreach_list --> 2 <select id="getUserByRoleId_foreach_list" resultMap="userMapByRole"> 3 select * from smbms_user where userRole in 4 <foreach collection="list" item="roleList" open="(" separator="," close=")"> 5 #{roleList} 6 </foreach> 7 </select>
3.入参为Map类型的foreach迭代
1 <!-- 根据用户角色列表和性别(多参数),获取该角色列表下并指定性别的用户列表信息-foreach_map --> 2 <select id="getUserByConditionMap_foreach_map" resultMap="userMapByRole"> 3 select * from smbms_user where gender = #{gender} and userRole in 4 <foreach collection="roleIds" item="roleMap" open="(" separator="," close=")"> 5 #{roleMap} 6 </foreach> 7 </select> 8 9 <!-- 根据用户角色列表(单参数),获取该角色列表下用户列表信息-foreach_map --> 10 <select id="getUserByRoleId_foreach_map" resultMap="userMapByRole"> 11 select * from smbms_user where userRole in 12 <foreach collection="rKey" item="roleMap" open="(" separator="," close=")"> 13 #{roleMap} 14 </foreach> 15 </select>
========================================================================================================================================================================================================
使用choose实现查询(根据条件查询用户表,具体要求:查询条件提供前三个中的任意一个即可,若前三个条件都不提供;那么默认提供最后一个条件来完成查询操作)
1 <!-- 查询用户列表(choose) --> 2 <select id="getUserList_choose" resultType="User">
<!--这里加入where 1=1 的原因是我们不需要再去处理多余的“and”--> 3 select * from smbms_user where 1=1 4 <choose> 5 <when test="userName != null and userName != ''"> 6 and userName like CONCAT ('%',#{userName},'%') 7 </when> 8 <when test="userCode != null and userCode != ''"> 9 and userCode like CONCAT ('%',#{userCode},'%') 10 </when> 11 <when test="userRole != null"> 12 and userRole=#{userRole} 13 </when> 14 <otherwise> 15 <!-- and YEAR(creationDate) = YEAR(NOW()) --> 16 and YEAR(creationDate) = YEAR(#{creationDate}) 17 </otherwise> 18 </choose> 19 </select>
<when>元素:当其属性test中条件满足的时候,就会输出<when>元素中的内容,并当<when>中一旦有条件满足的时候,就会跳出<choose>元素标签,即所有的<when>和<otherwise>条件中,只有一个条件会输出。
<otherwise>元素:当<when>中的所有条件都不满足的时候,就会自动输出<otherwise>元素中的内容。
========================================================================================================================================================================================================
MyBatis实现分页功能(增加了两个参数:起始位置(from)和页面容量(pageSize))
1 <!-- 查询用户列表(分页显示) --> 2 <select id="getUserList" resultMap="userList"> 3 select u.*,r.roleName from smbms_user u,smbms_role r where u.userRole = r.id 4 <if test="userRole != null"> 5 and u.userRole = #{userRole} 6 </if> 7 <if test="userName != null and userName != ''"> 8 and u.userName like CONCAT ('%',#{userName},'%') 9 </if> 10 order by creationDate DESC limit #{from},#{pageSize} 11 </select>

浙公网安备 33010602011771号