目录
一. sql 代码片段标签
二. resultMap 映射结果集标签
三. where 条件标签
四. set 修改标签
五. trim 标签
六. foreach 循环标签
一. sql 代码片段标签
sql 标签是 mybatis 框架中一个非常常用的标签页,特别是当一张表很有多个字段多,或者要多表查询很多字段时,都会用到。拓本之际是用来定义一个SQL代码片段,然后可以在其它SQL语句中引入此片段。
举例:如下图,m_user 表中有多个字段。
定义方法:将SQL代码片段写在标签内,并定义唯一id,如下
<sql id="User_Column">id,username,salt,phone,avatar,email,`status`,created,last_login</sql>
引用方式:<include refid="被引用的代码片段ID">
select<include refid="User_Column" />from m_user
好处:在 mapper.xml 文件中,通常会定义大量的增删改查语句,当我们定义一个SQL片段时,就可以在整个 mapper.xml 文件中进行引用,简化了后续的开发,省去了很多不必要的写字段的时间和精力。
此外,当我们后续要对表添加修改字段时,基本上只需要对SQL片段内部的字段做增减,然后所有引用此SQL片段的SQL语句都会跟着改变,非常方便。
二. resultMap 映射结果集标签
数据库字段与Java代码之前的映射关系是我们经常需要注意的一个问题,例如数据库字段名称为 create_time,但是在Java代码中,通常采用驼峰的方式定义为 createTime,这就会导致字段映射失败,而 resultMap 就是来解决这个问题的。
使用方法:以上述 m_user 表为例
last_login 字段在Java代码中通常定义为 lastLogin;
定义方式: mapper.xml 文件中,在 resultMap 标签中这样写,定义为一 id ,type 就是映射的 Java 实体类,即 User 类。
<!-- 通用查询映射结果 --><resultMap id="UserResultMap" type="com.markerhub.entity.User"><id column="id" property="id" /><result column="username" property="username" /><result column="avatar" property="avatar" /><result column="email" property="email" /><result column="password" property="password" /><result column="status" property="status" /><result column="created" property="created" /><result column="last_login" property="lastLogin" /></resultMap>
使用方式:以 select 查询语句为例,当查询返回值 resultMap 中全部都有时,就可以通过 resultMap="id值" 来进行引用,当查询数据库时,mybatis 会自动把数据库字段的值映射到我们定义的Java属性值上。
<select id="selectByUserName" resultMap="UserResultMap">select <include refid="User_Column"/>from m_user where username = #{username}</select>
三. where 条件标签
条件 where 语句,用于动态 SQL 时使用,在查询数据库时,我们不确定用户是否传入值,如果有值,就将条件加入到 where ,如果没有传值,则不加。
使用方法:将 where条件SQL定义在<where>标签内部,当 <where> 标签内部不为空时,where 标签就会生效。如果<where> 标签内部为空,则不会将where条件语句拼接到SQL语句内。
<if>标签也是同样的道理,test ="id != null" 表示如果 id 值不为空,则将 "and id = #{id}"SQL片段拼接到SQL语句中。
<select id="selectByIdAndUserName" resultMap="UserResultMap">select <include refid="User_Column"/>from m_user w<where><if test="id != null" >and id = #{id}</if><if test="username != null">and username = #{username}</if></where></select>
四. set 修改标签
条件 set 语句,和 where 标签是一个道理。
示例代码如下,<set> 标签 搭配 <if> 标签,就可以达到当传入的值不为空时,就更新值的效果。
<update id="updateUserById" parameterType="com.markerhub.entity.User">update m_user<set><if test="username != null">username = #{username},</if><if test="avatar != null">avatar = #{avatar},</if><if test="email != null">email = #{email},</if><if test="password != null">password = #{password},</if><if test="status != null">status = #{status},</if><if test="created != null">created = #{created},</if><if test="lastLogin != null">last_login = #{lastLogin},</if></set>where id = #{id}</update>
五. trim 标签
trim 标签是一个单独的标签,一共提供了四个属性,如下图所示。
prtefix/suffix:在trim标签中内容前面/后面添加指定内容;
prefixOverrides/suffixOverrides:在trim标签中内容前面/后面去掉指定内容;
完整的SQL语句
这样写表示当 <if> 标签内容不为空时,添加 where 过滤条件,并且将 <if> 标签内不多余的后缀关键词 and 去掉防止语法错误。
<select id="selectUserByIdAndUserName" parameterType="com.markerhub.entity.User">select <include refid="User_Column"/>from m_user<trim prefix="where" suffixOverrides="and"><if test="id != null and id != ''">id = #{id} and</if><if test="username != null and username != ''">username = #{username} and</if></trim></select>
六. foreach 循环标签
foreach 就是用来循环的语句,通常出现在按照列表数据批量添加或批量删除数据时使用,它提供了五个属性;
cllection:代表要遍历的数组,Java接口在定义是最好使用@Parm进行标注;
item:表示数组中的每个元素;
open:表示该标签体以什么开头;
close :表示该标签体以什么结尾;
separator:表示数据之间的分隔符是什么;
示例代码:
这段代码的意思就是参数传递一个ID集合,集合不为空时,where 过滤条件按照 id 值循环删除所有符合条件的数据。
<delete id="deleteUserByIds" parameterType="arraylist">delete from m_user<where>id in<foreach collection="array" item="id" open="(" separator="," close=")">#{id}</foreach></where></delete>