Mybatis映射文件详解-mapper.xml文件

在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>
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客编程坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值