一文了解mybatis的延遲加載
本文主要介紹下mybatis的延遲加載,從原理上介紹下怎么使用、有什么好處能規(guī)避什么問題。延遲加載一般用于級聯(lián)查詢(級聯(lián)查詢可以將主表不能直接查詢的數(shù)據(jù)使用自定義映射規(guī)則調用字表來查,主查詢查完之后通過某個column列或多個列將查詢結果傳遞給子查詢,子查詢再根據(jù)主查詢傳遞的參數(shù)進行查詢,最后將子查詢結果進行映射)。mybatis的懶加載是通過創(chuàng)建代理對象來實現(xiàn)的,只有當調用getter等方法的時候才會去查詢子查詢,查詢后完成設值再獲取值。
1. 什么時候會創(chuàng)建代理對象
private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException { this.useConstructorMappings = false; // reset previous mapping result final List<Class<?>> constructorArgTypes = new ArrayList<>(); final List<Object> constructorArgs = new ArrayList<>(); // 創(chuàng)建result接收對象 Object resultObject = createResultObject(rsw, resultMap, constructorArgTypes, constructorArgs, columnPrefix); if (resultObject != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) { // 處理其他屬性properties final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings(); for (ResultMapping propertyMapping : propertyMappings) { // issue gcode #109 && issue #149 創(chuàng)建代理 if (propertyMapping.getNestedQueryId() != null && propertyMapping.isLazy()) { resultObject = configuration.getProxyFactory().createProxy(resultObject, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs); break; } } } // 使用有參構造函數(shù)創(chuàng)建了對象 this.useConstructorMappings = resultObject != null && !constructorArgTypes.isEmpty(); // set current mapping result return resultObject; }
通過mybatis代碼propertyMapping.getNestedQueryId() != null && propertyMapping.isLazy()
發(fā)現(xiàn)只有當存在嵌套查詢select子句和isLazy=true的時候才會創(chuàng)建代理,那么isLazy=true是什么條件,從創(chuàng)建ResultMapping的代碼中可以看到boolean lazy = "lazy".equals(context.getStringAttribute("fetchType", configuration.isLazyLoadingEnabled() ? "lazy" : "eager"));
只有手動設置fetchType=lazy或者全局設置configuration的lazyLoadingEnabled=true,兩者缺一不可。
關于代理是通過Javassist創(chuàng)建的,下面有一個簡單的例子
public class HelloMethodHandler implements MethodHandler { private Object target; public HelloMethodHandler(Object o) { this.target = o; } @Override public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable { String methodName = thisMethod.getName(); if (methodName.startsWith("get")) { System.out.println("select database...."); // 進行sql查詢到結果并set設置值 ((Student)self).setName("monian"); } return proceed.invoke(self, args); } public static void main(String[] args) throws Exception { Student student = new Student(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass(Student.class); Constructor<Student> declaredConstructor = Student.class.getDeclaredConstructor(); Object o = proxyFactory.create(declaredConstructor.getParameterTypes(), new Object[]{}); ((Proxy)o).setHandler(new HelloMethodHandler(student)); Student proxy = (Student)o; System.out.println(proxy.getName()); } }
mybatis的原理就是通過創(chuàng)建一個代理對象,當通過這個代理對象調用getter、is、equals、clone、toString、hashCode等方法時會調用select子查詢,然后完成設置,最后取值就像早就獲取到一樣。
2. 如何使用
public class UserDO { private Integer userId; private String username; private String password; private String nickname; private List<PermitDO> permitDOList; public UserDO() {} }
<resultMap id="BaseMap" type="org.apache.ibatis.study.entity.UserDO"> <id column="user_id" jdbcType="INTEGER" property="userId" /> <result column="username" jdbcType="VARCHAR" property="username" /> <result column="password" jdbcType="VARCHAR" property="password" /> <result column="nickname" jdbcType="VARCHAR" property="nickname"/> <collection property="permitDOList" column="user_id" select="getPermitsByUserId" fetchType="lazy"> </collection> </resultMap> <resultMap id="PermitBaseMap" type="org.apache.ibatis.study.entity.PermitDO"> <id column="id" jdbcType="INTEGER" property="id"/> <result column="code" jdbcType="VARCHAR" property="code"/> <result column="name" jdbcType="VARCHAR" property="name"/> <result column="type" jdbcType="TINYINT" property="type"/> <result column="pid" jdbcType="INTEGER" property="pid"/> </resultMap> <select id="getByUserId2" resultMap="BaseMap"> select * from user where user_id = #{userId} </select> <select id="getPermitsByUserId" resultMap="PermitBaseMap"> select p.* from user_permit up inner join permit p on up.permit_id = p.id where up.user_id = #{userId} </select>
通過fetchType=lazy指定子查詢getPermitsByUserId使用懶加載,這樣的話就不用管全局配置lazyLoadingEnabled是true還是false了。當然這里可以直接用多表關聯(lián)查詢不使用子查詢,使用方法在上一篇文章
測試代碼
public class Test { public static void main(String[] args) throws IOException { try (InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml")) { // 構建session工廠 DefaultSqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); UserDO userDO = userMapper.getByUserId2(1); System.out.println(userDO); } } }
結果如下,打了斷點可以看到原userDO對象已被代理并且permitDOList是null需要調用get方法才會去查詢拿到值,咳咳這邊之前直接運行顯示是已經(jīng)把permitDOList查詢出來了,想了半天啥原因后來才發(fā)現(xiàn)println會調用userDO對象的toString方法,而toString方法也會走代理方法直接去調用子查詢的
3.延遲加載的好處
延遲加載主要能解決mybatis的N+1問題,什么是N+1問題其實叫1+N更為合理,以上面的業(yè)務例子來說就是假設一次查詢出來10000個用戶,那么還需要針對這10000個用戶使用子查詢getPermitsByUserId獲取每個用戶的權限列表,需要10000次查詢,總共10001次,真實情況下你可能并不需要每個子查詢的結果,這樣就浪費數(shù)據(jù)庫連接資源了。如果使用延遲加載的話就相當于不用進行這10000次查詢,因為它是等到你真正使用的時候才會調用子查詢獲取結果。
以上就是一文了解mybatis的延遲加載的詳細內(nèi)容,更多關于mybatis延遲加載的資料請關注腳本之家其它相關文章!
相關文章
Java實現(xiàn)excel大數(shù)據(jù)量導入
這篇文章主要為大家詳細介紹了Java實現(xiàn)excel大數(shù)據(jù)量導入,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08使用SpringBoot注解方式處理事務回滾實現(xiàn)
這篇文章主要介紹了使用SpringBoot注解方式處理事務回滾實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08解決maven?maven.compiler.source和maven.compiler.target的坑
這篇文章主要介紹了解決maven?maven.compiler.source和maven.compiler.target的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Springboot詳解整合SpringSecurity實現(xiàn)全過程
Spring Security基于Spring開發(fā),項目中如果使用Springboot作為基礎,配合Spring Security做權限更加方便,而Shiro需要和Spring進行整合開發(fā)。因此作為spring全家桶中的Spring Security在java領域很常用2022-07-07