mybatis的常用标签
1、CDATA标签<![CDATA[ ]]>:内容里面的符号不进行解析,如:”<”,”>”,”&”等2、foreach标签collection:指定数据的类型,(如果ids是字符串,也可以用ids.split(',')分割成字符串数组)item:遍历时生成的对象open:开始遍历时拼接的字符串close:结束遍历时拼接的字符串separator:...
1、CDATA标签
<![CDATA[ ]]>:内容里面的符号不进行解析,如:”<”,”>”,”&”等
2、foreach标签
collection:指定数据的类型,(如果ids是字符串,也可以用ids.split(',')分割成字符串数组)
item:遍历时生成的对象
open:开始遍历时拼接的字符串
close:结束遍历时拼接的字符串
separator:拼接sql使用的分隔符
<foreach item="id" collection="ids" open="(" close=")" separator=","></foreach>
3、include标签
include引用,可以复用SQL片段
sql标签中id属性对应include标签中的refid属性。
<sql id="fields">
id,user_name
</sql>
<select id="user.selectbyId" resultType="User">
select
<include refid="fields"/>
from user
</select>
4、choose标签
按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束,当 choose 中所有 when 的条件都不满足时,则执行 otherwise
<select id="user.selectbyId" resultType="User" parameterType="User">
select
<include refid="fields"/>
from user where 1=1
<choose>
<when test="state == 1">
and state in (1,2,3)
</when>
<when test="state == 2">
and state in (4,5,6)
</when>
<otherwise>
and state in (7,8,9)
</otherwise>
</choose>
</select>
5、trim标签
prefix:拼接sql的前缀
suffix:拼接sql的后缀
prefixOverrides:去除sql语句前面的关键字符
suffixOverrides:去除sql语句后面的关键字符
<select id="user.selectbyId" resultType="User" parameterType="User">
select * from user
<trim prefix="WHERE" prefixOverrides="AND | OR">
<if test="name != null and name != ''"> AND user_name like '%${name}%'</if>
<if test="sex != null and sex != ''"> AND sex = #{sex}</if>
</trim>
</select>
<!-- 假如说name和sex的值都不为空,SQL:select * from user WHERE user_name like '%xx%' and sex = 'x' -->
<update id="user.update" parameterType="User">
update user
<trim prefix="set" suffixOverrides="," suffix=" where id = #{id}">
<if test="name != null and name != ''"> user_name = #{name},</if>
<if test="sex != null and sex != ''"> sex = #{sex},</if>
</trim>
</update>
<!-- 假如说name和sex的值都不为空,SQL:update user set user_name = 'xx',sex = 'xx' where id = 'x' -->
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)