MyBatis in resultType and parameterType and resultMap summary of usage and differences

resultType and parameterType use of basic differences:

1, using resultType: primarily directed to the respective data extracted from the database

2, using parameterType: primarily directed to the stored information in a database, such as: insert zhong Update increase data to the database and the like

resultType package type sql statement is a query result set, that is to say the package sql query results in the bean return back, it is stored in the data. paramType is taken from the data transmission over the Bean insert values ​​into, for example, in the statement when the argument used, the data is taken.

MyBatis difference in resultType and the resultMap

ResultType and resultMap similar function, object information is returned, but resultMap to be more powerful, customizable. Because resultMap To configure it, one to one relationship between the tables and the like, so that even if the property name your field and your name is not the same entity class does not matter, will give you mapped out, however, resultType relatively tasteless, must field the same name, for example cId and c_id that are not mapped. Here are a few common mapping relationship:

Table 1. Single query: resultMap: When using the SQL statement returns do resultMap processing result type, generally need to define the correspondence resultMap pojo and the corresponding field in the table in mapper.xml.
<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">
    <!-- 配置映射的订单信息 -->
    <!-- id:指定查询列中的唯 一标识,订单信息的中的唯 一标识,如果有多个列组成唯一标识,配置多个id
        column:订单信息的唯 一标识 列
        property:订单信息的唯 一标识 列所映射到Orders中哪个属性
      -->
    <id column="id" property="id"/>
    <result column="user_id" property="userId"/>
    <result column="number" property="number"/>
    <result column="createtime" property="createtime"/>
    <result column="note" property="note"/>        
</resultMap>
2. relational query (one to one): resultMap is generally carried out for another add another table in the nested pojo pojo primary table, then use the association node elements mapper.xml for handling one connection table a connection processing table. E.g
<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">
    <!-- 配置映射的订单信息 -->
    <!-- id:指定查询列中的唯 一标识,订单信息的中的唯 一标识,如果有多个列组成唯一标识,配置多个id
        column:订单信息的唯 一标识 列
        property:订单信息的唯 一标识 列所映射到Orders中哪个属性
      -->
    <id column="id" property="id"/>
    <result column="user_id" property="userId"/>
    <result column="number" property="number"/>
    <result column="createtime" property="createtime"/>
    <result column="note" property=note/>
    
    <!-- 配置映射的关联的用户信息 -->
    <!-- association:用于映射关联查询单个对象的信息
    property:要将关联查询的用户信息映射到Orders中哪个属性
     -->
    <association property="user"  javaType="cn.itcast.mybatis.po.User">
        <!-- id:关联查询用户的唯 一标识
        column:指定唯 一标识用户信息的列
        javaType:映射到user的哪个属性
         -->
        <id column="user_id" property="id"/>
        <result column="username" property="username"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>
    
    </association>
</resultMap>
3. relational query (many): The resultMap processing mode add a list in order pojo table data, schedule list of attributes for the order, the following processing is employed in a manner in mapper.xml:

Orders and order details resultMap use extends inheritance, do not configure the order information and user information in the mapping

<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersAndOrderDetailResultMap" extends="OrdersUserResultMap">
	 <!- 订单信息 -->
    <!- 用户信息 -->
    <!- 使用extends继承,不用在中配置订单信息和用户信息的映射 -->
    <!- 订单明细信息
    一个订单关联查询出了多条明细,要使用collection进行映射
    collection:对关联查询到多条记录映射到集合对象中
    property:将关联查询到多条记录映射到cn.itcast.mybatis.po.Orders哪个属性
    ofType:指定映射到list集合属性中pojo的类型
     -->
     <collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail">
         <!-- id:订单明细唯 一标识
         property:要将订单明细的唯 一标识 映射到cn.itcast.mybatis.po.Orderdetail的哪个属性
           -->
         <id column="orderdetail_id" property="id"/>
         <result column="items_id" property="itemsId"/>
         <result column="items_num" property="itemsNum"/>
         <result column="orders_id" property="ordersId"/>
     </collection>
    

