mybatis动态语句的元素及简单用法
if 作用:做条件判断 (但条件判断)
例:
<select id="selByName" resultType="com.ganin.entity.Account">
select id,name,created,updated from account where 1=1
<if test="name !=null and name !=''">
and name like concat('%',#{name},'%')
</if>
</select>
choose-when-otherwise 作用:条件选择(参考一下switch语句形式; 用来多条件分支判断)
例:
<select id="selByChoose" resultType="com.ganin.entity.Account">
select id,name,created,updated,money from account where 1=1
<choose>
<when test="name !=null and name !=''">
and name like concat('%',#{name},'%')
</when>
<when test="money !=null and money !=''">
and money =#{money}
</when>
<otherwise>
and isdeleted=1
</otherwise>
</choose>
</select>
where、set 作用:辅助 (处理sql语句拼接问题)
where~if 例:
<select id="selByName" resultType="com.ganin.entity.Account">
select id,name,created,updated from account
<where>
<if test="name !=null and name !=''">
and name like concat('%',#{name},'%')
</if>
</where>
</select>
set ~if 例:(在遇到逗号的时候,把对应的逗号去掉)
<update id="updateAccout" parameterType="com.ganin.entity.Account">
update account
<set>
<if test="name !=null and name !=''">
name=#{name},
</if>
<if test="money!=null and money!=''">
money=#{money}
</if>
</set>
where id=#{id}
</update>
foreache 作用:循环 (作用时用来遍历集合,支持数组、List、Set接口集合)
<select id="selIn" resultType="com.ganin.entity.Account">
select id,name,created,updated from account where name in
<foreach collection="names" separator="," item="name" open="(" separator="," close=")">
#{name}
</foreach>
</select>
版权声明:本文为weixin_54517901原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。