存储过程和表值函数的区别
最简单的区别是存储过程是pr开头的,而存储过程是fn开头的,表值函数返回的是一个表结构的结果。如果mybatis用调用存储过程的方式去调用表值函数,就会抛出以下的异常:
exception:不能调用'xxxxx',因为'xxxx'是表值函数对象。
调用存储过程
如果要调用一个叫做“pr_test_a”的存储过程,需要传入参数并且有返回值。如下所示,resultMap中存的是返回值的字段名和类型,在select中调用存储过程,传入对应的参数
<resultMap type="java.util.HashMap" id="resultMap">
<result column="id" property="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
<result column="code" property="code" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result column="name" property="name" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result column="modified_time" property="modifiedTime" javaType="java.sql.Timestamp" jdbcType="TIMESTAMP"/>
</resultMap>
<select id="selectA" parameterType="java.util.HashMap" statementType="CALLABLE" resultMap="resultMap">
{
call pr_test_a(
#{id,mode=IN,jdbcType=INTEGER},
#{card_no,mode=IN,jdbcType=VARCHAR}
}
</select>
调用表值函数
就当做一张表来查询即可。如调用一个表值函数fn_test_b,返回结果到resultMap中。
<resultMap type="java.util.HashMap" id="resultMap">
<result column="fullid" property="fullid" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result column="seq" property="seq" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result column="material_id" property="materialId" javaType="java.lang.Integer" jdbcType="INTEGER"/>
</resultMap>
<select id="fn_bom_decomposition" parameterType="java.util.HashMap" resultMap="resultMap">
select * from fn_test_b(
#{material_id,mode=IN,jdbcType=INTEGER},
#{quantity,mode=IN,jdbcType=NUMERIC},
#{start_time,mode=IN,jdbcType=TIMESTAMP}
)
</select>