1. DAO接口
public interface EmpDao {
void addEmp1();
void findEmp(Map<String,Object> map);
void findEmpById(Map<String,Object> map);
void findEmpBySalary(Map<String,Object> map);
}
2.無參數無返回值存儲過程調用
<update id="addEmp1" statementType="CALLABLE">
{CALL addEmp_pro()}
</update>
測試略
3.無參數返回值爲遊標類型
mapper文件
<mapper namespace="com.baizhi.mybatis.EmpDao">
<resultMap id="FINDEMP_MAP" type="com.baizhi.bean.Emp">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="salary" column="salary"></result>
</resultMap>
<update id="findEmp" statementType="CALLABLE">
{CALL findAllEmp_pro(#{cur_emp,mode=OUT,jdbcType=CURSOR,javaType=ResultSet,resultMap=FINDEMP_MAP})}
</update>
</mapper>
測試類
@Test
public void test1() throws Exception {
SqlSession sqlSession = openSqlSession();
EmpDao dao = sqlSession.getMapper(EmpDao.class);
Map<String, Object> map = new HashMap<>();
map.put("cur_emp", OracleTypes.CURSOR);
dao.findEmp(map);
List<Emp> app = (List<Emp>) map.get("cur_emp");
for (Emp emp : app) {
System.out.println(emp);
}
}
4.有參數有返回值
mapper文件
<update id="findEmpById" statementType="CALLABLE">
{CALL findEmpById_pro(#{eid,mode=INOUT,jdbcType=INTEGER},
#{ename,mode=OUT,jdbcType=VARCHAR},
#{esalary,mode=OUT,jdbcType=DOUBLE})}
</update>
測試調用
@Test
public void test3() throws Exception {
SqlSession sqlSession = openSqlSession();
EmpDao dao = sqlSession.getMapper(EmpDao.class);
Map<String, Object> map = new HashMap<>();
map.put("eid", 100);
dao.findEmpById(map);
System.out.println(map);
}
5.有參數返回值爲遊標類型
mapper文件
<update id="findEmpBySalary" statementType="CALLABLE">
{CALL findEmpBySalary_pro(#{esalary,mode=IN,jdbcType=DOUBLE},
#{cur_emp,mode=OUT,jdbcType=CURSOR,javaType=ResultSet,resultMap=FINDEMP_MAP})}
</update>
測試調用
@Test
public void test4() throws Exception {
SqlSession sqlSession = openSqlSession();
EmpDao dao = sqlSession.getMapper(EmpDao.class);
Map<String, Object> map = new HashMap<>();
map.put("esalary", 10000);
dao.findEmpBySalary(map);
System.out.println(map);
//獲取到emp的結果集合
List<Emp> emps = (List<Emp>) map.get("cur_emp");
for (Emp emp : emps) {
System.out.println(emp);
}
}
附測試數據和存儲過程
--存儲過程1
create or replace procedure addEmp_pro
as
begin
insert into emp values(emp_seq.nextval,'Tom',1000);
commit;
end;
--存儲過程2
create or replace procedure findAllEmp_pro( cur_emp OUT sys_refcursor)
as
begin
open cur_emp for select * from emp;
end;
--存儲過程3
create or replace procedure findEmpById_pro(eid in out number,ename out varchar2,esalary out number)
as
begin
select * into eid,ename,esalary from emp where id=eid;
end;
--存儲過程4
create or replace procedure findEmpBySalary_pro(esalary number,cur_emp OUT sys_refcursor)
as
begin
open cur_emp for select * from emp where salary=esalary;
end;
drop table emp;
create table emp(
id number(9) primary key,
name varchar2(50),
salary number(9,3)
);
insert into emp select employee_id,first_name,salary from employees;
commit;
select * from emp;