JavaEE——Mybatis(20)--MyBatis实用场景 3.存储过程
实际开发中,我们通常也会写一些存储过程,
MyBatis也支持对存储过程的调用
• 一个最简单的存储过程
delimiter $$ create procedure test() begin select 'hello'; end $$ delimiter ;
oracle中创建存储过程
• 存储过程的调用
1、 select标签中statementType=“CALLABLE”
2、标签体中调用语法:
{call procedure_name(#{param1_info},#{param2_info})}
<!-- public void getPageByProcedure(); 1、使用select标签定义调用存储过程 2、statementType="CALLABLE":表示要调用存储过程 3、{call procedure_name(params)} --> <select id="getPageByProcedure" statementType="CALLABLE" databaseId="oracle"> {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=PageEmp} )} </select> <resultMap type="com.atguigu.mybatis.bean.Employee" id="PageEmp"> <id column="EMPLOYEE_ID" property="id"/> <result column="LAST_NAME" property="email"/> <result column="EMAIL" property="email"/> </resultMap>
查询
/** * oracle分页: * 借助rownum:行号;子查询; * 存储过程包装分页逻辑 * @throws IOException */ @Test public void testProcedure() throws IOException{ SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try{ EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); OraclePage page = new OraclePage(); page.setStart(1); page.setEnd(5); mapper.getPageByProcedure(page); System.out.println("总记录数:"+page.getCount()); System.out.println("查出的数据:"+page.getEmps().size()); System.out.println("查出的数据:"+page.getEmps()); }finally{ openSession.close(); } }