Mybatis简单使用——存储过程(6)

    xiaoxiao2022-07-04  119

    一、调用存储过程:

    1)statementType="CALLABLE":表示要调用存储过程

    2){call procedure_name(params)}

    <select id= " selectUserPage” statement Type=” CALLABLE ” useCache=” false ” resultMap=” userMap ”> {call select user page( #{userName, mode=IN}, #{offset, mode=IN}, #{limit, mode=IN} , #{total, mode=OUT, jdbcType=BIGINT} )} </select>

    案例:

     CREATE PROCEDURE select_country(IN p_in int)     BEGIN      SELECT * from country WHERE id=p_in;     END;

    <select id="selectByIdCall" statementType="CALLABLE" useCache="false" resultMap="BaseResultMap" databaseId="mysql"> { call select_country( #{id,mode=IN} )} </select>

    注:

    1、OUT 模式的参数必须指定jdbcType

    2、存储过程方式不支持MyBatis 的二级缓存,useCache 属性设置为false 。

    二、在Oracle 中使用游标参数的存储过程

    create or replace procedure SELECT COUNTRIES(              ref curl out sys refcursor ,              ref 一cur2 out sys_refcursor) is begin             open ref_curl for select * from country where id < 3;             open ref cur2 for select * from country where id >= 3 ; end SELECT COUNTRIES ;

    调用:

    <select id= ” selectCountries ” statementType= ” CALLABLE ” useCache=” false ” > {call SELECT COUNTRIES( #{listl , mode=OUT , jdbcType=CURSOR ,javaType=ResultSet , resultMap=BaseResultMap), #{list2, mode=OUT , jdbcType=CURSOR ,javaType=ResultSet, resultMap=BaseResultMap) )} </select>

    测试:

    Map<String, Object> params =new HashMap<String , Object> (); countryMapper . selectCountries{params ); List<Country> listl = (List<Country>) params.get ( ” listl”); List<Country> list2 = (List<Country>) params.get (” 1 工st2 ”) ;

    最新回复(0)