讓Mybatis查詢存儲過程不那么另類

jopen 10年前發布 | 30K 次閱讀 MyBatis3 持久層框架 MyBatis

Mybatis默認查詢存儲過程的返回值是使用參數傳來傳去的,從參數里獲取返回值總讓我感覺怪怪的,特別是在使用接口做Dao的時候,破壞了Dao接口方法的統一性。

然后就有了mybatis-callable,獲得方式如下:

<dependencies>
  ...
    <dependency>
        <groupId>com.github.miemiedev</groupId>
        <artifactId>mybatis-callable</artifactId>
        <version>1.0</version>
    </dependency>
 ...
</dependencies>

 

配置是這樣的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
    <plugins>
        <plugin interceptor="com.github.miemiedev.mybatis.callable.CallableConvertInterceptor">        </plugin>
    </plugins>
</configuration>

創建一個查詢,需要注意的是只有statementType為CALLABLE時攔截器才會起作用:

<select id="query2" statementType="CALLABLE">
    <![CDATA[
        {call test_proc2(
            #{acResult,mode=OUT,jdbcType=CURSOR,javaType=ResultSet, resultMap=hashMap},
            #{userType},
            #{branchCode}
        )}
    ]]>
</select>

然后Dao或許是這樣的,接口也是一樣的:

public List<Map<String, Object>> query2(String userType, String branchCode){
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("userType",userType);
    params.put("branchCode",branchCode);
    //存儲過程只返回一個游標,則使用selectList返回List
    return getSqlSession().selectList("db.table.user.query2", params);
}

===========================================

返回單個參數但是不是游標的話就這樣

<select id="query3" statementType="CALLABLE">
    <![CDATA[
        {call test_proc3(
            #{retCode,mode=OUT,jdbcType=INTEGER},
            #{userType},
            #{branchCode}
        )}
    ]]>
</select>
public Integer query3(String userType, String branchCode){
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("userType",userType);
    params.put("branchCode",branchCode);
    //存儲過程只有一個返回值,并且不是游標,則使用selectOne
    return getSqlSession().selectOne("db.table.user.query3", params);
}

===========================================

返回多個參數,里面啥都有:

<select id="query" statementType="CALLABLE">
    <![CDATA[
        {call test_proc(
            #{retCode,mode=OUT,jdbcType=INTEGER},                
            #{acResult,mode=OUT,jdbcType=CURSOR,javaType=ResultSet, resultMap=hashMap},
            #{userType},
            #{branchCode}
        )}
    ]]>
</select>
public Map<String, Object> query(String userType, String branchCode){
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("userType",userType);
    params.put("branchCode",branchCode);
    //存儲過程只有一個返回值,并且不是游標,則使用selectOne
    return getSqlSession().selectOne("db.table.user.query", params);
}

Map中包含所有存儲過程輸出的結果,Key是存儲過程的參數名,按需來取就行了。

=============================================

自己定義結果集的話參考一下SimpleResultHandler的實現就行了,反正把自己實現的按照下面這么配就行了。不配resultHandler的話默認就是SimpleResultHandler了。

<plugin interceptor="com.github.miemiedev.mybatis.callable.CallableConvertInterceptor">
    <property name="resultHandler" value="com.github.miemiedev.mybatis.callable.handler.SimpleResultHandler"/>
</plugin>

 

完。
來自:http://my.oschina.net/miemiedev/blog/267432

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!