</resultMap>
Sample student table (CRUD)
 <?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.qcby.dao.StudentMapper">
  <resultMap id="BaseResultMap" type="com.qcby.entity.Student">
    <id column="Sid" jdbcType="INTEGER" property="sid" />
    <result column="Snum" jdbcType="VARCHAR" property="snum" />
    <result column="Spassword" jdbcType="VARCHAR" property="spassword" />
    <result column="Sname" jdbcType="VARCHAR" property="sname" />
    <result column="Ssex" jdbcType="VARCHAR" property="ssex" />
    <result column="Sgrade" jdbcType="VARCHAR" property="sgrade" />
    <result column="Smajor" jdbcType="VARCHAR" property="smajor" />
  </resultMap>
  <sql id="Base_Column_List">
    Sid, Snum, Spassword, Sname, Ssex, Sgrade, Smajor
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from student
    where Sid = #{sid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from student
    where Sid = #{sid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.qcby.entity.Student">
    insert into student (Sid, Snum, Spassword,
      Sname, Ssex, Sgrade, 
      Smajor)
    values (#{sid,jdbcType=INTEGER}, #{snum,jdbcType=VARCHAR}, #{spassword,jdbcType=VARCHAR}, 
      #{sname,jdbcType=VARCHAR}, #{ssex,jdbcType=VARCHAR}, #{sgrade,jdbcType=VARCHAR}, 
      #{smajor,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.qcby.entity.Student">
    insert into student
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="sid != null">
        Sid,
      </if>
      <if test="snum != null">
        Snum,
      </if>
      <if test="spassword != null">
        Spassword,
      </if>
      <if test="sname != null">
        Sname,
      </if>
      <if test="ssex != null">
        Ssex,
      </if>
      <if test="sgrade != null">
        Sgrade,
      </if>
      <if test="smajor != null">
        Smajor,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="sid != null">
        #{sid,jdbcType=INTEGER},
      </if>
      <if test="snum != null">
        #{snum,jdbcType=VARCHAR},
      </if>
      <if test="spassword != null">
        #{spassword,jdbcType=VARCHAR},
      </if>
      <if test="sname != null">
        #{sname,jdbcType=VARCHAR},
      </if>
      <if test="ssex != null">
        #{ssex,jdbcType=VARCHAR},
      </if>
      <if test="sgrade != null">
        #{sgrade,jdbcType=VARCHAR},
      </if>
      <if test="smajor != null">
        #{smajor,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.qcby.entity.Student">
    update student
    <set>
      <if test="snum != null">
        Snum = #{snum,jdbcType=VARCHAR},
      </if>
      <if test="spassword != null">
        Spassword = #{spassword,jdbcType=VARCHAR},
      </if>
      <if test="sname != null">
        Sname = #{sname,jdbcType=VARCHAR},
      </if>
      <if test="ssex != null">
        Ssex = #{ssex,jdbcType=VARCHAR},
      </if>
      <if test="sgrade != null">
        Sgrade = #{sgrade,jdbcType=VARCHAR},
      </if>
      <if test="smajor != null">
        Smajor = #{smajor,jdbcType=VARCHAR},
      </if>
    </set>
    where Sid = #{sid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.qcby.entity.Student">
    update student
    set Snum = #{snum,jdbcType=VARCHAR},
      Spassword = #{spassword,jdbcType=VARCHAR},
      Sname = #{sname,jdbcType=VARCHAR},
      Ssex = #{ssex,jdbcType=VARCHAR},
      Sgrade = #{sgrade,jdbcType=VARCHAR},
      Smajor = #{smajor,jdbcType=VARCHAR}
    where Sid = #{sid,jdbcType=INTEGER}
  </update>


  <select id="stushow"  resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from student
  </select>

  <select id="selectStuBySnum" parameterType="java.lang.String" resultType="com.qcby.entity.Student">
    select
    <include refid="Base_Column_List" />
    from student
    where Snum = #{Snum,jdbcType=VARCHAR}
  </select>
  <select id="selectStuLogin" parameterType="com.qcby.entity.Student" resultType="com.qcby.entity.Student">
    select
    <include refid="Base_Column_List" />
    from student
    where Snum = #{snum,jdbcType=VARCHAR}
    AND Spassword = #{spassword,jdbcType=VARCHAR}
  </select>
</mapper>
Note: When must add jdbcType

In the coding of the time, sometimes you find, for example, in User.xml, whether by userId = # {userId} or userId = # {userId, jdbcType = Integer}, no problem. But when it must be added jdbcType?

Mybatis official document described it this way jdbcType of:

The JDBC Type from the list of supported types that follows this table. The JDBC type is only required for nullable columns upon insert, update or delete. This is a JDBC requirement, not a MyBatis one. So even if you were coding JDBC directly, you’d need to specify this type – but only for nullable values.

This means that only when a null effect on the insert, update, delete, jdbcType is necessary. This is a demand JDBC, not a MyBatis. So even if you are the direct write JDBC, when it is empty, you must specify its type, the other without the need to specify its type.

On Stack Overflow, also has this explanation:

Most of the times you don’t need to specify the jdbcType as MyBatis is smart enough to figure out the type from the objects you are working with. But if you send your parameters to the MyBatis statement inside a HashMap, for example, and one of the parameters is null, MyBatis won’t be able to determine the type of the parameter by looking at the HashMap because the HashMap is just a generic container and null itself carries no type information. At that point it would be o good idea to provide the jdbcType so that switching the database implementation later on does not cause any issues with null values.

That is, most of the time, we do not need to specify jdbcType, because MyBatis to be smart enough to identify the type of object. But if you pass over the parameter is a HashMap, where the argument is empty, MyBatis will not be able to determine the type of object HashMap. Because HashMap is a generic container itself and if it is empty, it will not carry any type of information. So the best approach is, when the value is empty to specify its jdbcType, so that when converted to db after implementation will not cause any problem.

JdbcType and the correspondence relationship javaType
jdbcType javaType
CHAR String
VARCHAR String
LONGVARCHAR String
NUMERIC java.math.BigDecimal
DECIMAL java.math.BigDecimal
BIT boolean
BOOLEAN boolean
TINYINT byte
SMALLINT short
INTEGER int
BIGINT long
REAL float
FLOAT double
DOUBLE double
BINARY byte[]
VARBINARY byte[]
LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
CLOB Clob
BLOB Blob
ARRAY Array
OTHER
UNDEFINED
NVARCHAR
NCHAR
NCLOB
CURSOR
NULL
Published 43 original articles · won praise 26 · views 1100

Guess you like

Origin blog.csdn.net/qq_40121580/article/details/103937715