MyBatis五动态语句

动态SQL
动态SQL其实不难,但是很实用
if choose when otherwise where set trim foreach bind
----------------------------------------------------
<if test="roleName!=null and roleName!=''">
and role_name like concat('%',#{roleName},'%')
</if>
----------------------------------------------------
<choose>
<when test="roleNo!=null and roleNo!=''">
and roleNo = #{roleNo}
</when>
<when test="roleName!=null and roleNo!=''">
and roleName like concat('%',#{roleName},'%')
</when>
<otherwise>
and note is not null
</otherwise>
</choose>
----------------------------------------------------where元素如果条件成立可以自己加上 where
<where>
<if test="roleName!=null and roleName!=''">
and role_name like concat('%',#{roleName},'%')
</if>
</where>
----------------------------------------------------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!=''">
role_name = #{note}
</if>
</set>
</update>
----------------------------------------------------trim元素意味着我们要去掉一些特殊的字符串
<select id="findRole" parameterType="string" resultMap="roleResultMap">
select role_no,role_name,note from t_role
<trim prifx="where" prifxOverrides="and">
<if test="roleName!=null and roleName!=''">
and role_name like concat('%',#{roleName},'%')
</if>
</trim>
</select>
----------------------------------------------------foreach 遍历集合
<select id="findRole" parameterType="string" resultMap="roleResultMap">
select role_no,role_name,note from t_role
<where>
<if test="sexList"!=null">
<foreach item="sex" index="index" collection="sexList" open="(" separator="," close=")">
#{sex}
</foreach>
</if>
</where>
</select>
----------------------------------------------------bind元素,定义一个上下文变量,很有用的元素,_pattern是传递进来的参数
<select id="findRole" parameterType="string" resultMap="roleResultMap">
<bind name="pattern" value="'%'+_pattern+'%'"></bind>
select role_no,role_name,note from t_role
<where>
<if test="pattern!=null and pattern!=''">
and  pattern like #{pattern}
</if>
</where>
</select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值