MyBatis中resultType和resultMap的用法及說明
封裝輸出結(jié)果:MyBatis執(zhí)行sql語句,得到ResultSet,轉(zhuǎn)為java對(duì)象
這里我們兩個(gè),分別為resultType和resultMap
1.resultType
- resultType屬性:在執(zhí)行select時(shí)使用,作為標(biāo)簽的屬性出現(xiàn)的。
- resultType:執(zhí)行sql得到ResultSet轉(zhuǎn)換的類型,使用類型的完全限定名或別名。注意如果返回的是集合,那應(yīng)該設(shè)置為集合包含的類型,而不是集合本身。
它的值有兩種:
- java類型的全限定名稱
- 使用別名
1.1 java類型的全限定名稱
dao接口中定義方法
Student selectStudentById(Integer id);
mapper文件中
<select id="selectStudentById" parameterType="int" resultType="com.lu.entity.Student">
select id, name, email, age from student where id = #{studentId}
</select>
resultType:現(xiàn)在使用的是java類型的全限定名稱。表示的意思是mybatis執(zhí)行sql,把ResultSet中的數(shù)據(jù)轉(zhuǎn)為Student類型的對(duì)象。
mybatis會(huì)做一下操作:
1.調(diào)用com.lu.entity.Student的無參構(gòu)造方法, 創(chuàng)建對(duì)象。
Student student = new Student(); //使用反射創(chuàng)建對(duì)象
2.同名的列賦值給同名的屬性
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
3.得到j(luò)ava對(duì)象,如果dao接口返回值是List集合,mybatis把student對(duì)象放入到List集合
所以執(zhí)行
Student mystudent = dao.selectById(1001);
得到數(shù)據(jù)庫中 id = 1001這行數(shù)據(jù),這行數(shù)據(jù)的列值,賦值給了mystudent對(duì)象的屬性。你能得到mystudent對(duì)象,就相當(dāng)于是id = 1001這行數(shù)據(jù)。
1.2 自定義別名
mybatis提供的對(duì)java類型定義簡(jiǎn)短,好記的名稱。
自定義別名有兩種方式:分別為typeAlias和package
1.2.1 typeAlias自定義別名
使用typeAlias自定義別名的步驟:
1.在mybatis主配置文件,使用typeAliases標(biāo)簽聲明別名
<typeAliases>
<!--第一種語法格式
type:java類型的全限定名稱(自定義類型)
alias:自定義別名
-->
<typeAlias type="com.lu.entity.Student" alias="stu"></typeAlias>
</typeAliases>
注意:標(biāo)簽必須寫在標(biāo)簽后面
2.在mapper文件中,resultType = “別名”
<select id="selectStudentById" parameterType="int" resultType="stu">
select id, name, email, age from student where id = #{studentId}
</select>
3.測(cè)試類中測(cè)試
@Test
public void testselectStudentById() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
Student student = dao.selectStudentById(1002);
System.out.println(student);
session.close();
}
控制臺(tái)輸出:

使用typeAlias有優(yōu)點(diǎn)也有缺點(diǎn)
- 優(yōu)點(diǎn):別名可以自定義
- 缺點(diǎn):每個(gè)類型必須單獨(dú)定義
1.2.2 package自定義別名
主配置文件中:
<!--第二種方式 name:包名,mybatis會(huì)把這個(gè)包中所有類名作為別名 --> <package name="com.lu.entity"/>
mapper文件中:
<select id="selectStudentById" parameterType="int" resultType="student">
select id, name, email, age from student where id = #{studentId}
</select>
這一種方式resultType中不區(qū)分大小寫,所以全用小寫同樣也可以
測(cè)試類中測(cè)試:

同樣測(cè)試成功
- 優(yōu)點(diǎn):使用方便,一次給多個(gè)類定義別名。
- 缺點(diǎn):別名不能隨便定義,必須是類名。
這兩種自定義別名都有優(yōu)點(diǎn)和缺點(diǎn),所以建議是不適用別名,就使用全限定名稱,這樣清晰明了。
1.3 resultType表示簡(jiǎn)單類型
我們首先在dao接口中定義一個(gè)方法
long countStudent();
在mapper文件中編寫sql語句
<!--執(zhí)行sql語句,得到的是一個(gè)值(一行一列)--> <select id="countStudent" resultType="java.lang.Long"> select count(*) from student </select>
在測(cè)試類中測(cè)試
@Test
public void testCountStudent() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
long nums = dao.countStudent();
System.out.println("nums = " + nums);
session.close();
}
控制臺(tái)輸出:

1.4 resultType
dao接口中定義方法:
Map<Object, Object> selectMap(Integer id);
mapper文件中寫sql語句:
<!--
執(zhí)行sql得到一個(gè)Map結(jié)構(gòu)數(shù)據(jù),mybatis執(zhí)行sql,把ResultSet轉(zhuǎn)為map
-->
<select id="selectMap" resultType="java.util.HashMap">
select id, name from student where id = #{studentid}
</select>
測(cè)試類中進(jìn)行測(cè)試:
@Test
public void testSelectMap() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
Map<Object, Object> map = dao.selectMap(1001);
System.out.println("map = " + map);
System.out.println("name = " + map.get("name"));
System.out.println("id = " + map.get("id"));
session.close();
}
控制臺(tái)輸出:

