Spring+SpringMVC+MyBatis深入學習及搭建(七)——MyBatis消極式載入,springmvcmybatis
轉載請註明出處:http://www.cnblogs.com/Joanna-Yan/p/6953005.html
前面講到:Spring+SpringMVC+MyBatis深入學習及搭建(六)——MyBatis關聯查詢
1.什麼是消極式載入
resultMap可以實現進階映射(使用association、collection實現一對一及一對多映射),association、collection具備消極式載入功能。
需求:
如果查詢訂單並且關聯查詢使用者資訊。如果先查詢訂單資訊即可滿足要求,當我們需要查詢使用者資訊時再查下使用者資訊。把對使用者資訊的按需去查詢就是消極式載入。
消極式載入:先從單表查詢、需要時再從關聯表去關聯查詢,大大提高資料庫效能,因為查詢單表要比關聯查詢多張錶速度要快。
2.使用association實現消極式載入2.1需求
查詢訂單並且關聯查詢使用者資訊
2.2mapper.xml
需要定義兩個mapper的方法對應的statement。
(1)只查詢訂單資訊
SELECT * FROM orders
在查詢訂單的statement中使用association去消極式載入(執行)下邊的statement(關聯查詢使用者資訊)。
<!-- 查詢訂單關聯查詢使用者 --> <select id="findOrdersUserLazyLoading" resultMap="OrdersUserLazyLoadingResultMap"> SELECT * FROM orders </select>
(2)關聯查詢使用者資訊
通過上邊查詢到的訂單資訊中user_id去關聯查詢使用者資訊
使用UserMapper.xml中的findUserById
<select id="findUserById" parameterType="int" resultType="user"> select * from user where id=#{value} </select>
上邊先去執行findOrdersUserLazyLoading,當需要去查詢使用者的時候再去執行fingUserById,通過resultMap的定義將消極式載入執行配置起來。
2.3 消極式載入resultMap
使用association中的select指定消極式載入去執行的statement的id。
<!-- 消極式載入的resultMap --> <resultMap type="joanna.yan.mybatis.entity.Orders" id="OrdersUserLazyLoadingResultMap"> <!-- 1.對訂單資訊進行映射配置 --> <id column="id" property="id"/> <result column="user_id" property="userId"/> <result column="number" property="number"/> <result column="createtime" property="createtime"/> <result column="note" property="note"/> <!-- 2.實現對使用者資訊進行消極式載入 --> <!-- select:指定消極式載入需要執行的statement的id(是根據user_id查詢使用者資訊的statement) 要使用UserMapper.xml中findUserById完成根據使用者id(user_id)使用者資訊的查詢, 如果findUserById不在本mapper中需要前邊加namespace。 column:訂單資訊中關聯使用者資訊查詢的列,是user_id 關聯查詢的sql理解為: SELECT orders.*, (SELECT username FROM USER WHERE orders.user_id = user.id)username, (SELECT sex FROM USER WHERE orders.user_id = user.id)sex FROM orders --> <association property="user" javaType="joanna.yan.mybatis.entity.User" select="joanna.yan.mybatis.mapper.UserMapper.findUserById" column="user_id"> </association> </resultMap>
2.4mapper.java
//查詢訂單關聯查詢使用者,使用者資訊時消極式載入 public List<Orders> findOrdersUserLazyLoading() throws Exception;
2.5測試2.5.1測試思路
(1)執行上邊mapper方法(findOrdersUserLazyLoading),內部去調用joanna.yan.mybatis.mapper.OrdersCustomMapper中findOrdersUserLazyLoading只查詢orders資訊(單表)。
(2)在程式中去遍曆上一步驟查詢出的List<Orders>,當我們調用Orders中的getUser()時,開始進行消極式載入。
(3)消極式載入,去調用UserMapper.xml中findUserById這個方法擷取使用者資訊。
2.5.2消極式載入配置
mybatis預設沒有開啟消極式載入,需要在SqlMapConfig.xml中setting配置。
在mybatis核心設定檔中配置:
lazyLoadingEnabled、aggressiveLazyLoading
|
設定項 |
描述 |
允許值 |
預設值 |
|
lazyLoadingEnabled |
全域性設定懶載入。如果設為‘false’,則所有相關聯的都會被初始化載入。 |
true | false |
false |
|
aggressiveLazyLoading |
當設定為‘true’的時候,懶載入的對象可能被任何懶屬性全部載入。否則,每個屬性都按需載入。 |
true | false |
true |
在SqlMapConfig.xml中配置:
<!-- 全域配置參數,需要時再設定 --> <settings> <!-- 開啟消極式載入的開關 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 將積極載入改為消極載入即按需要載入 --> <setting name="aggressiveLazyLoading" value="false"/> </settings>
2.5.3測試代碼
@Test public void findOrdersUserLazyLoadingTest() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); OrdersCustomMapper ordersCustomMapper=sqlSession.getMapper(OrdersCustomMapper.class); List<Orders> list=ordersCustomMapper.findOrdersUserLazyLoading(); for (Orders orders : list) { //執行getUser()去查詢使用者資訊,這裡實現按需載入 User user=orders.getUser(); System.out.println(user); } sqlSession.close(); }2.6消極式載入思考
不使用mybatis提供的association及collection中的消極式載入功能,如何?消極式載入?
實現方法如下:
定義兩個mapper方法:
(1)查詢訂單列表
(2)根據使用者id查詢使用者資訊
實現思路:先去查詢第一個mapper方法,擷取訂單資訊列表
在測試程式中,按需去調用第二個mapper方法去查詢使用者資訊。
總之,使用消極式載入方法,先去查詢簡單的sql(最好單表,也可以關聯查詢),再去按需要載入關聯查詢的其它資訊。
如果此文對您有協助,打賞我一下吧~