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

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

而後就有了mybatis-callable,得到方式以下:git

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


配置是這樣的:github

<?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時攔截器纔會起做用:apache

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

而後Dao或許是這樣的,接口也是同樣的:mybatis

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);
}

===========================================spa

返回單個參數可是不是遊標的話就這樣code

<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);
}

===========================================
xml

返回多個參數,裏面啥都有:接口

<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是存儲過程的參數名,按需來取就好了。ci

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

上面看上去規則有點麻煩,其實用接口作的話就不用考慮是selectList仍是selectOne了,直接接收返回值就能夠。

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

本身定義結果集的話參考一下SimpleResultHandler的實現就好了,反正把本身實現的按照下面這麼配就好了。不配resultHandler的話默認就是SimpleResultHandler了。

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


完。

相關文章
相關標籤/搜索