【问题标题】:Lazy fetching in MyBatisMyBatis 中的延迟抓取
【发布时间】:2016-12-07 05:31:43
【问题描述】:

我找不到如何实现延迟加载(即使在 MyBatis 文档中)。

我的ma​​pper xml如下图:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.FooMyBatisLazyFetch">
        <select id="callFooProc"
                parameterType="com.example.FooProcBundle"
                statementType="CALLABLE">
            {call FooProc(
                    #{arg1,  jdbcType=VARCHAR, mode=IN},
                    #{arg2,  jdbcType=VARCHAR, mode=IN},
                    #{arg3,  jdbcType=VARCHAR, mode=IN},
                    #{error, jdbcType=NUMERIC, mode=OUT},
                    #{res2,  jdbcType=CURSOR,  mode=OUT, resultMap=FooProcResult}
                )
            }
        </select>

        <resultMap id="FooProcResult" type="com.example.FooProcResult">
            <result property="bar1" column="barcol1"/>
            <result property="bar2" column="barcol2"/>
            <result property="bar3" column="barcol3"/>
            <result property="bar4" column="barcol4"/>
            <result property="bar5" column="barcol5"/>
        </resultMap>
    </mapper>

Pojo 类:

    public class FooProcResult {
        private String bar1;
        private String bar2;
        private String bar3;
        private String bar4;
        private String bar5;
    }        

    public class FooProcBoondle {
        private String arg1;
        private String arg2;
        private String arg3;
        private Integer error;
        private List<FooProcResult> res2;
        //getters,setters, etc
    }

及使用代码;

    FooProcBundle bundle = new FooProcBundle();
    bundle.setArg1("foo");
    bundle.setArg2("bar");
    bundle.setArg3("baz");
    fooMyBatisLazyFetch.callFooProc(bundle);
    Integer error = bundle.getError();
    if(error == 123) /*some condition*/ {
        List<FooProcResult> res2 = bundle.getRes2();
        // iterate res2
    --->// Only here CURSOR may be opened and executed
    }

即我不想获取 res2 ,除非我的代码明确要求它。那个特定的光标很重,我不想在不需要时执行它(但 mybatis 会这样做)。

我还想将此应用于类似生成器的过程(Oracle 称它们为“流水线表函数”,它们会产生结果、休眠并等到调用者获取下一行 - 唤醒并计算下一个。通常他们这样调用:SELECT * FROM TABLE(GenProc(arg1,arg2)) .

关于实现这一点所需的配置有什么想法吗?

【问题讨论】:

  • 程序延迟加载?你现在有解决办法吗?

标签: java oracle lazy-loading mybatis pipelined-function


【解决方案1】:

过程输出游标参数在org.apache.ibatis.executor.resultset.DefaultResultSetHandler类中处理 在方法 handleOutputParameters 中,然后在方法 handleRefCursorOutputParameter 中。您会注意到,当前状态下的代码不允许您搜索,唯一使用的“自定义选项”是必须提供的 resultMap。我还希望有更多选项,例如延迟加载、自定义结果处理程序和一些能够监控实际执行时间和获取时间的日志。

这可以在 JDBC 中实现,并且需要在框架中未实现的配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2015-12-07
    • 2016-09-23
    • 2014-05-05
    • 2020-07-20
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多