resultType
resultType可以把查詢結果封裝到pojo類型中,但必須pojo類的屬性名和查詢到的數據庫表的字段名一致。
如果sql查詢到的字段與pojo的屬性名不一致,則需要使用resultMap將字段名和屬性名對應起來,進行手動配置封裝,將結果映射到pojo中
resultMap
resultMap可以實現將查詢結果映射爲複雜類型的pojo,比如在查詢結果映射對象中包括pojo和list實現一對一查詢和一對多查詢。
先在Mapper文件中,配置基本的sql語句
<!-- 查詢所有的訂單數據 -->
<!-- resultMap:填入配置的resultMap標籤的id值 -->
<select id="queryOrderAll" resultMap="orderResultMap">
SELECT id, user_id,
number,
createtime, note FROM `order`
</select>
配置resultMap標籤,映射不同的字段和屬性名
<!-- resultMap最終還是要將結果映射到pojo上,type就是指定映射到哪一個pojo -->
<!-- id:設置ResultMap的id -->
<resultMap type="order" id="orderResultMap">
<!-- 定義主鍵 ,非常重要。如果是多個字段,則定義多個id -->
<!-- property:主鍵在pojo中的屬性名 -->
<!-- column:主鍵在數據庫中的列名 -->
<id property="id" column="id" />
<!-- 定義普通屬性 -->
<result property="userId" column="user_id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</resultMap>
結果就可以封裝到pojo類型中
使用resultMap進行關聯查詢
一對一查詢
一對一數據模型:訂單用戶
一個訂單信息只會是一個人下的訂單,所以從查詢訂單信息出發關聯查詢用戶信息爲一對一查詢。如果從用戶信息出發查詢用戶下的訂單信息則爲一對多查詢,因爲一個用戶可以下多個訂單。
-
改造pojo類
在訂單類中添加User屬性,User屬性是一個引用類型,用於存儲關聯查詢的用戶信息,因爲關聯關係是一對一,所以只需要添加單個屬性即可 -
配置Mapper.xml配置文件
OrderMapper.xml
先使用id和result屬性,映射order類的結果集,然後在使用association映射關聯對象User的結果集<resultMap type="order" id="orderUserResultMap"> <id property="id" column="id" /> <result property="userId" column="user_id" /> <result property="number" column="number" /> <result property="createtime" column="createtime" /> <result property="note" column="note" /> <!-- association :配置一對一屬性 --> <!-- property:order裏面的User屬性名 --> <!-- javaType:屬性類型 --> <association property="user" javaType="user"> <!-- id:聲明主鍵,表示user_id是關聯查詢對象的唯一標識--> <id property="id" column="user_id" /> <result property="username" column="username" /> <result property="address" column="address" /> </association>
</resultMap>
<!-- 一對一關聯,查詢訂單,訂單內部包含用戶屬性 -->
<select id="queryOrderUserResultMap" resultMap="orderUserResultMap">
SELECT
o.id,
o.user_id,
o.number,
o.createtime,
o.note,
u.username,
u.address
FROMorder
o
LEFT JOIN user
u ON o.user_id = u.id
</select>
* 測試
@Test
public void testQueryOrderUserResultMap() {
// mybatis和spring整合,整合之後,交給spring管理
SqlSession sqlSession = this.sqlSessionFactory.openSession();
// 創建Mapper接口的動態代理對象,整合之後,交給spring管理
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 使用userMapper執行根據條件查詢用戶,結果封裝到Order類中
List<Order> list = userMapper.queryOrderUserResultMap();
for (Order o : list) {
System.out.println(o);
}
// mybatis和spring整合,整合之後,交給spring管理
sqlSession.close();
}
* 結果
![](https://s1.51cto.com/images/blog/202001/11/1ec27c53f157d88009665afc05580a30.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
**一對多查詢**
查詢所有用戶信息及相關訂單。
* 修改pojo類,在pojo類添加訂單集合屬性
![](https://s1.51cto.com/images/blog/202001/11/8bf8f7d7cc7a0b388de7a539f8ada365.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
* 修改UserMapper.xml配置文件
先使用id和result配置映射User類的結果,然後使用一對多關係的collection標籤配置Order結果
<resultMap type="user" id="userOrderResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="birthday" column="birthday" />
<result property="sex" column="sex" />
<result property="address" column="address" />
<!-- 配置一對多的關係
property:填寫pojo類中集合類類屬性的名稱
javaType:填寫集合類型的名稱
-->
<collection property="orders" javaType="list" ofType="order">
<!-- 配置主鍵,是關聯Order的唯一標識 -->
<id property="id" column="oid" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</collection>
</resultMap>
<!-- 一對多關聯,查詢訂單同時查詢該用戶下的訂單 -->
<select id="queryUserOrder" resultMap="userOrderResultMap">
SELECT
u.id,
u.username,
u.birthday,
u.sex,
u.address,
o.id oid,
o.number,
o.createtime,
o.note
FROMuser
u
LEFT JOIN order
o ON u.id = o.user_id
</select>
* 測試結果
![](https://s1.51cto.com/images/blog/202001/11/e5e94dfbef43a8e5b884deddee8648bd.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)