zoukankan      html  css  js  c++  java
  • 【mybatis笔记】 resultType与resultMap的区别

    序言:

    昨天做一个项目,看到很多刚开始用mybatis的同事对于resultType和resultMap的理解与使用含糊不清,这里我试图用最好理解的说法写一写,欢迎大家勘误。

    两者异同:

    相同点:resultType和resultMap都是映射结果集到Javabean用的

    不同点:

    1. resultType属于自动映射到javabean,而resultMap是手动映射到Javabean的,其中简单的映射关系可以使用resultType复杂映射关系的推荐使用resultMap
    2. 使用resultMap需要先在mapper.xml中定义resultMap。而resultType则无需定义

    下面我举两个正例、两个反例:

    需要映射的JavaBean:User

     1 package com.github.hellxz.entity;
     2 
     3 /**
     4  * @Author : Hellxz
     5  * @Description: 被映射的Javabean,常见的User
     6  * @Date : 2018/3/9 8:25
     7  */
     8 public class User {
     9 
    10     private Long userId;
    11     private String username;
    12     private String password;
    13 
    14     public String getUsername() {
    15         return username;
    16     }
    17 
    18     public void setUsername(String username) {
    19         this.username = username;
    20     }
    21 
    22     public String getPassword() {
    23         return password;
    24     }
    25 
    26     public void setPassword(String password) {
    27         this.password = password;
    28     }
    29 
    30     public Long getUserId() {
    31         return userId;
    32     }
    33 
    34     public void setUserId(Long userId) {
    35         this.userId = userId;
    36     }
    37 }

    UserMapper接口定义:

     1 package com.github.hellxz.dao;
     2 
     3 /**
     4  * @Author : Hellxz
     5  * @Description: User的dao接口
     6  * @Date : 2018/3/9 8:44
     7  */
     8 public interface UserMapper {
     9     
    10     User selectByUsername(String username);
    11 }

    正例:

    【resultType正例】:resultType指向具体类型或别名 

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.github.hellxz.dao.UserMapper" >
     4   <sql id="Base_Column_List" >
     5     user_id, user_name, password
     6   </sql>
     7   <select id="selectByUsername" resultType="com.github.hellxz.entity.User" parameterType="string" >
     8     select
     9     <include refid="Base_Column_List" />
    10     from user
    11     where user_name = #{username}
    12   </select>
    13   
    14 </mapper>

    【resultMap正例】:resultMap引用定义好的resultMap的id 

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.github.hellxz.dao.UserMapper" >
     4   <resultMap id="BaseResultMap" type="com.github.hellxz.entity.User" >
     5     <id column="user_id" property="userId" jdbcType="BIGINT" />
     6     <result column="user_name" property="username" jdbcType="VARCHAR" />
     7     <result column="password" property="password" jdbcType="VARCHAR" />
     8   </resultMap>
     9   <sql id="Base_Column_List" >
    10     user_id, user_name, password
    11   </sql>
    12   <select id="selectByUsername" resultMap="BaseResultMap" parameterType="string" >
    13     select 
    14     <include refid="Base_Column_List" />
    15     from user
    16     where user_name = #{username}
    17   </select>
    18 
    19 </mapper>

    反例:

    【resultType反例】:使用resultType去引用定义的resultMap 

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.github.hellxz.dao.UserMapper" >
     4   <resultMap id="BaseResultMap" type="com.github.hellxz.entity.User" >
     5     <id column="user_id" property="userId" jdbcType="BIGINT" />
     6     <result column="user_name" property="username" jdbcType="VARCHAR" />
     7     <result column="password" property="password" jdbcType="VARCHAR" />
     8   </resultMap>
     9   <sql id="Base_Column_List" >
    10     user_id, user_name, password
    11   </sql>
    12   <select id="selectByUsername" resultType="BaseResultMap" parameterType="string" >
    13     select 
    14     <include refid="Base_Column_List" />
    15     from user
    16     where user_name = #{username}
    17   </select>
    18 
    19 </mapper>

    【resultMap反例】:使用resultType去引用定义的resultMap或者引用没有定义的resultMap 

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.github.hellxz.dao.UserMapper" >
     4   <sql id="Base_Column_List" >
     5     user_id, user_name, password
     6   </sql>
     7   <select id="selectByUsername" resultMap="com.github.hellxz.entity.User" parameterType="string" >
     8     select 
     9     <include refid="Base_Column_List" />
    10     from user
    11     where user_name = #{username}
    12   </select>
    13 
    14 </mapper>
  • 相关阅读:
    老李推荐:第14章7节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-获取版本号 2
    老李推荐:第14章7节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-获取版本号 1
    老李推荐:第14章6节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-启动ViewServer
    老李推荐:第14章5节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态
    老李推荐:第14章4节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-端口转发 3
    老李推荐:第14章4节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-端口转发 2
    mybatis-generator : 自动生成代码
    mybatis-generator XML Parser Error on line 38: 必须为元素类型 "table" 声明属性 "enableInsertByPrimaryKey"。
    server.properties 文件详解
    Java 生成 JNI 头文件
  • 原文地址:https://www.cnblogs.com/hellxz/p/8532041.html
Copyright © 2011-2022 走看看