国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

MyBatis學習筆記(5)

2019-11-06 09:33:20
字體:
來源:轉載
供稿:網友

MyBatis入門之延遲加載

resultMap可以實現高級映射(使用association、collection實現一對一及一對多映射),association、collection具備延遲加載功能。

延遲加載:先從單表查詢、需要時再從關聯表去關聯查詢,大大提高數據庫性能,因為查詢單表要比關聯查詢多張表速度要快。

使用association實現延遲加載

mapper.xml 需要定義兩個mapper的方法對應的statement。

在查詢博客的statement中使用association去延遲加載下邊的satatement(博客作者,即關聯的用戶):

<select id="findBlogAndUser" resultMap="BlogAndUser"> SELECT * FROM tbl_blog </select>

通過上邊的blogUserId來查詢關聯信息:

<select id="selectByjava.lang.Integer" > select userId, userName from tbl_user where userId = #{userid,jdbcType=INTEGER} </select>延遲加載resultMap<resultMap type="com.iCSS.po.Blog" id="BlogAndUser"> <id property="blogid" column="blogId" /> <result property="blogtitle" column="blogTitle" /> <result property="blogcontent" column="blogContent" /> <association property="user" column="blogUserId" select="com.icss.dao.UserMapper.selectByPrimaryKey" javaType="com.icss.po.User"> <id property="userid" column="userId"/> <result property="username" column="userName" /> </association> </resultMap>

與非延遲加載的主要區別就在association標簽屬性多了select和column

延遲加載配置

在mybatis核心配置文件中配置:lazyLoadingEnabled、aggressiveLazyLoading

設置項 描述 允許值 允許值
lazyLoadingEnabled 全局性設置懶加載。如果設為‘false’,則所有相關聯的都會被初始化加載 true/false false
aggressiveLazyLoading 當設置為‘true’的時候,懶加載的對象可能被任何懶屬性全部加載。否則,每個屬性都按需加載。 true/false true

<settings> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings>測試代碼@Test public void testfindBlogAndUserLoading() throws Exception{ Sqlsession sqlSession =null; try { //mybatis配置文件 String resourse = "SqlMapConfig.xml"; //獲取配置文件流 InputStream is = Resources.getResourceAsStream(resourse); //創建會話工廠 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //通過會話工廠獲取SqlSession sqlSession = sqlSessionFactory.openSession(); //獲取代理對象 BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class); List<Blog> list = blogMapper.findBlogAndUser(); for(int i=0;i<list.size();i++){ Blog blog = list.get(i); System.out.println("博客標題:"+blog.getBlogtitle()); System.out.println("博客內容:"+blog.getBlogcontent()); User user = blog.getUser(); System.out.println("博客作者:"+user.getUsername()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ sqlSession.close(); } }

延遲加載思考

不使用mybatis提供的association及collection中的延遲加載功能,如何實現延遲加載??

實現方法如下:

定義兩個mapper方法:

查詢博客列表根據用戶id查詢用戶信息

實現思路:

先去查詢第一個mapper方法,獲取博客信息列表;在程序中(service),按需去調用第二個mapper方法去查詢用戶信息。

總之,使用延遲加載方法,先去查詢簡單的sql(最好單表,也可以關聯查詢),再去按需要加載關聯查詢的其它信息。

結果

DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.DEBUG [main] - PooledDataSource forcefully closed/removed all connections.DEBUG [main] - PooledDataSource forcefully closed/removed all connections.DEBUG [main] - PooledDataSource forcefully closed/removed all connections.DEBUG [main] - PooledDataSource forcefully closed/removed all connections.DEBUG [main] - Opening JDBC ConnectionDEBUG [main] - Created connection 552364977.DEBUG [main] - Setting autocommit to false on JDBC Connection [com.MySQL.jdbc.JDBC4Connection@20ec6bb1]DEBUG [main] - ==> Preparing: SELECT * FROM tbl_blog DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 1博客標題:Mybatis入門博客內容:Mybatis入門程序內容DEBUG [main] - ==> Preparing: select userId, userName from tbl_user where userId = ? DEBUG [main] - ==> Parameters: 1(Integer)DEBUG [main] - <== Total: 1博客作者:Mr_LiDEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@20ec6bb1]DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@20ec6bb1]DEBUG [main] - Returned connection 552364977 to pool.

注意

寫代碼過程中發現一個異常,然后并不是我自己引起的異常,而是因為導入的mybatis包版本問題,異常代碼如下:

java.lang.NullPointerException at com.icss.TestMybatis.testfindBlogAndUserLoading(TestMybatis.java:333) at sun.reflect.NativeMethodaccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

這是mybatis依賴包OGNL 2.6.9的一個bug,可以通過升級mybatis版本到 3.3.0及以上來解決此問題。

mybatis官方issue說明: https://github.com/mybatis/mybatis-3/issues/224


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临颍县| 永济市| 定边县| 盐源县| 上饶市| 日照市| 内乡县| 太原市| 贵阳市| 建平县| 朝阳区| 通化市| 邵东县| 喜德县| 广饶县| 宣恩县| 南宁市| 突泉县| 皮山县| 泸州市| 泰顺县| 高阳县| 双牌县| 博乐市| 宝兴县| 阿克陶县| 连州市| 洛浦县| 镇原县| 南昌市| 祥云县| 盐津县| 西林县| 郓城县| 曲沃县| 兴和县| 榕江县| 黎城县| 界首市| 民乐县| 舒兰市|