mybatis:存储过程procedure

实际开发中,我们通常也会写一些存储过程,MyBatis也支持对存储过程的调用

一个最简单的存储过程delimiter $$create procedure test()beginselect 'hello';end $$delimiter ;

存储过程的调用

       1、select标签中statementType=“CALLABLE”

       2、标签体中调用语法:{call procedure_name(#{param1_info},#{param2_info})}

 

二、使用

1、编写一个存储过程:

create or replace procedure 
    hello_test(
        p_start in int,p_end in int,p_count out int,p_emps out sys_refcursor
    ) AS 
BEGIN
    select count(1) into p_count from TB_EMPLOYEE;
    open p_emps FOR
        select * from (select rownum rn ,c.* from TB_EMPLOYEE c where rownum <=p_end) where rn >=p_start;
end hello_test;

可以查询所有的存储过程

select * from user_source;

2 调用

MyBatis对存储过程的游标提供了一个JdbcType=CURSOR的支持可以智能的把游标读取到的数据,映射到我们声明的结果集中

<!--public void getPageByProcedure(OraclePage page);
    -->
    <!-- public void getPageByProcedure();
	1、使用select标签定义调用存储过程
	2、statementType="CALLABLE":表示要调用存储过程
	3、{call procedure_name(params)} :固定写法

	jdbcType:需要参考枚举类JdbcType
	-->

    <select id="getPageByProcedure" statementType="CALLABLE" databaseId="oracle" useCache="false">
        {call hello_test(
                #{start,mode=IN,jdbcType=INTEGER},
                #{end,mode=IN,jdbcType=INTEGER},
                #{count,mode=OUT,jdbcType=INTEGER},
                #{emps,mode=OUT,jdbcType=CURSOR,javaType=ResultSet,resultMap=oraclePage}
        )}
    </select>
    <resultMap id="oraclePage" type="com.mybatis.entity.Employee">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender" />
        <result column="last_name" property="lastName" />
    </resultMap>
		
 @Test
    public void testProcedure() {
        SqlSession sqlSession = null;
        try {
            sqlSession = EmployeeTest.getSqlSession();
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            OraclePage page = new OraclePage();
            page.setStart(1);
            page.setEnd(4);
            mapper.getPageByProcedure(page);

            System.out.println("总记录数:"+page.getCount());
            System.out.println("查询的数据员工有几个:"+page.getEmps().size());
            System.out.println("员工数据:"+page.getEmps());
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            sqlSession.close();
        }


    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis可以使用存储过程来访问数据库。在使用存储过程的方法中,需要将statementType设置为CALLABLE。另外,在使用select标签调用存储过程时,由于存储过程方式不支持MyBatis的二级缓存,为了避免缓存配置出错,可以直接将select标签的useCache属性设置为false。 要查看存储过程的详细内容,可以使用"show procedure status where db='数据库名';"的SQL语句来查询。这将返回存储过程的详细信息,包括存储过程的名称、参数、返回值等等。 如果需要删除存储过程,可以使用"SHOW CREATE PROCEDURE '存储过程名';"的SQL语句来获取存储过程的创建语句,然后根据创建语句来删除存储过程。请注意,在这个SQL语句中,存储过程名需要用Esc键下面的那个键来包裹起来。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MyBatis学习:存储过程](https://blog.csdn.net/sssxlxwbwz/article/details/122873916)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mybatis使用存储过程](https://blog.csdn.net/m0_37561039/article/details/83716915)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值