MyBatis之xml文件中常用的标签简介说明
下文笔者讲述MyBatis之xml文件中常用标签简介说明,如下所示
例:使用MyBatis提供的<foreach>标签拼接SQL语句
mybatis常用标签功能
mybatis中常用标签的功能:
用于按照一定的规则组合sql语句
如:
where if sql foreach
choose when otherwise
trim set
例
<where>标签、<if>标签
当查询语句的查询条件由于输入参数的不同而无法确切定义时
可使用<where>标签对来包裹需要动态指定的SQL查询条件
而在<where>标签对中
可使用<if test="...">条件来分情况设置SQL查询条件
例:MyBatis中使用动态SQL语句,获取用户信息
<!-- 查询用户信息 -->
<select id="queryUserInfo" parameterType="com.java265.mybatis.po.UserParam" resultType="com.java265.mybatis.po.User">
SELECT * FROM tb_user
<where>
<if test="userId>0">
and user_id = #{userId}
</if>
<if test="userName!=null and userName!=''">
and user_name like '%${userName}%'
</if>
<if test="sex!=null and sex!=''">
and sex = #{sex}
</if>
</where>
</select>
<sql>片段
sql片段可以为mybatis 提供可复用的代码块
<sql id="query_user_where">
<!-- 要复用的SQL语句 -->
</sql>
注意事项:
1.sql片段中的id ,在当前xml文件中必须唯一
2.sql片段中不要使用<where>标签,而在调用的SQL方法中写<where>标签
如果sql方法可能还会引入其他的SQL片段
如果这些多个的SQL片段中都有<where>标签,则会引起语句冲突
3. sql片段中除自身所在的Mapper文件
每个sql映射配置还可以引入外部Mapper文件中的SQL片段
只需要在refid属性填写的SQL片段的id前添加其所在Mapper文件的namespace信息即可(如:test.query_user_where)
例:sql片段示例
<!--用户查询条件SQL片段-->
<sql id="query_user_where">
<if test="userId>0">
AND user_id = #{userId}
</if>
<if test="userName!=null and userName!=''">
AND user_name like '%${userName}%'
</if>
<if test="sex!=null and sex!=''">
AND sex = #{sex}
</if>
</sql>
<!-- 查询用户信息 -->
<select id="queryUserInfo" parameterType="com.java265.mybatis.po.UserParam" resultType="com.java265.mybatis.po.User">
SELECT * FROM tb_user
<where>
<include refid="query_user_where"/>
<!-- 这里可能还会引入其他的SQL片段 -->
</where>
</select>
<!-- 查询用户总数 -->
<select id="queryUserCount" parameterType="com.java265.mybatis.po.UserParam" resultType="int">
SELECT COUNT(*) FROM tb_user
<where>
<include refid="query_user_where"/>
<!-- 这里可能还会引入其他的SQL片段 -->
</where>
</select>
<foreach>标签
SELECT * FROM tb_user WHERE user_id=2 OR user_id=4 OR user_id=5; -- 或者 SELECT * FROM tb_user WHERE user_id IN (2,4,5);
<foreach>标签属性说明
| 属性 | 备注 |
| index | 当迭代对象是数组,列表时,表示的是当前迭代的次数 |
| item | 当迭代对象是数组,列表时,表示的是当前迭代的元素 |
| collection | 当前遍历的对象 |
| open | 遍历的SQL以什么开头 |
| close | 遍历的SQL以什么结尾 |
| separator | 遍历完一次后,在末尾添加的字符等 |
例:使用MyBatis提供的<foreach>标签拼接SQL语句
实现查询多个用户编号信息
1.在Java包中类中
增加一个包含多个id信息的数组属性。
package com.java265.mybatis.po;
/**
* 用户信息参数类
* @author
**/
public class UserParam
{
private int userId; //用户ID
private String userName; //用户姓名
private String sex; //性别
private int[] ids; //多个用户ID
//省略getter与setter方法...
}
2.创建SQL片段,使用foreach标签,拼接or语句
<!-- 使用foreach标签,拼接or语句 -->
<sql id="query_user_or">
<if test="ids!=null and ids.length>0">
<foreach collection="ids" item="user_id" open="AND (" close=")" separator="OR">
user_id=#{user_id}
</foreach>
</if>
</sql>
3.创建SQL片段,使用foreach标签,拼接in语句。
<!-- 使用foreach标签,拼接in语句 -->
<sql id="query_user_in">
<if test="ids!=null and ids.length>0">
AND user_id IN
<foreach collection="ids" item="user_id" open="(" close=")" separator=",">
#{user_id}
</foreach>
</if>
</sql>
注意事项:
在SQL片段中“and”用来拼接已有一个或多个查询条件的语句
当此语句为第一个查询条件时
会因为<where>标签的存在而屏蔽第一个“and”
<choose>标签、<when>标签、<otherwise>标签
choose其效果类似于java的switch语句例:使用<choose>标签、<when>标签、<otherwise>标签,根据查询条件,获取用户信息。
<select id="queryUserChoose" parameterType="com.java265.mybatis.po.UserParam" resultType="com.java265.mybatis.po.User">
SELECT * FROM tb_user
<where>
<choose>
<when test="userId>0">
AND user_id = #{userId}
</when>
<when test="userName!=null and userName!=''">
AND user_name like '%${userName}%'
</when>
<otherwise>
AND sex = '女'
</otherwise>
</choose>
</where>
</select>
<trim>标签、<set>标签
<where>标签:
会在至少有一个子元素的条件返回SQL子句的情况下才去插入“WHERE”子句
当语句的开头为“AND”或“OR”,<where>标签也会将它们去除。
MyBatis还提供<trim>标签
可通过自定义<trim>标签来定制<where>标签的功能
如:和<where>标签等价的自定义 <trim>标签为:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
prefixOverrides 属性:
会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)
prefixOverrides 属性的功能:
移除所有指定在prefixOverrides属性中的内容
并且插入prefix属性中指定内容
例:使用自定义<trim>标签来定制<where>标签的功能,获取用户信息
<!-- 查询用户信息 -->
<select id="queryUserTrim" parameterType="com.java265.mybatis.po.UserParam" resultType="com.java265.mybatis.po.User">
SELECT * FROM tb_user
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<if test="userId>0">
and user_id = #{userId}
</if>
<if test="userName!=null and userName!=''">
and user_name like '%${userName}%'
</if>
<if test="sex!=null and sex!=''">
and sex = #{sex}
</if>
</trim>
</select>
例:在修改用户信息的SQL配置方法中,使用<set>标签过滤多余的逗号
<!-- 修改用户信息 -->
<update id="updateUser" parameterType="com.java265.mybatis.po.UserParam">
UPDATE tb_user
<set>
<if test="userName != null">user_name=#{userName},</if>
<if test="sex != null">sex=#{sex},</if>
<if test="age >0 ">age=#{age},</if>
<if test="blogUrl != null">blog_url=#{blogUrl}</if>
</set>
where user_id = #{userId}
</update>
<set>标签会动态前置SET关键字
同时也会删掉无关的逗号
因为用条件语句之后很可能就会在生成的SQL语句的后面留下这些逗号
因为用的是“if”元素,若最后一个“if”没有匹配上而前面的匹配上,SQL 语句的最后就会有一个逗号遗留
<trim prefix="SET" suffixOverrides=",">
...
</trim>
注意事项:
删去后缀值
同时添加前缀值
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


