在Mybatis中,Mapper XML文件是用于定义SQL语句和Java方法之间映射关系的核心配置文件。通过这些文件,开发者可以将数据库中的表与Java对象进行映射,实现数据的持久化操作。本文将详细介绍Mybatis映射文件的相关知识,包括其结构、标签以及如何编写和使用。
一、Mybatis映射文件概述
Mybatis是一个Java持久层框架,它提供了一种简单易用的方式来访问和操作数据库。在Mybatis中,映射文件(Mapper XML)起到了至关重要的作用,它们定义了SQL语句与Java方法之间的映射关系。
二、映射文件的结构
Mapper XML文件通常用于定义与数据库交互的SQL语句和操作。它的基本结构如下所示:
<?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="cn.tedu.tea.admin.server.content.dao.persist.mapper.TagMapper">
<!-- TagStandardVO getStandardById(Long id);-->
<select id="getStandardById" resultMap="StandardResultMap">
SELECT id,name,parent_id,enable,sort
FROM content_tag
WHERE id=#{id}
</select>
<resultMap id="StandardResultMap" type="cn.tedu.tea.admin.server.content.pojo.vo.TagStandardVO">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="parent_id" property="parentId"></result>
<result column="enable" property="enable"></result>
<result column="sort" property="sort"></result>
</resultMap>
</mapper>
Mapper XML文件详解
mapper元素是Mapper XML文件的根元素,它有一个namespace属性用于指定对应的Mapper接口或命名空间。
<mapper namespace="cn.tedu.tea.admin.server.content.dao.persist.mapper.TagMapper">
</mapper>
定义SQL语句和对应的操作:
: 用于执行查询操作。
: 用于执行插入操作。
: 用于执行更新操作。
: 用于执行删除操作。
每个标签有以下主要属性:
id: SQL语句的唯一标识符,可以通过这个id在Java代码中调用对应的SQL语句。
parameterType: SQL语句的参数类型,指定了传入SQL语句的参数类型。
resultType 或 resultMap: 如果是查询操作,可以通过resultType指定返回结果的类型,或者使用resultMap自定义结果映射规则。
查询操作
<select id="selectByType"
resultType="cn.tedu.baking.pojo.vo.ContentManagementVO">
SELECT c.id,
c.title,
c.img_url,
c.brief,
c.type,
cat.name categoryName,
c.view_count,
c.comment_count,
c.create_time
FROM t_content c
JOIN t_category cat ON c.category_id = cat.id
WHERE c.type = #{type}
AND c.create_by = #{id}
</select>
这个示例中,标签定义了一个查询操作,id为selectUserById,返回结果类型为User,SQL语句为SELECT * FROM users WHERE id = #{id}。#{id}是占位符,表示动态传入的参数。
插入操作
<insert id="insert">
INSERT INTO t_content
VALUES (NULL, #{title}, #{imgUrl}, #{videoUrl},
#{content}, #{type}, 0, 0, #{createBy},
#{createTime}, null, null,
#{brief}, #{categoryId})
</insert>
这个示例中,标签定义了一个插入操作,id为insertUser,参数类型为User,SQL语句为INSERT INTO users (username, password) VALUES (#{username}, #{password})。#{username}和#{password}分别表示User对象中的属性。
更新操作
<update id="updateUser" parameterType="User">
UPDATE users SET username = #{username}, password = #{password} WHERE id = #{id}
</update>
这个示例中,标签定义了一个更新操作,id为updateUser,参数类型为User,SQL语句为UPDATE users SET username = #{username}, password = #{password} WHERE id = #{id}。这里的#{id}、#{username}和#{password}都是User对象的属性。
删除操作
<delete id="deleteById">
DELETE
FROM t_content
WHERE id = #{id}
</delete>
这个示例中,标签定义了一个删除操作,id为deleteUser,SQL语句为DELETE FROM users WHERE id = #{id},其中#{id}是动态传入的参数。
动态SQL
MyBatis支持使用、、、等标签来构建动态SQL语句,根据条件动态生成SQL片段,提高SQL语句的灵活性和可重用性。
<update id="update">
UPDATE t_content
<set>
<if test="title!=null">title=#{title},</if>
<if test="imgUrl!=null">img_url=#{imgUrl},</if>
<if test="brief!=null">brief=#{brief},</if>
<if test="videoUrl!=null">video_url=#{videoUrl},</if>
<if test="type!=null">type=#{type},</if>
<if test="categoryId!=null">category_id=#{categoryId},</if>
<if test="viewCount!=null">view_count=#{viewCount},</if>
<if test="commentCount!=null">comment_count=#{commentCount},</if>
<if test="updateBy!=null">update_by=#{updateBy},</if>
<if test="updateTime!=null">update_time=#{updateTime},</if>
<if test="content!=null">content=#{content}</if>
</set>
WHERE id=#{id}
</update>
结果映射
除了简单的resultType属性外,还可以使用标签自定义复杂的结果映射关系,将数据库中的查询结果映射到Java对象的属性中。
<select id="getStandardById" resultMap="StandardResultMap">
SELECT id,name,parent_id,enable,sort
FROM content_tag
WHERE id=#{id}
</select>
<resultMap id="StandardResultMap" type="cn.tedu.tea.admin.server.content.pojo.vo.TagStandardVO">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="parent_id" property="parentId"></result>
<result column="enable" property="enable"></result>
<result column="sort" property="sort"></result>
</resultMap>