MyBatis中有哪些常用标签及使用场景呢?
下文笔者讲述MyBatis中常用标签的简介说明,如下所示
MyBatis常用标签
<if> <where> <trim> <foreach>
1.<if> 标签
if标签的功能: 用于where语句的条件动态拼接前的判断
<!--使用 if 元素根据条件动态查询用户信息-->
<select id="selectUser" resultType="com.domain.UserInfo" parameterType="com.domain.UserInfo">
select * from user_info where 1=1
<if test="uname!=null and uname!=''">
and uname like concat('%',#{uname},'%')
</if >
<if test="usex !=null and usex !=''">
and usex=#{usex}
</if >
</select>
注意事项:
where后面1=1
为解决当两个条件都不为true时
sql语句不合法导致报错的问题
2.<where> 标签
where标签的功能:
输出where语句
可智能过滤掉条件中多出来and或or
当条件都不满足,则会查出所有记录
例
<!--使用where元素根据条件动态查询用户信息-->
<select id="selectUser" resultType="com.domain.UserInfo" parameterType="com.domain.MyInfo">
select * from user_info
<where>
<if test="uname != null and uname ! = ''">
and uname like concat('%',#{uname},'%')
</if>
<if test="usex != null and usex != '' ">
and usex=#{usex}
</if >
</where>
</select>
3.<trim> 标签
trim标签的功能:
用于添加SQL语句的前缀或后缀
可使用 <trim> 来代替 <where>功能
<!--使用trim元素根据条件动态查询用户信息-->
<select id="selectUser" resultType="com.domain.UserInfo"parameterType="com.domain.UserInfo">
select * from user_info
<trim prefix="where" prefixOverrides = "and | or">
<if test="uname!=null and uname!=''">
and uname like concat('%',#{uname},'%')
</if>
<if test="usex!=null and usex!=''">
and usex=#{usex}
</if>
</trim>
</select>
注意事项:
prefix:指定sql语句拼接的前缀
subfix:指定sql语句拼接的后缀
prefixOverrides:指定sql语句前面要去除的关键字或字符,如AND 逗号 括号等
suffixOverrides:指定sql语句后面要去除的关键字或字符
4.<foreach>标签
foreach功能:
用于在SQL语句中迭代一个集合
可用在构建in条件中
<!--使用foreach元素查询用户信息--> <select id="selectUserByForeach" resultType="com.domain.UserInfo" parameterType= "list"> select * from user_info where uid in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> # {item} </foreach> </select> <foreach> 元素的属性包括: item:集合中每一个元素进行迭代时的别名 index:标识指定一个名字,用于表示在迭代过程中每次迭代到的位置 open:该语句以什么开始 separator:在每次进行迭代之间以什么符号作为分隔符 close:以什么结束
二.MyBatis标签使用场景示例
1.List 批量插入数据操作
<!--使用List批量插入多条记录-->
<insert id="insertByList" parameterType="java.util.List">
INSERT INTO user_info
(name,sex,address,status,create_time)
VALUES
<foreach collection="userInfoList" item="item" index="index" separator=",">
(
#{item.name},
#{item.sex},
#{item.address},
#{item.status},
#{item.createTime}
)
</foreach>
</insert>
2.使用List批量更新数据操作
根据id值不同
设置对应的状态值和创建时间
<!--使用List批量更新多条记录-->
<update id="updateByList" parameterType="java.util.List">
<if test="userInfoList!=null">
UPDATE user_info
<trim prefix="set" suffixOverrides=",">
<trim prefix="status = case" suffix="end,">
<foreach collection="userInfoList" item="item" index="index">
<if test="item.status != null and item.status != ''">
WHEN id=#{item.id} THEN #{item.status}
</if>
</foreach>
</trim>
<trim prefix="create_time = case" suffix="end,">
<foreach collection="userInfoList" item="item" index="index">
<if test="item.createTime != null">
WHEN id=#{item.id} THEN #{item.createTime}
</if>
</foreach>
</trim>
</trim>
<where>
id IN
<foreach collection="userInfoList" item="item" index="index" separator="," open="(" close=")">
<if test="item.id != null and item.id != ''">
#{item.id}
</if>
</foreach>
</where>
</if>
</update>
注意事项:
时间类型判断为空时
只需使用<if test="item.createTime != null">
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


