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

使用typeAlias有優(yōu)點也有缺點
- 優(yōu)點:別名可以自定義
- 缺點:每個類型必須單獨定義
1.2.2 package自定義別名
主配置文件中:
<!--第二種方式 name:包名,mybatis會把這個包中所有類名作為別名 --> <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ū)分大小寫,所以全用小寫同樣也可以
測試類中測試:

同樣測試成功
- 優(yōu)點:使用方便,一次給多個類定義別名。
- 缺點:別名不能隨便定義,必須是類名。
這兩種自定義別名都有優(yōu)點和缺點,所以建議是不適用別名,就使用全限定名稱,這樣清晰明了。
1.3 resultType表示簡單類型
我們首先在dao接口中定義一個方法
long countStudent();
在mapper文件中編寫sql語句
<!--執(zhí)行sql語句,得到的是一個值(一行一列)--> <select id="countStudent" resultType="java.lang.Long"> select count(*) from student </select>
在測試類中測試
@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();
}
控制臺輸出:

1.4 resultType
dao接口中定義方法:
Map<Object, Object> selectMap(Integer id);
mapper文件中寫sql語句:
<!--
執(zhí)行sql得到一個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>
測試類中進行測試:
@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();
}
控制臺輸出:

我們可以看到執(zhí)行結(jié)果,列名作為map的key,列值作為value
dao接口返回一個map,sql語句最多能獲取一行記錄,多于一行是錯誤
2.resultMap
resultMap:結(jié)果映射。自定義列名和Java對象屬性的對應關(guān)系。常用在列名和屬性名不同的情況。
用法:
1.先定義resultMap標簽,指定列名和屬性名稱對應關(guān)系
2.在select標簽使用resultMap屬性,指定上面定義的resultMap的id值
我們來舉一個例子
首先定義一個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ù)庫里面的列名不一致
我們再在dao接口中定義一個方法
CustomObject selectById2(Integer id);
如果我們要將從數(shù)據(jù)庫取出來的內(nèi)容賦值給CustomObject對象,那么如果我們繼續(xù)用resultType的話,從數(shù)據(jù)庫中取出來的數(shù)據(jù)只能賦值給email和age,無法給cid和cname進行賦值,這個時候我們就需要用到resultMap
我們在mapper文件中這樣來寫:
<!--
使用resultMap定義列和屬性的關(guān)系
定義resultMap
id:給resultMap的映射關(guān)系起個名稱,唯一值
type:Java類型的全限定名稱
-->
<resultMap id="customMap" type="com.lu.entity.CustomObject">
<!--定義列名和屬性名的對應-->
<!--主鍵類型使用id標簽-->
<id column="id" property="cid"></id>
<!--非主鍵類型使用result標簽-->
<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)系
我們在測試類中進行測試:
@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();
}
控制臺輸出:

這樣resultMap就成功解決了列名和屬性名不一致的情況。
注意:resultType和resultMap不能同時使用。
3.列名和Java對象屬性名稱不一致解決方式
3.1 使用resultMap:自定義列名和屬性名稱對應關(guān)系
這個方法在上面講解resultMap的時候就講解了,可以直接看上面
3.2 使用resultType
使用列別名,讓別名和Java對象屬性名稱一樣
我們在dao接口中定義一個方法:
CustomObject selectById3(Integer id);
我們在mapper文件中寫:
<!--使用列別名,解決列名和屬性名不同的問題-->
<select id="selectById3" resultType="com.lu.entity.CustomObject">
select id AS cid, name AS cname, email, age where id = #{studentid}
</select>
我們在測試類中進行測試
@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();
}
控制臺輸出:

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-puls中的resultMap數(shù)據(jù)映射
這篇文章主要介紹了mybatis-puls中的resultMap數(shù)據(jù)映射,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
idea查看properties中文變成unicode碼的解決方案
這篇文章主要介紹了idea查看properties中文變成unicode碼的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

