Mybatis開發要點-resultType和resultMap有什麼區別?

語言: CN / TW / HK

一起養成寫作習慣!這是我參與「掘金日新計劃 · 4 月更文挑戰」的第3天,點選檢視活動詳情


Mybatis返回Xml返回值有resultType和resultMap,我們一般都該如何選擇呢?

一、resultType

1、resultType介紹

當使用resultType做SQL語句返回結果型別處理時,對於SQL語句查詢出的欄位在相應的pojo中必須有和它相同的欄位對應,而resultType中的內容就是pojo在本專案中的位置。

2、對映規則

  1. 基本型別  :resultType=基本型別  
  2. List型別:   resultType=List中元素的型別
  3. Map型別    單條記錄:resultType =map    多條記錄:resultType =Map中value的型別

3、自動對映注意事項

  1. 前提:SQL列名和JavaBean的屬性是一致的;
  2. 使用resultType,如用簡寫需要配置typeAliases (別名);
  3. 如果列名和JavaBean不一致,但列名符合單詞下劃線分割,Java是駝峰命名法,則mapUnderscoreToCamelCase可設定為true;

4、程式碼演示

1、t_user_test.sql準備

CREATE TABLE `t_user_test` ( `id` int(20) NOT NULL AUTO_INCREMENT, `user_name` varchar(60) DEFAULT NULL COMMENT '使用者名稱稱', `real_name` varchar(60) DEFAULT NULL COMMENT '真實名稱', `sex` tinyint(3) DEFAULT NULL COMMENT '姓名', `mobile` varchar(20) DEFAULT NULL COMMENT '電話', `email` varchar(60) DEFAULT NULL COMMENT '郵箱', `note` varchar(200) DEFAULT NULL COMMENT '備註', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=142 DEFAULT CHARSET=utf8;

2、實體類

``` package com.enjoylearning.mybatis.entity;

import java.io.Serializable; import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.mysql.jdbc.Blob;

public class TUser implements Serializable{

private Integer id;

private String userName;

private String realName;

private Byte sex;

private String mobile;

private String email;

private String note;

private TPosition position;

private List<TJobHistory> jobs ;

private List<HealthReport> healthReports;


private List<TRole> roles;



@Override
public String toString() {
    String positionId=  (position == null ? "" : String.valueOf(position.getId()));
    return "TUser [id=" + id + ", userName=" + userName + ", realName="
            + realName + ", sex=" + sex + ", mobile=" + mobile + ", email="
            + email + ", note=" + note + ", positionId=" + positionId + "]";
}


public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public String getRealName() {
    return realName;
}
public void setRealName(String realName) {
    this.realName = realName;
}

public Byte getSex() {
    return sex;
}

public void setSex(Byte sex) {
    this.sex = sex;
}


public String getMobile() {
    return mobile;
}


public void setMobile(String mobile) {
    this.mobile = mobile;
}


public String getEmail() {
    return email;
}


public void setEmail(String email) {
    this.email = email;
}


public String getNote() {
    return note;
}

public void setNote(String note) {
    this.note = note;
}


public TPosition getPosition() {
    return position;
}


public void setPosition(TPosition position) {
    this.position = position;
}

public List<TJobHistory> getJobs() {
    return jobs;
}


public void setJobs(List<TJobHistory> jobs) {
    this.jobs = jobs;
}

public List<HealthReport> getHealthReports() {
    return healthReports;
}

public void setHealthReports(List<HealthReport> healthReports) {
    this.healthReports = healthReports;
}

public List<TRole> getRoles() {
    return roles;
}

public void setRoles(List<TRole> roles) {
    this.roles = roles;
}

} ```

3、Mapper介面類

``` public interface TUserTestMapper {

TUser selectByPrimaryKey(Integer id);
List<TUser> selectAll();

} ```

4、Mapper xml

```

<select id="selectByPrimaryKey" resultType="TUser">
    select
    id, user_name, real_name, sex, mobile, email, note
    from t_user_test
    where id = #{id,jdbcType=INTEGER}
</select>


<select id="selectAll" resultType="TUser">
    select
    id, user_name, real_name, sex, mobile, email, note
    from t_user_test
</select>

```

5、配置檔案

```

<properties resource="db.properties"/>

<settings>
    <!-- 設定自動駝峰轉換        -->
    <setting name="mapUnderscoreToCamelCase" value="true" />

    <!-- 開啟懶載入 -->      
     <!-- 當啟用時,有延遲載入屬性的物件在被呼叫時將會完全載入任意屬性。否則,每種屬性將會按需要載入。預設:true -->
  <setting name="aggressiveLazyLoading" value="false" />

</settings>
<!-- 別名定義 -->
<typeAliases>
    <package name="com.enjoylearning.mybatis.entity" />
</typeAliases>

<plugins>
    <plugin interceptor="com.enjoylearning.mybatis.Interceptors.ThresholdInterceptor"> 
        <property name="threshold" value="10"/>
    </plugin>

     <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <property name="pageSizeZero" value="true" />
    </plugin>
</plugins>


<!--配置environment環境 -->
<environments default="development">
    <!-- 環境配置1,每個SqlSessionFactory對應一個環境 -->
    <environment id="development">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://ip:port/test?useUnicode=true" />
            <property name="username" value="root" />
            <property name="password" value="123456" />
        </dataSource>
    </environment>
</environments>

<!-- 對映檔案,mapper的配置檔案 -->
<mappers>
    <!--直接對映到相應的mapper檔案 -->
    <mapper resource="sqlmapper/TUserTestMapper.xml" />
</mappers>


```

6、啟動測試類

``` public class MybatisDemo2 {

private SqlSessionFactory sqlSessionFactory;

@Before
public void init() throws IOException {
    //--------------------第一階段---------------------------
    // 1.讀取mybatis配置檔案創SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    // 1.讀取mybatis配置檔案創SqlSessionFactory
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    inputStream.close();
}

@Test
//知識點:resultType
public void testAutoMapping() throws IOException {
    // 2.獲取sqlSession   
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 3.獲取對應mapper
    TUserTestMapper mapper = sqlSession.getMapper(TUserTestMapper.class);
    // 4.執行查詢語句並返回多條資料

    List<TUser> users = mapper.selectAll();
    for (TUser tUser : users) {
        System.out.println(tUser);
    }

}

} ```

7、執行結果

sql語句:“com.mysql.jdbc.JDBC4PreparedStatement@654f0d9c: select id, user_name, real_name, sex, mobile, email, note from t_user_test”執行時間為:35毫秒,已經超過閾值! TUser [id=1, userName=zhangsan, realName=張三, sex=1, mobile=186995587411, [email protected], note=zhangsan的備註, positionId=] TUser [id=2, userName=lisi, realName=李四, sex=1, mobile=18677885200, email=l[email protected], note=lisi的備註, positionId=] TUser [id=3, userName=wangwu, realName=王五, sex=2, mobile=18695988747, [email protected], note=wangwu's note, positionId=]

resultType當返基本型別的時候建議選擇,當返回POJO類的時候由於需要完全和資料庫欄位進行對應,存在不靈活、問題排查難等問題。

二、resultMap

1、resultMap  介紹

resultMap 元素是 MyBatis 中最重要最強大的元素。它可以讓你從 90% 的 JDBC ResultSets 資料提取程式碼中解放出來,在對複雜語句進行聯合對映的時候,它很可能可以代替數千行的同等功能的程式碼。ResultMap 的設計思想是,簡單的語句不需要明確的結果對映,而複雜一點的語句只需要描述它們的關係就行了。

2、resultMap屬性

| | | | ----------- | ------------------------------------------------------------------------------------- | | 屬性 | 描述 | | id | 當前名稱空間中的一個唯一標識,用於標識一個result map. | | type | 類的完全限定名, 或者一個類型別名. | | autoMapping | 如果設定這個屬性,MyBatis將會為這個ResultMap開啟或者關閉自動對映。這個屬性會覆蓋全域性的屬性 autoMappingBehavior。預設值為:unset。 |

3、使用場景

  1. 欄位有自定義的轉化規則
  2. 複雜的多表查詢

4、resultMap子元素屬性

  1. id –一個 ID 結果;標記出作為 ID 的結果可以幫助提高整體效能,一對多的查詢中用於結果集合並;
  2. result – 注入到欄位或 JavaBean 屬性的普通結果
  3. association – 一個複雜型別的關聯;許多結果將包裝成這種型別。關聯可以指定為一個 resultMap 元素,或者引用一個
  4. collection – 一個複雜型別的集合

5、程式碼演示

實體類,配置檔案同上

1、mapper介面

```

public interface TUserMapper {

List<TUser> selectTestResultMap();

} ```

2、Mapper.xml

```

<resultMap id="UserResultMap" type="TUser" autoMapping="true">
    <id column="id" property="id" />
    <result column="userName" property="userName"/>
    <result column="realName" property="realName" />
    <result column="sex" property="sex" />
    <result column="mobile" property="mobile" />
    <result column="email" property="email" />
    <result column="note" property="note" />
    <association property="position" javaType="TPosition" columnPrefix="post_">
        <id column="id" property="id"/>
        <result column="name" property="postName"/>
        <result column="note" property="note"/>
    </association>
</resultMap>

<select  id="selectTestResultMap" resultMap="UserResultMap" >
    select
        a.id,
        userName,
        realName,
        sex,
        mobile,
        email,
        a.note,
        b.id  post_id,
        b.post_name,
        b.note post_note
    from t_user a,
        t_position b
    where a.position_id = b.id

</select>

```

3、啟動測試

```

public class MybatisDemo2 {

private SqlSessionFactory sqlSessionFactory;

@Before
public void init() throws IOException {
    //--------------------第一階段---------------------------
    // 1.讀取mybatis配置檔案創SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    // 1.讀取mybatis配置檔案創SqlSessionFactory
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    inputStream.close();
}



@Test
public void testResultMap() throws IOException {
    //--------------------第二階段---------------------------
    // 2.獲取sqlSession   
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 3.獲取對應mapper
    TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);

    //--------------------第三階段---------------------------

    // 4.執行查詢語句並返回單條資料
    List<TUser> users = mapper.selectTestResultMap();
    for (TUser tUser : users) {
        System.out.println(tUser.getUserName());
        System.out.println(tUser.getPosition().getPostName());
    }
}

} ```

4、執行結果

sql語句:“com.mysql.jdbc.JDBC4PreparedStatement@19bb07ed: select a.id, userName, realName, sex, mobile, email, a.note, b.id post_id, b.post_name, b.note post_note from t_user a, t_position b where a.position_id = b.id”執行時間為:52毫秒,已經超過閾值! zhangsan 總經理 lisi 零時工 wangwu 總經理

三、結論

當返回物件為基礎型別時建議走resultType,當返回物件為POJO時,強制走resultMap。同時可以參考阿里巴巴JAVA開發手冊中的5.4.3節,返回要解耦,不訥訥更直接使用resultClass。