【MyBatis】动态SQL



(1) 动态元素

 


(2) if元素(和test属性联合使用)

<select id=”findRoles”  parameterType=”string”  resultMap=”roleResultMap”>
select role_no , role_name , note  from t_role  where 1=1
<if  test=”roleName != null  and  roleName !=’’ ”>
and role_name  like concat(‘%’ , #{roleName} , ‘%’)
</if>
</select>

(3) choose、when、otherwise元素

 实例:
当角色编号不为空,则只用角色编号作为条件查询
当角色编号为空,而角色名称不为空,则用角色名称作为条件查询
当角色编号和角色名称都为空,则要求角色备注不为空
<select id=”findRoles” parameterType=”role”  resultMap=”roleResultMap”>
select role_no , role_name , note from t_role where 1 =1 
<choose>
<when test=”roleNo !=null  and roleNo  !=’’ ”>
AND role_no = #{roleNo}
</when>
<when  test=”roleName !=null  and roleName=’’ ”>
AND role_name  like  concat(‘%’ , #{roleName} , ‘%’)
</when>
<otherwise>
AND note is not null
</otherwise>
</choose>
</select>

(4) trim、where、set元素

实例一:where元素
<select id=”findRoles”  parameterType=”string” resultMap=”roleResultMap”>
select role_no , role_name , note  from t_role 
<where>
<if  test=”roleName !=null  and roleName!=’’ ”>
and role_name like concat(‘%’ , #{roleName} , ‘%’)
</if>
</where>
</select>

实例二:trim元素
<select id =”findRoles”  parameterType=”string”  resultMap=”roleResultMap”>
select role_no , role_name , note from t_role
<trim prefix=”where”  prefixOverrides=”and”>
<if  test=”roleName !=null  and roleName !=’’ ” >
and  role_name  like  concat(‘%’, #{roleName} , ‘%’)
</if>
</trim>
</select>
说明:trim元素:意味着要去掉一些特殊的字符串
 prefix:代表语句的前缀
 prefixOverrides:代表需要去掉的字符串

实例三:set元素
<update  id=”updateRole”  parameterType=”role”>
update t_role
<set>
<if  test=”roleName != null  and  roleName!=’’ ”>
role_name = #{roleName},
</if>
<if  test=”note!=null  and  note != ‘’ ”>
note = #{note}
</if>
</set>
where role_no = #{roleNo}
</update>
说明:只想更新备注,只需要传递备注信息和角色编号即可,set元素会把对应的逗号去掉

(5) foreach元素

作用:遍历集合
<select  id=”findUserBySex”  resultType=”user”>
select  * from  t_user  where  sex  in 
<foreach   item=”sex”  index=”index”  collection=”sexList”  open=”(”  separator=”,”  close=”)”>
#{sex}
</foreach>
</select>

说明:
collection:sexList是传递进来的参数名称,可以是一个数组或者List、Set等集合
item:循环中当前的元素
index:当前元素在集合的位置下标
open和close:配置以什么符号将这些集合包装进来
separator:各个元素的间隔符


(6) test的属性

用于条件判断的语句中。
<select id=”getRoleTest”  parameterType=”roleResultMap”>
select  role_no , role_name , note  from  t_role
<if  test=”type = ‘Y’”>
where  1= 1 
</if>
</select>

(7) bind元素

作用:通过OGNL表达式去自定义一个上下文变量。
问题说明:
模糊查询时,MySQL数据库:concat用”%”和参数相连接
Oracle数据库使用连接符号”||”
bind元素可以解决该问题
实例一:单个参数
<select  id=”findRole” resultType = “com.lwt.RoleBean”>
<bind  name=”pattern”  value=”’%’+ _parameter +’%’”/>
SELECT  id, role_name  as roleName , create_date  as createDate , end_date as endDate , end_flag as endFlag , note  FROM  t_role
where role_name like #{pattern}
</select>
说明:“_parameter”代表的是传递进来的参数,赋值给了pattern,这样不管是MySQL数据库还是Oracle数据库都可以使用这样的语句。

实例二:多参数绑定
<select  id=”findRole” resultType=”com.lwt.RoleBean”>
<bind  name=”pattern_roleName”  value=”’%’+roleName+’%’ ”/>
<bind  name=”pattern_note”  value=”’%’+ note +’%’ ”/>
SELECT id, role_name  as  roleName , create_date as createDate ,
end_date  as endDate  , end_flag  as  endFlag , note  FROM t_role
where role_name  like #{pattern_roleName}
and  note like #{pattern_note}
</select>
接口:
public  List<RoleBean>  findRole(@Param(“roleName”) String  roleName , 
@Param(“note”) String note)l
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值