• MyBatis学习存档(3)——mapper.xml映射文件


    MyBatis 真正的强大在于映射语句,专注于SQL,功能强大,SQL映射的配置却是相当简单

    所以我们来看看映射文件的具体结构

    一、xml节点结构

    mapper为根节点 - namespace命名空间

    cache - 配置给定命名空间的缓存

    cache-ref – 从其他命名空间引用缓存配置

    resultMap –用来描述数据库结果集和对象的对应关系

    sql – 可以重用的SQL块,也可以被其他语句引用

    insert – 映射插入语句

    update – 映射更新语句

    delete – 映射删除语句

    select – 映射查询语句

    二、各节点作用

    此处以MyBatis学习存档(1)——入门中反向生成的UsersMapper.xml和UsersMapper.java为例

    <?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.mapper.UsersMapper" >
      <resultMap id="BaseResultMap" type="com.pojo.Users" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
      </resultMap>
      <sql id="Base_Column_List" >
        id, name, password
      </sql>
      <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select 
        <include refid="Base_Column_List" />
        from users
        where id = #{id,jdbcType=INTEGER}
      </select>
      <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from users
        where id = #{id,jdbcType=INTEGER}
      </delete>
      <insert id="insert" parameterType="com.pojo.Users" >
        insert into users (id, name, password
          )
        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
          )
      </insert>
      <insert id="insertSelective" parameterType="com.pojo.Users" >
        insert into users
        <trim prefix="(" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            id,
          </if>
          <if test="name != null" >
            name,
          </if>
          <if test="password != null" >
            password,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            #{id,jdbcType=INTEGER},
          </if>
          <if test="name != null" >
            #{name,jdbcType=VARCHAR},
          </if>
          <if test="password != null" >
            #{password,jdbcType=VARCHAR},
          </if>
        </trim>
      </insert>
      <update id="updateByPrimaryKeySelective" parameterType="com.pojo.Users" >
        update users
        <set >
          <if test="name != null" >
            name = #{name,jdbcType=VARCHAR},
          </if>
          <if test="password != null" >
            password = #{password,jdbcType=VARCHAR},
          </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
      </update>
      <update id="updateByPrimaryKey" parameterType="com.pojo.Users" >
        update users
        set name = #{name,jdbcType=VARCHAR},
          password = #{password,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
      </update>
    </mapper>
    UsersMapper.xml
    package com.mapper;
    
    import com.pojo.Users;
    
    public interface UsersMapper {
        int deleteByPrimaryKey(Integer id);
    
        int insert(Users record);
    
        int insertSelective(Users record);
    
        Users selectByPrimaryKey(Integer id);
    
        int updateByPrimaryKeySelective(Users record);
    
        int updateByPrimaryKey(Users record);
    }
    UsersMapper.java

    2.1 mapper节点

    作为xml的根节点,它具有一个很重要的属性——namespace(命名空间)

    namespace的值应与其所绑定的映射接口向一致,为保证其唯一性,通常为“包名+类名”,如com.mapper.UsersMapper,即绑定了com.mapper包下的名为UsersMapper的接口

    而与其绑定的接口UsersMapper中的方法应与映射文件中SQL语句id一一对应(顺序无需一致)

    2.2 cache、cache-ref节点

    不推荐使用MyBatis自带的缓存,因此此处不细讲

    2.3 resultMap节点

    可在sql映射语句中被引用,通常用于表示数据库表和实体类之间的映射关系

    resultMap节点有2个属性:

    id:标识作用,应具有唯一性

    type:类型,即对应的实体类,若在mybatis-config.xml中设置过别名,也可使用设置的别名

    id节点:对应数据库中的主键

    result节点:非主键的其他字段

    column:数据库表中对应的字段名

    property:实体类中对应的属性

    jdbcType:数据库中改字段的类型

    2.4 sql节点

    很简单的一个节点,用于sql语句的重用,其id属性起到标识作用,应具有唯一性

    2.5 select节点

    select是MyBatis中最常用的元素之一,对应select语句

    id:命名空间中唯一的标识符,接口中的方法与映射文件中的SQL语句id一一对应

    parameterType:传入SQL语句的参数类型,分为2种情况:

    1.基础数据类型

    int、String、Date等

    只能传入一个,通过#{参数名}即可获取传入的值

    2.复杂数据类型

    Java实体类、Map等

    通过#{属性名}或者#{map的keyName}即可获取传入值

    resultType:SQL语句返回值类型的完整类名或别名

    resultMap:引用配置的resultMap

    2.6 insert、delete、update节点

    分别对应添加、删除、更改语句,属性与select基本相同,唯一不同之处在于无需配置resultType或resultMap,本身默认返回受影响的行数

    因此,对于增删改这类更新操作,接口方法的返回值建议为int类型,执行sql影响的行数,最好不要写boolean类型

    三、多参数传入

    如2.5中所说,MyBatis仅支持单个参数的传入,那么如何进行多参数的传入呢?

    方法有2种:

    1.将多个参数封装成1个Map,传入Map即可,但该方法不透明,无法直接看出所需的参数是什么

       或直接传入一个实体类对象

    2.使用注解@Param来传入多个参数

    在接口中对传入参数进行注解@Param("value"),在映射文件中使用${value}即可获得该参数

    建议:通常传入参数大于3个时最好封装成一个对象,而不是一个个传入

  • 相关阅读:
    R12.2.4 ORA-01017: invalid username/password; logon denied
    VBA 判断一个TXT编码方式,再创建一个新的文件,复制数据进去
    查看功能所挂在的菜单
    EBS WebADI 存储过程增加参数
    用C++实现半透明按钮控件(PNG,GDI+)
    Linux五种IO模型性能分析
    用DirectX实现多视图渲染
    论YUV422(YUYV)与YUV420相互转换
    图文详解YUV420数据格式
    YUV422 YUV420 Planar Semi-Planar Interleaved YCbCr与YUV
  • 原文地址:https://www.cnblogs.com/s1165482267/p/8037785.html
Copyright © 2020-2023  润新知