电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Mybatis懶加載的實現

瀏覽:102日期:2023-10-23 17:09:55

因為通過javassist和cglib代理實現的,所以說到底最主要的就是JavasisstProxyFactory類中的invoke方法和里面的load方法。

其實讀一讀,里面的邏輯就是跟配置中定義的規則一樣的因為github上的mybatis中文版中這部分注釋比較少,所以從網上尋找博客,截取了代碼注釋片段記錄下。JavasisstProxyFactory

public class JavassistProxyFactory implements org.apache.ibatis.executor.loader.ProxyFactory { /** * 接口實現 * @param target 目標結果對象 * @param lazyLoader 延遲加載對象 * @param configuration 配置 * @param objectFactory 對象工廠 * @param constructorArgTypes 構造參數類型 * @param constructorArgs 構造參數值 * @return */ @Override public Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) { return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs); } //省略... /** * 代理對象實現,核心邏輯執行 */ private static class EnhancedResultObjectProxyImpl implements MethodHandler { /** * 創建代理對象 * @param type * @param callback * @param constructorArgTypes * @param constructorArgs * @return */ static Object crateProxy(Class<?> type, MethodHandler callback, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) { ProxyFactory enhancer = new ProxyFactory(); enhancer.setSuperclass(type); try { //通過獲取對象方法,判斷是否存在該方法 type.getDeclaredMethod(WRITE_REPLACE_METHOD); // ObjectOutputStream will call writeReplace of objects returned by writeReplace if (log.isDebugEnabled()) { log.debug(WRITE_REPLACE_METHOD + " method was found on bean " + type + ", make sure it returns this"); } } catch (NoSuchMethodException e) { //沒找到該方法,實現接口 enhancer.setInterfaces(new Class[]{WriteReplaceInterface.class}); } catch (SecurityException e) { // nothing to do here } Object enhanced; Class<?>[] typesArray = constructorArgTypes.toArray(new Class[constructorArgTypes.size()]); Object[] valuesArray = constructorArgs.toArray(new Object[constructorArgs.size()]); try { //創建新的代理對象 enhanced = enhancer.create(typesArray, valuesArray); } catch (Exception e) { throw new ExecutorException("Error creating lazy proxy. Cause: " + e, e); } //設置代理執行器 ((Proxy) enhanced).setHandler(callback); return enhanced; } /** * 代理對象執行 * @param enhanced 原對象 * @param method 原對象方法 * @param methodProxy 代理方法 * @param args 方法參數 * @return * @throws Throwable */ @Override public Object invoke(Object enhanced, Method method, Method methodProxy, Object[] args) throws Throwable { final String methodName = method.getName(); try { synchronized (lazyLoader) { if (WRITE_REPLACE_METHOD.equals(methodName)) { //忽略暫未找到具體作用 Object original; if (constructorArgTypes.isEmpty()) { original = objectFactory.create(type); } else { original = objectFactory.create(type, constructorArgTypes, constructorArgs); } PropertyCopier.copyBeanProperties(type, enhanced, original); if (lazyLoader.size() > 0) { return new JavassistSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs); } else { return original; } } else { //延遲加載數量大于0 if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) { //aggressive 一次加載性所有需要要延遲加載屬性或者包含觸發延遲加載方法 if (aggressive || lazyLoadTriggerMethods.contains(methodName)) { log.debug("==> laze lod trigger method:" + methodName + ",proxy method:" + methodProxy.getName() + " class:" + enhanced.getClass()); //一次全部加載 lazyLoader.loadAll(); } else if (PropertyNamer.isSetter(methodName)) { //判斷是否為set方法,set方法不需要延遲加載 final String property = PropertyNamer.methodToProperty(methodName); lazyLoader.remove(property); } else if (PropertyNamer.isGetter(methodName)) { final String property = PropertyNamer.methodToProperty(methodName); if (lazyLoader.hasLoader(property)) { //延遲加載單個屬性 lazyLoader.load(property); log.debug("load one :" + methodName); } } } } } return methodProxy.invoke(enhanced, args); } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } } }

load方法

