實現一對一查詢
resultType:使用resultType實現較爲簡單,如果pojo中沒有包括查詢出來的列名,創建一個pojo類(VO),繼承查詢字段較多的po類,再增加需要的屬性,即可完成映射。
如果沒有對查詢結果有特殊要求建議使用resultType
作用:
將查詢結果按照sql列名pojo屬性名一致性映射到pojo中
場合:
常見一些明細記錄的展示,比如用戶購買商品明細,將關聯查詢信息全部展示在頁面時,此時可直接使用resultType將每一條記錄映射到pojo中,在前端頁面遍歷list(list中是pojo)
resultMap:需要單獨定義resultMap,實現有點麻煩,如果有對查詢結果有特殊的要求,使用resultMap可以完成將關聯查詢映射pojo屬性中。
resultMap可以實現延遲加載,resultType無法實現延遲加載。
使用association和collection完成一對一和一對多高級映射(對結果有特殊映射要求)
association:
作用:
將關聯查詢信息映射到一個pojo對象中。
場合:
爲了方便查詢關聯信息可以使用association將關聯訂單信息映射爲用戶對象的pojo屬性中:比如查詢訂單及關聯用戶信息。
使用resultType無法將查詢結果映射到pojo對象的pojo屬性中,根據對結果集查詢遍歷的需要,選擇使用resultType和resultMap。
collection:
作用:
將關聯查詢信息映射到一個list集合中。
場合:
爲了方便查詢遍歷相關信息可以使用collection將關聯信息映射到list集合中,比如:查詢用戶極限範圍模塊及模塊下的菜單,可使用collection,將模塊映射到模塊list中,將菜單列表映射到模塊對象的菜單list屬性中,這樣的作用目的也是方便對查詢結果進行遍歷查詢。
如果使用resultType無法將查詢結果映射到List集合中。
使用resultType實現步驟:
1.創建一個Vo類,添加需要映射出來的屬性(可以繼承查詢字段較多的POJO類)
例如查詢用戶和其所在部門的信息
package cn.mybatis.model;
//通過此類映射用戶和部門查詢的結果,讓此類繼承查詢字段較多的pojo類
public class UserVo extends User{
// 添加關聯查詢的部門的屬性
private String deptname;
private String description;
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
2.編寫UserMapper.xml,增加statement----findAllByResultType
<select id="findAllByResultType" resultType="cn.mybatis.model.UserVo">
select
user.*,
dept.deptname,
dept.description
from
user,
dept
where user.deptid = dept.did
</select>
3.編寫UserMapper接口,增加 findAllByResultType()方法
public List<UserVo> findAllByResultType()throws Exception;
4.編寫測試類方法
@Test
public void testFindAllByResultType() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
// 創建usermapper對象,mybatis自動生成mapper代理對象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 調用UserMapper的方法
List<UserVo> userList = userMapper.findAllByResultType();
System.out.println(userList);
sqlSession.close();
}
打斷點可以看到userList中有查詢結果
使用resultMap實現步驟:
1.編寫UserMapper.xml,增加一個resultMap,配置相關屬性
<resultMap type="cn.mybatis.model.User" id="BaseResultMap">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="age" property="age"/>
<result column="deptid" property="deptid"/>
<!-- 配置映射的關聯部門信息 -->
<!-- association用於映射關聯查詢單個對象的信息
property:要將關聯查詢的用戶信息映射到表中對應屬性
-->
<association property="dept" javaType="cn.mybatis.model.Dept">
<!-- id:關聯查詢的部門的唯一標誌 -->
<!-- column:指定唯一標誌部門的列 -->
<!-- javaTypey:映射到dept對應屬性 -->
<id column="did" property="did"/>
<result column="deptname" property="deptname"/>
<result column="description" property="description"/>
</association>
</resultMap>
2.編寫UserMapper.xml,增加statement---findAllByResultMap
<select id="findAllByResultMap" resultMap="BaseResultMap">
select
user.*,
dept.deptname,
dept.description
from
user,
dept
where user.deptid = dept.did
</select>
3.編寫UserMapper接口,增加findAllByResultMap()方法
public List<User> findAllByResultMap()throws Exception;
4.編寫測試類方法
@Test
public void testFindAllByResultMap() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
// 創建usermapper對象,mybatis自動生成mapper代理對象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 調用UserMapper的方法
List<User> userList = userMapper.findAllByResultMap();
System.out.println(userList);
sqlSession.close();
}
打斷點可以看到userList中有查詢結果
延遲加載
resultMap可以實現高級映射(使用association、collection實現一對一及一對多映射),association、collection具備延遲加載功能。
延遲加載:先從單表查詢,需要時再從關聯表去關聯查詢,大大提高數據庫性能,因爲查詢單表要比關聯查詢多張錶速度快。
實現查詢用戶和關聯查詢部門信息時,延遲加載部門信息。
1.編寫UserMapper.xml,新增resultMap
<!-- 延遲加載的resultMap -->
<resultMap type="cn.mybatis.model.User" id="UserLazyloadMap">
<!-- 對用戶信息配置 -->
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="age" property="age"/>
<result column="deptid" property="deptid"/>
<!-- select:指定延遲加載需要執行的statement的id(根據部門id查詢部門信息的statement) -->
<!-- 要使用DeptMapper.xml中的selectByPrimaryKey完成根據部門id查詢部門信息,如果不在本mapper.xml中,需要前面加namespace -->
<!-- column:用戶信息中關聯部門信息查詢的列 -->
<association property="dept" javaType="cn.mybatis.model.Dept" select="cn.mybatis.dao.DeptMapper.findDeptByDid" column="deptid">
<!-- 實現對部門信息延遲加載 -->
</association>
</resultMap>
2.編寫UserMapper.xml,增加statement---findUserLazyLoading
<!-- 查詢用戶信息,所在部門信息延遲加載 -->
<select id="findUserLazyLoading" resultMap="UserLazyloadMap">
select * from user
</select>
3.編寫DeptMaper.xml,增加statement---findDeptByDid
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.mybatis.dao.DeptMapper" >
<select id="findDeptByDid" parameterType="int" resultType="Dept" >
select * from dept where did = #{value}
</select>
</mapper>
4.編寫UserMapper接口---新增方法findUserLazyLoading()
// 查詢用戶,延遲加載部門信息
public List<User> findUserLazyLoading()throws Exception;
5.配置SqlMapConfig.xml中延遲加載開關
<!-- 全局配置參數 -->
<settings>
<!-- 打開延遲的開關 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 將積極加載改爲消極加載 -->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 配置日誌 -->
<setting name="logImpl" value="LOG4J"/>
</settings>
6.編寫測試類方法
@Test
public void testFindAllLazyLoading() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
// 創建usermapper對象,mybatis自動生成mapper代理對象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 調用UserMapper的方法
List<User> userList = userMapper.findUserLazyLoading();
for(User user:userList){
// 執行getDept()實現延遲加載
Dept dept = user.getDept();
System.out.println(dept.getDeptname());
}
sqlSession.close();
}