我們可以看到執(zhí)行結(jié)果,列名作為map的key,列值作為value
dao接口返回一個(gè)map,sql語句最多能獲取一行記錄,多于一行是錯(cuò)誤
2.resultMap
resultMap:結(jié)果映射。自定義列名和Java對(duì)象屬性的對(duì)應(yīng)關(guān)系。常用在列名和屬性名不同的情況。
用法:
1.先定義resultMap標(biāo)簽,指定列名和屬性名稱對(duì)應(yīng)關(guān)系
2.在select標(biāo)簽使用resultMap屬性,指定上面定義的resultMap的id值
我們來舉一個(gè)例子
首先定義一個(gè)CustomObject類
package com.lu.entity;
public class CustomObject {
private Integer cid;
private String cname;
private String email;
private Integer age;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "CustomObject{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
我們發(fā)現(xiàn)這里cid和cname和數(shù)據(jù)庫里面的列名不一致
我們?cè)僭赿ao接口中定義一個(gè)方法
CustomObject selectById2(Integer id);
如果我們要將從數(shù)據(jù)庫取出來的內(nèi)容賦值給CustomObject對(duì)象,那么如果我們繼續(xù)用resultType的話,從數(shù)據(jù)庫中取出來的數(shù)據(jù)只能賦值給email和age,無法給cid和cname進(jìn)行賦值,這個(gè)時(shí)候我們就需要用到resultMap
我們?cè)趍apper文件中這樣來寫:
<!--
使用resultMap定義列和屬性的關(guān)系
定義resultMap
id:給resultMap的映射關(guān)系起個(gè)名稱,唯一值
type:Java類型的全限定名稱
-->
<resultMap id="customMap" type="com.lu.entity.CustomObject">
<!--定義列名和屬性名的對(duì)應(yīng)-->
<!--主鍵類型使用id標(biāo)簽-->
<id column="id" property="cid"></id>
<!--非主鍵類型使用result標(biāo)簽-->
<result column="name" property="cname"></result>
<!--列名和屬性名相同,不用定義-->
</resultMap>
<!--使用resultMap屬性,指定映射關(guān)系的id-->
<select id="selectById2" resultMap="customMap">
select id, name, email, age from student where id = #{studentid}
</select>
這樣我們就可以使用resultMap來定義數(shù)據(jù)庫列和屬性的關(guān)系
我們?cè)跍y(cè)試類中進(jìn)行測(cè)試:
@Test
public void testSelectById2() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
CustomObject co = dao.selectById2(1001);
System.out.println(co);
session.close();
}
控制臺(tái)輸出:

這樣resultMap就成功解決了列名和屬性名不一致的情況。
注意:resultType和resultMap不能同時(shí)使用。
3.列名和Java對(duì)象屬性名稱不一致解決方式
3.1 使用resultMap:自定義列名和屬性名稱對(duì)應(yīng)關(guān)系
這個(gè)方法在上面講解resultMap的時(shí)候就講解了,可以直接看上面
3.2 使用resultType
使用列別名,讓別名和Java對(duì)象屬性名稱一樣
我們?cè)赿ao接口中定義一個(gè)方法:
CustomObject selectById3(Integer id);
我們?cè)趍apper文件中寫:
<!--使用列別名,解決列名和屬性名不同的問題-->
<select id="selectById3" resultType="com.lu.entity.CustomObject">
select id AS cid, name AS cname, email, age where id = #{studentid}
</select>
我們?cè)跍y(cè)試類中進(jìn)行測(cè)試
@Test
public void testSelectById3() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
CustomObject co = dao.selectById2(1003);
System.out.println(co);
session.close();
}
控制臺(tái)輸出:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)接口防刷的五種方案
接口防刷是保障系統(tǒng)安全與穩(wěn)定性的重要措施,惡意的高頻請(qǐng)求不僅會(huì)消耗服務(wù)器資源,還可能導(dǎo)致數(shù)據(jù)異常,甚至系統(tǒng)癱瘓,本文將介紹在SpringBoot框架下實(shí)現(xiàn)接口防刷的5種技術(shù)方案,需要的朋友可以參考下2025-04-04
Java中的ReadWriteLock高效處理并發(fā)讀寫操作實(shí)例探究
這篇文章主要為大家介紹了Java中的ReadWriteLock高效處理并發(fā)讀寫操作實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
SpringBoot項(xiàng)目解決跨域的5種實(shí)現(xiàn)過程
本文介紹了SpringBoot項(xiàng)目中解決跨域問題的5種方式,包括使用@CrossOrigin注解、全局配置WebMvcConfigurer、使用CorsFilter過濾器、WebFlux配置和手動(dòng)配置過濾器,推薦使用全局配置WebMvcConfigurer,因?yàn)樗冉y(tǒng)一又靈活,適合大多數(shù)項(xiàng)目場(chǎng)景2026-05-05
springboot構(gòu)造樹形結(jié)構(gòu)數(shù)據(jù)并查詢的方法
本文主要介紹了springboot怎樣構(gòu)造樹形結(jié)構(gòu)數(shù)據(jù)并查詢,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
SpringCloud?Gateway實(shí)現(xiàn)請(qǐng)求解密和響應(yīng)加密的過程解析
這篇文章主要介紹了SpringCloud?Gateway實(shí)現(xiàn)請(qǐng)求解密和響應(yīng)加密的相關(guān)知識(shí),本文環(huán)境使用比較新的?Java?17?和?SpringBoot?3.1.5,對(duì)應(yīng)到Spring的版本是?6.0.13,本文重心是網(wǎng)關(guān)項(xiàng)目,需要的朋友可以參考下2023-11-11
如何在Springboot實(shí)現(xiàn)攔截器功能
其實(shí)spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了,下面這篇文章主要給大家介紹了關(guān)于如何在Springboot實(shí)現(xiàn)攔截器功能的相關(guān)資料,需要的朋友可以參考下2022-06-06

