对Mapper.xml文件进行深入的学习

1.  前言

既上次在Mapper.xml文件出现bug之后,痛改前非,决定吃透Mapper.xml映射文件。

让我们通过具体的代码段来进一步理解 MyBatis 的 Mapper XML 文件中的每个组成部分。

<?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.goblin.BIbackend.mapper.BillsMapper">

    <resultMap id="BaseResultMap" type="com.goblin.BIbackend.model.entity.Bills">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="userId" column="user_id" jdbcType="BIGINT"/>
            <result property="amount" column="Amount" jdbcType="FLOAT"/>
            <result property="billDate" column="bill_date" jdbcType="DATE"/>
            <result property="dueDate" column="due_date" jdbcType="DATE"/>
            <result property="isPaid" column="is_paid" jdbcType="BIGINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,user_id,Amount,
        bill_date,due_date,is_paid
    </sql>

    <select id="list" resultType="com.goblin.BIbackend.model.entity.Bills">
            select * from bills
            <where>
                <if test="userId != null">
                    user_id = #{userId}
                </if>
                <if test="amount != null">
                    and Amount = #{amount}
                </if>
                <if test="billDate != null">
                    and bill_date = #{billDate}
                </if>
                <if test="dueDate != null">
                    and due_date = #{dueDate}
                </if>
                <if test="isPaid != null">
                    and is_paid = #{isPaid}
                </if>
            </where>
    </select>

    <select id="mySelectById" resultType="com.goblin.BIbackend.model.entity.Bills">
        select * from bills where id = #{id}
    </select>

    <insert id="myInsert" parameterType="com.goblin.BIbackend.model.entity.Bills">
            insert into bills (user_id,Amount,bill_date,due_date,is_paid)
            values (#{userId},#{amount},#{billDate},#{dueDate},#{isPaid})
    </insert>

    <delete id="myDeleteById" >
        delete from bills where id = #{id}
    </delete>

    <select id="mySelectAll" resultMap="BaseResultMap">
            select * from bills
    </select>

    <update id="myUpdate" parameterType="com.goblin.BIbackend.model.entity.Bills">
            update bills where id = #{id}
    </update>

</mapper>

2.  各部分

2.1  <mapper>根元素

<mapper namespace="com.goblin.BIbackend.mapper.BillsMapper">
    <!-- 其他配置 -->
</mapper>

这个根元素定义了 XML 文件的命名空间,通常对应 Java 中的 Mapper 接口的完全限定名。这有助于区分不同的 Mapper 文件,防止 SQL 语句冲突。

2.2  <resultMap> 和字段映射

<resultMap id="BaseResultMap" type="com.goblin.BIbackend.model.entity.Bills">
    <id property="id" column="id" jdbcType="BIGINT"/>
    <result property="userId" column="user_id" jdbcType="BIGINT"/>
    <result property="amount" column="Amount" jdbcType="FLOAT"/>
    <result property="billDate" column="bill_date" jdbcType="DATE"/>
    <result property="dueDate" column="due_date" jdbcType="DATE"/>
    <result property="isPaid" column="is_paid" jdbcType="BIGINT"/>
</resultMap>

这里定义了一个名为 BaseResultMap 的结果映射它将数据库查询结果映射到 com.goblin.BIbackend.model.entity.Bills 类的实例。

具体映射关系如下:

1. id属性对应数据库表的id列,数据类型为BIGINT。
2. userId属性对应数据库表的user_id列,数据类型为BIGINT。
3. amount属性对应数据库表的Amount列,数据类型为FLOAT。
4. billDate属性对应数据库表的bill_date列,数据类型为DATE。
5. dueDate属性对应数据库表的due_date列,数据类型为DATE。
6. isPaid属性对应数据库表的is_paid列,数据类型为BIGINT。

前面是实体类里面定义的名称,后面是数据库里面对应的字段名。 

2.3  <sql> 片段

<sql id="Base_Column_List">
    id, user_id, Amount, bill_date, due_date, is_paid
</sql>
  • id: 为 SQL 片段定义一个标识符,可以在 <select> 或其他元素中通过 <include> 引用。
  • 内容: 列出了查询操作中需要的列名。这是为了避免在多个 <select> 元素中重复相同的列名列表。

2.4  增删改查SQL语句

<select id="list" resultType="com.goblin.BIbackend.model.entity.Bills">
            select * from bills
            <where>
                <if test="userId != null">
                    user_id = #{userId}
                </if>
                <if test="amount != null">
                    and Amount = #{amount}
                </if>
                <if test="billDate != null">
                    and bill_date = #{billDate}
                </if>
                <if test="dueDate != null">
                    and due_date = #{dueDate}
                </if>
                <if test="isPaid != null">
                    and is_paid = #{isPaid}
                </if>
            </where>
    </select>

    <select id="mySelectById" resultType="com.goblin.BIbackend.model.entity.Bills">
        select * from bills where id = #{id}
    </select>

    <insert id="myInsert" parameterType="com.goblin.BIbackend.model.entity.Bills">
            insert into bills (user_id,Amount,bill_date,due_date,is_paid)
            values (#{userId},#{amount},#{billDate},#{dueDate},#{isPaid})
    </insert>

    <delete id="myDeleteById" >
        delete from bills where id = #{id}
    </delete>

    <update id="myUpdate" parameterType="com.goblin.BIbackend.model.entity.Bills">
            update bills where id = #{id}
    </update>

parameterTyperesultType 在 SQL 映射文件中用于指定参数和结果的 Java 类型,但它们的作用和使用场景不同。 一定不能写错,否则测试接口的时候就错乱了。

2.4.1  resultType

  • 作用resultType 用于指定 MyBatis 查询操作返回的结果类型。这个属性告诉 MyBatis 应该如何将查询结果集的每一行映射到 Java 对象。
  • 使用场景:通常用在 <select> 元素中,用于定义查询结果应该如何映射到 Java 类的实例。
  • 示例:
<select id="selectBlog" resultType="Blog">
    select * from Blog
</select>

在这个例子中,resultType 指定了查询结果应该映射到 Blog 类的实例。

2.4.2  parameterType

  • 作用parameterType 用于指定 MyBatis 操作(如 <insert><update><delete> 或带有参数的 <select>)中传入的参数类型。这个属性告诉 MyBatis 期待的参数对象的类型,MyBatis 会根据这个类型来自动映射方法参数和 SQL 语句中的占位符。
  • 使用场景:通常用在需要传入参数执行数据库操作的元素中,例如插入、更新或删除操作。
  • 示例
<select id="selectById" parameterType="java.util.Map" resultType="com.example.Blog">
    select * from blog where id = #{id}
</select>

在这个例子中,parameterType 指定了方法的参数类型为 java.util.Map,这意味着你可以传递一个 Map 对象作为查询参数。

2.4.3  两者区别总结 

  • 方向parameterType 定义了进入操作的参数类型,而 resultType 定义了从数据库查询返回的结果类型。
  • 使用位置parameterType 通常用在需要参数的 SQL 操作中,resultType 则用在查询操作中。
  • 映射方式parameterType 映射的是方法的参数到 SQL 语句的占位符,resultType 映射的是查询结果集到 Java 对象的属性。

 2.4.5  占位符#{id}

在 SQL 映射文件中编写 SQL 语句时,可以使用 #{} 来引用 Java 方法的参数。

<select id="selectBlog" resultType="Blog">
    SELECT * FROM blog WHERE id = #{id}
</select>

在这个例子中,#{id} 就是一个占位符,它将被 MyBatis 替换为方法参数 id 的值。

参数替换

MyBatis 会根据方法的参数列表自动替换 #{} 中的内容。如果方法的参数是一个简单类型或 POJO(Plain Old Java Object),MyBatis 会根据参数的名称来替换占位符。

预处理语句

使用 #{} 占位符的 SQL 语句会被 MyBatis 转换为预处理语句(PreparedStatement),这样可以提高性能并防止 SQL 注入攻击。

类型处理

MyBatis 会根据参数的实际类型来设置 SQL 语句中的参数。例如,如果参数是 java.sql.Date 类型,MyBatis 会使用适当的 JDBC 方法来设置日期参数。

占位符总结

#{} 占位符是 MyBatis 中实现参数化查询的关键机制,它允许开发者将 Java 方法的参数值安全、灵活地传递给 SQL 语句。通过使用 #{},MyBatis 可以自动处理参数的类型转换和预处理语句的生成,从而提高数据库操作的效率和安全性。

相关推荐

  1. Mapper.xml文件进行深入学习

    2024-07-10 05:32:06       32 阅读
  2. C#如何进行深度学习行人进行识别?

    2024-07-10 05:32:06       17 阅读
  3. 使用深度学习网络摄像头图像进行分类

    2024-07-10 05:32:06       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 05:32:06       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 05:32:06       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 05:32:06       42 阅读
  4. Python语言-面向对象

    2024-07-10 05:32:06       53 阅读

热门阅读

  1. 工作中遇到的问题与解决办法(三)

    2024-07-10 05:32:06       19 阅读
  2. linux工具应用_VERDI

    2024-07-10 05:32:06       26 阅读
  3. 大模型/NLP/算法面试题总结4——bert参数量计算

    2024-07-10 05:32:06       26 阅读
  4. springsecurity(学习自用)

    2024-07-10 05:32:06       25 阅读
  5. 构建响应式CSS导航栏:实现优雅的用户体验

    2024-07-10 05:32:06       21 阅读
  6. debian或Ubuntu中开启ssh允许root远程ssh登录的方法

    2024-07-10 05:32:06       23 阅读
  7. 深入理解基本数据结构:链表详解

    2024-07-10 05:32:06       23 阅读
  8. 白骑士的C++教学基础篇 1.3 控制流

    2024-07-10 05:32:06       22 阅读
  9. Istio实战教程:Service Mesh部署与流量管理

    2024-07-10 05:32:06       22 阅读