/** * 執行懶加載查詢,獲取數據并且set到userObject中返回 * @param userObject * @throws SQLException */ public void load(final Object userObject) throws SQLException { //合法性校驗 if (this.metaResultObject == null || this.resultLoader == null) { if (this.mappedParameter == null) { throw new ExecutorException('Property [' + this.property + '] cannot be loaded because ' + 'required parameter of mapped statement [' + this.mappedStatement + '] is not serializable.'); } //獲取mappedstatement并且校驗 final Configuration config = this.getConfiguration(); final MappedStatement ms = config.getMappedStatement(this.mappedStatement); if (ms == null) { throw new ExecutorException('Cannot lazy load property [' + this.property + '] of deserialized object [' + userObject.getClass() + '] because configuration does not contain statement [' + this.mappedStatement + ']'); } //使用userObject構建metaobject,并且重新構建resultloader對象 this.metaResultObject = config.newMetaObject(userObject); this.resultLoader = new ResultLoader(config, new ClosedExecutor(), ms, this.mappedParameter, metaResultObject.getSetterType(this.property), null, null); } /* We are using a new executor because we may be (and likely are) on a new thread * and executors aren’t thread safe. (Is this sufficient?) * * A better approach would be making executors thread safe. */ if (this.serializationCheck == null) { final ResultLoader old = this.resultLoader; this.resultLoader = new ResultLoader(old.configuration, new ClosedExecutor(), old.mappedStatement, old.parameterObject, old.targetType, old.cacheKey, old.boundSql); } //獲取數據庫查詢結果并且set到結果對象返回 this.metaResultObject.setValue(property, this.resultLoader.loadResult()); }

參考地址:https://www.cnblogs.com/qixidi/p/10251126.htmlhttps://blog.csdn.net/mingtian625/article/details/47358003

到此這篇關于Mybatis懶加載的實現的文章就介紹到這了,更多相關Mybatis 懶加載內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 蒸压釜-陶粒板隔墙板蒸压釜-山东鑫泰鑫智能装备有限公司 | 开云(中国)Kaiyun·官方网站 - 登录入口 | 灌装封尾机_胶水灌装机_软管灌装封尾机_无锡和博自动化机械制造有限公司 | 陕西自考报名_陕西自学考试网 | 运动木地板价格,篮球馆体育运动木地板生产厂家_欧氏地板 | 上海办公室装修,写字楼装修—启鸣装饰设计工程有限公司 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 诚暄电子公司首页-线路板打样,pcb线路板打样加工制作厂家 | 数控走心机-走心机价格-双主轴走心机-宝宇百科 | 深圳市索富通实业有限公司-可燃气体报警器 | 可燃气体探测器 | 气体检测仪 | 糖衣机,除尘式糖衣机,全自动糖衣机,泰州市长江制药机械有限公司 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 | TTCMS自助建站_网站建设_自助建站_免费网站_免费建站_天天向上旗下品牌 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 硬质合金模具_硬质合金非标定制_硬面加工「生产厂家」-西迪技术股份有限公司 | 探伤仪,漆膜厚度测试仪,轮胎花纹深度尺厂家-淄博创宇电子 | 磁棒电感生产厂家-电感器厂家-电感定制-贴片功率电感供应商-棒形电感生产厂家-苏州谷景电子有限公司 | 步入式高低温测试箱|海向仪器| VI设计-LOGO设计公司-品牌设计公司-包装设计公司-导视设计-杭州易象设计 | 智能垃圾箱|垃圾房|垃圾分类亭|垃圾分类箱专业生产厂家定做-宿迁市传宇环保设备有限公司 | 西安文都考研官网_西安考研辅导班_考研培训机构_西安在职考研培训 | 苏州工作服定做-工作服定制-工作服厂家网站-尺品服饰科技(苏州)有限公司 | 淬火设备-钎焊机-熔炼炉-中频炉-锻造炉-感应加热电源-退火机-热处理设备-优造节能 | 全国国际学校排名_国际学校招生入学及学费-学校大全网 | 深圳市八百通智能技术有限公司官方网站 | 14米地磅厂家价价格,150吨地磅厂家价格-百科 | 圆盘鞋底注塑机_连帮鞋底成型注塑机-温州天钢机械有限公司 | 首页|成都尚玖保洁_家政保洁_开荒保洁_成都保洁 | 废气处理设备-工业除尘器-RTO-RCO-蓄热式焚烧炉厂家-江苏天达环保设备有限公司 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 | 广州监控安装公司_远程监控_安防弱电工程_无线wifi覆盖_泉威安防科技 | 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 喷砂机厂家_自动除锈抛丸机价格-成都泰盛吉自动化喷砂设备 | 四探针电阻率测试仪-振实密度仪-粉末流动性测定仪-宁波瑞柯微智能 | 伸缩节_伸缩器_传力接头_伸缩接头_巩义市联通管道厂 | 上海恒驭仪器有限公司-实验室平板硫化机-小型平板硫化机-全自动平板硫化机 | 房在线-免费房产管理系统软件-二手房中介房屋房源管理系统软件 | 中细软知识产权_专业知识产权解决方案提供商 | 阳光1号桔柚_无核沃柑_柑橘新品种枝条苗木批发 - 苧金网 | 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 |