mybatis - [04] mapper文件详解

  Mybatis的Mapper文件(通常是以.xml为扩展名的文件)主要用于定义SQL语句和它们与Java接口方法之间的映射关系。以下是Mapper文件中一些常用的配置元素和属性。

 

一、mapper文件配置详解

(1)namespace:定义Mapper接口对应的唯一命名空间,用于区分不同的Mapper。(常以UserMapper类的全限定名命名)

<mapper namespace="com.example.mapper.UserMapper">
    ...
</mapper>

(2)<select>|<insert>|<update>|<delete>:分别对应SQL查询、插入、更新和删除操作。

<!--
    findById: 是mapper类中的接口方法
    parameterType: 是sql中的参数数据类型
    resultType: 是执行sql的返回结果,因为id唯一,返回的是一条记录。而数据库中一条记录就是Java中的一个实体类对象。
    所以resultType填写的是对应实体类的全限定名。
-->
<select id="findById" parameterType="int" resultType="com.example.domain.User">
    select * from user where id = #{id}
</select>


<!--
    insert: 是mapper类中的接口方法
    parameterType: 向数据库中插入的一条记录就是一个实体类对象的数据。所以是实体类的全限定名
    jdbcType=INTEGER这类语法是用来明确指定传入参数在数据库中的类型映射
    大多数情况下,Mybatis能够根据参数的Java类型自动推断出相应的JDBC类型。
    但是,在某些情况下,特别是当参数为null或者类型推断不确定时,显示指定jdbcType可以帮助Mybatis更精确地执行类型转换,避免类型不匹配的错误。
-->
<insert id="insert" parameterType="com.example.domain.User">
    insert into User (id,username,password) 
    values (#{id,jdbcType=INTEGER},#{username,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR})
</insert>

<!--
    以下是一个动态的可选的insert
    prefix,suffix: 使用小括号()将<trim>标签中所有属性括起来
    suffixOverrides: 属性之间逗号隔开
-->
<insert id="insertSelective" parameterType="com.example.domain.User">
    insert into User (id,username,password) 
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">
            id,
        </if>
        <if test="username != null">
            username,
        </if>
        <if test="password != null">
            password,
        </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id != null">
            #{id,jdbcType=INTEGER},
        </if>
        <if test="username != null">
            #{username,jdbcType=VARCHAR},
        </if>
        <if test="password != null">
            #{password,jdbcType=VARCHAR},
        </if>
    </trim>
</insert>


<!--
    更新操作中,修改指定字段的其他属性,set和where的字段都需要赋值,此时paramterType使用实体类的全限定名。
-->
<update id="updateByKeySelective" paramterType="com.example.domain.User">
    update user
    <set>
        <if test="username != null">
            username = #{username,jdbcType=VARCHAR}
        </if>
        <if test="password != null">
            password = #{password,jdbcType=VARCHAR}
        </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
</update>


<!--
    parameterType: 需要删除指定id的记录,所以parameterType是id的数据类型: java.lang.Integer
-->
<delete id="deleteById" parameterType="java.lang.Integer">
    delete from User
    where if = #{id,jdbcType=INTEGER}
</delete>

(3)<resultMap>:复杂结果集的映射,定义如何将查询结果映射到Java对象的属性上。

<resultMap id="UserResultMap" type="com.example.domain.User">
    <id property="id" column="user_id"/>
    <result property="name" column="username"/>
    <association property="address" javaType="com.example.domain.Address">
        <id property="addressId" column="address_id"/>
        <result property="city" column="city"/>
    </association>
</resultMap>

属性:

① id:resultMap的唯一标识

② type:映射的目标对象类型

③ <id>:主键字段映射

④ <result>:普通字段映射

⑤ <association>:关联对象映射

⑥ <collection>:集合属性映射

(4)<sql>(可选):定义可重用的SQL片段,提高代码复用性

<sql id="userColumns">id, username, password</sql>

(5)<include>(结合<sql>使用)

<select id="findAllUsers" resultType="com.example.domain.User">
    SELECT
        <include refid="userColumns"/>
    FROM user
</select>

(6)cache(可选):配置缓存策略,提高查询效率。

<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/>

 

 

 

二、mapper文件配置示例

2.1. 实体类和数据库的表字段映射配置(应对实体类属性和表字段命名不统一场景)

<resultMap id="BaseResultMap" type="com.example.domain.User" >
   <id column="id" property="id" jdbcType="INTEGER" />
   <result column="user_name" property="username" jdbcType="VARCHAR" />
   <result column="pass_word" property="password" jdbcType="VARCHAR" />
   <result column="address" property="address" jdbcType="VARCHAR" />
   <result column="birth_day" property="birthday" jdbcType="TIMESTAMP" />
</resultMap>

type:是对应实体类的全限定名;

column="user_name":对应数据库中的字段;

property="username":对应的是实体类属性;

jdbcType="VARCHAR":是其在数据库中字段的数据类型。

 

 

 

— 业精于勤荒于嬉,行成于思毁于随 —

posted @ 2024-05-27 15:41  HOUHUILIN  阅读(116)  评论(0编辑  收藏  举报