日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis(延迟加载 缓存)

發布時間:2023/12/3 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis(延迟加载 缓存) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

mybatis目錄

一對一延遲加載
1.在SqlMapConfig.xml中配置setting標簽
2.在IAccoutDao.xml中配置association標簽
3.測試類
4.成功運行
一對多延遲加載
2.在IUserDao.xml中配置collection標簽
緩存
一級緩存
二級緩存
1.SqlMaoConfig.xml中
2.在需要使用二級緩存的實體類的mapper中
3.測試類

一對一延遲加載

1.在SqlMapConfig.xml中配置setting標簽

詳情看中文官網

<settings><!-- 配置全局緩存--><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="true"/></settings>

2.在IAccoutDao.xml中配置association標簽

<!--定義封裝account和user的resultMap--><resultMap id="accountUserMap" type="Account"><id property="id" column="id"></id><result property="uid" column="uid"></result><result property="money" column="money"></result><!--一對一的關系映射,配置封裝user的內容select屬性的內容,查詢用戶的唯一標識符column屬性的內容:用戶根據id查詢時,所需要參數的值--><association property="user" column="uid" javaType="User" select="com.daniel.dao.IUserDao.findById"><id property="id" column="id"></id><result property="username" column="username"></result><result property="sex" column="sex"></result><result property="address" column="address"></result><result property="birthday" column="birthday"></result></association></resultMap><select id="findAll" resultMap="accountUserMap">select * from account</select>

3.測試類

@Testpublic void findAll(){List<Account> accounts = accoutDao.findAll();for (Account account:accounts) {System.out.println("每一個account的信息");System.out.println(account);System.out.println(account.getUser());}}

4.成功結果

一對多延遲加載

和一對一沒有太多區別

2.在IUserDao.xml中配置collection標簽

<!--定義封裝account和user的resultMap--><resultMap id="accountUserMap" type="Account"><id property="id" column="id"></id><result property="username" column="username"></result><result property="sex" column="sex"></result><result property="address" column="address"></result><result property="birthday" column="birthday"></result><!--一對一的關系映射,配置封裝user的內容select屬性的內容,查詢用戶的唯一標識符column屬性的內容:用戶根據id查詢時,所需要參數的值--><collection property="accounts" ofType="Account" select="com.daniel.dao.IAccoutDao.findAccountByUid" column="id"><id property="id" column="aid"></id><result property="uid" column="uid"></result><result property="money" column="money"></result></collection></resultMap>

緩存

一級緩存

其實mybatis中默認就是一級緩存了(平時的測試類就是一級緩存存在SqlSession中)

二級緩存

1.SqlMaoConfig.xml中

<settings><setting name="cacheEnabled" value="true"/></settings>

2.在需要使用二級緩存的實體類的mapper中

<!--開啟user支持二級緩存--><cache/><!-- 根據id查詢用戶 注意屬性useCache --><select id="findById" parameterType="INT" resultType="user" useCache="true">select * from user where id = #{uid}</select>

3.測試類

public class SecondLevelCacheTest {private InputStream in;private SqlSessionFactory factory;@Before//用于在測試方法執行之前執行public void init()throws Exception{//1.讀取配置文件,生成字節輸入流in = Resources.getResourceAsStream("SqlMapConfig.xml");//2.獲取SqlSessionFactoryfactory = new SqlSessionFactoryBuilder().build(in);}@After//用于在測試方法執行之后執行public void destroy()throws Exception{in.close();}/*** 測試二級緩存*/@Testpublic void testFirstLevelCache(){SqlSession sqlSession1 = factory.openSession();IUserDao dao1 = sqlSession1.getMapper(IUserDao.class);User user1 = dao1.findById(41);System.out.println(user1);sqlSession1.close();//一級緩存消失SqlSession sqlSession2 = factory.openSession();IUserDao dao2 = sqlSession2.getMapper(IUserDao.class);User user2 = dao2.findById(41);System.out.println(user2);sqlSession2.close();System.out.println(user1 == user2);}}


注意:
從圖中可以看出來第二次findbyId根本沒有走數據庫(數據都是從SqlSessionFactory的二級緩存中拿的 不是對象!)

總結

以上是生活随笔為你收集整理的MyBatis(延迟加载 缓存)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。