Mybatis标签
xml模板
mapper 标签作为根标签,namespace 为该 xml 文件对应的 dao 接口的限定性类名。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lingyuan.mapper.ArticleMapper">
</mapper>
接口定义
每个 xml 文件对应一个接口,接口中的每个方法对应一个 select/updaet/delete 标签,方法名为标签的 id,多个入参时建议使用对象类型封装。
必须给接口的每个方法配置一条 sql 语句,但是并不是每条 sql 语句都要对应方法。
@Param
@Param
注解用于标记接口方法声明中的形式参数,用于重命名参数,在 xml 文件中使用重命名后的参数名。
警告
接口方法声明多个形式参数时必须重命名,否则 xml 中找不到参数。
resultMap
resultMap 标签在 mapper 标签下,定义实体类与数据库字段的映射关系,即一对一的对应关系。
<!-- type:该映射关系的实体类限定性类名,id:resultMap 的唯一标识 -->
<resultMap type="com.lingyuan.model.Article" id="BaseResultMap">
<!-- property:实体类字段名,column:数据库字段名,jdbcType:字段的 JDBC 类型 -->
<result property="articleId" column="article_id" jdbcType="BIGINT" />
<result property="articleTitle" column="article_title" jdbcType="VARCHAR" />
<result property="articleAuthor" column="article_author" jdbcType="VARCHAR" />
<result property="articleContent" column="article_content" jdbcType="TEXT" />
<result property="createBy" column="create_by" jdbcType="BIGINT" />
<result property="createByName" column="create_by_name" jdbcType="VARCHAR" />
<result property="createTime" column="create_time" jdbcType="DATETIME" />
<result property="updateBy" column="update_by" jdbcType="BIGINT" />
<result property="updateByName" column="update_by_name" jdbcType="VARCHAR" />
<result property="updateTime" column="update_time" jdbcType="DATETIME" />
</resultMap>
sql
用于定义一段自定义 SQL,在其他标签中使用 include 标签引用。
<sql id="Base_Column_List">
article_id,
article_title,
article_author,
article_content,
create_by,
create_by_name,
create_time,
update_by,
update_by_name,
update_time
</sql>
select & include
select 标签定义在 mapper 标签下,定义一条 DQL,数据库查询语句。
<!-- id:该标签的 SQL 对应接口中的方法名,resultMap:使用的映射关系 -->
<select id="selectByPrimaryKey" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List" />
FROM article
WHERE article_id = #{articleId, jdbcType=BIGINT}
</select>
where & if
where 标签用于 select/update/delete 标签中,在条件判断中拼接 where 条件时,Mybatis 自动处理:添加或删除 where 关键字,删除多余的 and 关键字。
if 标签用于做条件判断,test 属性中的值为 true 时才渲染标签中的值。
<!-- 通过实体类查询 -->
<select id="selectByModel" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List" />
FROM article
<where>
<if test="articleId != null">
article_id = #{articleId, jdbcType=BIGINT}
</if>
<if test="articleTitle != null and articleTitle != ''">
article_title = #{articleTitle, jdbcType=VARCHAR}
</if>
<if test="articleAuthor != null and articleAuthor != ''">
article_author = #{articleAuthor, jdbcType=VARCHAR}
</if>
<if test="articleContent != null and articleContent != ''">
article_content = #{articleContent, jdbcType=TEXT}
</if>
<if test="createBy != null">
create_by = #{createBy, jdbcType=BIGINT}
</if>
<if test="createByName != null and createByName != ''">
create_by_name = #{createByName, jdbcType=VARCHAR}
</if>
<if test="createTime != null">
create_time = #{createTime, jdbcType=DATETIME}
</if>
<if test="updateBy != null">
update_by = #{updateBy, jdbcType=BIGINT}
</if>
<if test="updateByName != null and updateByName != ''">
update_by_name = #{updateByName, jdbcType=VARCHAR}
</if>
<if test="updateTime != null">
update_time = #{updateTime, jdbcType=DATETIME}
</if>
</where>
</select>
update & set
update 标签用于定义一条修改语句。
set 标签用于 update 标签中,在条件判断中拼接 set 字段时,Mybatis 自动处理:添加 set 关键字,删除多余的 ,
.
<!-- 按选择更新 -->
<update id="updateBySelective">
UPDATE article
<set>
<if test="articleId != null" >
article_id = #{articleId, jdbcType=BIGINT},
</if>
<if test="articleTitle != null and articleTitle != ''" >
article_title = #{articleTitle, jdbcType=VARCHAR},
</if>
<if test="articleAuthor != null and articleAuthor != ''" >
article_author = #{articleAuthor, jdbcType=VARCHAR},
</if>
<if test="articleContent != null and articleContent != ''" >
article_content = #{articleContent, jdbcType=TEXT},
</if>
<if test="createBy != null" >
create_by = #{createBy, jdbcType=BIGINT},
</if>
<if test="createByName != null and createByName != ''" >
create_by_name = #{createByName, jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
create_time = #{createTime, jdbcType=DATETIME},
</if>
<if test="updateBy != null" >
update_by = #{updateBy, jdbcType=BIGINT},
</if>
<if test="updateByName != null and updateByName != ''" >
update_by_name = #{updateByName, jdbcType=VARCHAR},
</if>
<if test="updateTime != null" >
update_time = #{updateTime, jdbcType=DATETIME},
</if>
</set>
WHERE article_id = #{articleId, jdbcType=BIGINT}
</update>
delete
delete 标签用于定义一条删除语句。
<!-- 删除 -->
<delete id="delete">
DELETE FROM article WHERE article_id = #{articleId, jdbcType=BIGINT}
</delete>
trim
用于拼接动态SQL语句,是未封装版的 where 和 set 标签。
参数作用:
prefix
:拼接的前缀。suffix
:拼接的后缀。suffixOverrides
:自动去掉多余的某个符号。
<!--修改员工信息,姓名,上级领导,工资,入职时间,奖金,部门编号,岗位,头像-->
<update id="updateEmpByEmpNo" parameterType="emp">
update emp
<trim prefix="set" suffixOverrides=",">
<if test="ename != null and ename != ''">
ename = #{ename},
</if>
<if test="sal != null ">
sal = #{sal},
</if>
</trim>
where empno = #{empNo}
</update>
转义标签
用于转义 Mybatis xml 中的关键字或标签,常见于小于号(左标签)的转义。
sal <![CDATA[ <= ]]> #{sal},
级联查询
当返回值存在其他对象类型(一对一或一对多)需要使用其他 SQL 另行查询时,可以在 resultMap 映射关系中配置级联查询。
:::warnig 级联查询为一对多关系(List)时本质还是 for 循环执行 SQL,性能并不好。 :::
<mapper namespace="com.ly.dao.UserDao">
<!-- 自定义映射关系 -->
<resultMap type="com.ly.pojo.User" id="userMap">
<!-- id 标签专门用来映射主键 -->
<id column="id" property="id"/>
<result column="name" property="u_name"/>
<result column="pwd" property="u_pwd"/>
<result column="age" property="age"/>
<result column="flag" property="flag"/>
<!-- 级联查询 User 对象里面的 Role 对象,一对一
property:级联查询出来的对象设置给 User 里面的哪个属性
column:进行级联查询需要通过 t_user 里面的哪个字段进行下一步查询
javaType:查询出来的对象类型
select:需要执行哪条 sql 语句
-->
<association property="role" column="role_id" javaType="com.ly.pojo.Role" select="com.ly.dao.RoleDao.queryUserByRoleId"/>
</resultMap>
<select id="login" parameterType="com.ly.pojo.User" resultMap="userMap">
select * from t_user where name= #{u_name} and pwd=#{u_pwd}
</select>
</mapper>
<mapper namespace="com.ly.dao.RoleDao">
<resultMap type="com.ly.pojo.Role" id="roleMap">
<id column="id" property="r_id"/>
<result column="name" property="r_name"/>
<!--
级联查询 Role 对象里面的 powers 对象,一对多
property:查询出来的 List 集合设置给该对象的哪个属性
column:级联查询时通过哪个字段值从 Power 表中查询
ofType:List 集合中的数据类型
select:引用哪条 sql 语句
-->
<collection property="powers" column="id" ofType="com.ly.pojo.Power" select="com.ly.dao.PowerDao.queryPowerByRoleId"></collection>
</resultMap>
<!-- 被 userMap 中级联查询引用的方法 -->
<select id="queryUserByRoleId" parameterType="int" resultMap="roleMap">
select * from t_role where id = #{role_id}
</select>
</mapper>
<mapper namespace="com.ly.dao.PowerDao">
<!-- 被 roleMap 中引用的方法 -->
<select id="queryPowerByRoleId" parameterType="int" resultType="com.ly.pojo.Power">
select tp.id p_id,tp.name p_name,tp.url p_url
from t_power tp,t_role_power trp
where tp.id=trp.role_id and trp.role_id=#{id}
</select>
</mapper>