MyBatis调用oracle存储过程
Posted 一宿君
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis调用oracle存储过程相关的知识,希望对你有一定的参考价值。
数据表Emp
创建存储过程(根据性别获取用户信息(游标集合))
create or replace procedure proc_emp(
v_sex in char,
cur_emp out sys_refcursor --定义返回类型类型是游标
) is
begin
IF v_sex is null THEN --如果性别为空则查询所有
open cur_emp for select * from emp;
ELSE
open cur_emp for select * from emp where sex=v_sex;
END IF;
end proc_emp;
实体类Emp
package com.train.entity;
import java.util.Date;
import java.util.List;
/**
* @author 一宿君(CSDN : qq_52596258)
* @date 2021-07-09 09:43:19
*/
public class Emp {
private int empno;
private String ename;
private String job;
private String mgr;
private Date hiredate;
private float sal;
private float comm;
private int deptno;
private String sex;
private List<Emp> cur_emp;
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\\'' +
", job='" + job + '\\'' +
", mgr='" + mgr + '\\'' +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
", sex='" + sex + '\\'' +
", cur_emp=" + cur_emp +
'}';
}
public Emp() {
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getMgr() {
return mgr;
}
public void setMgr(String mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}
public float getComm() {
return comm;
}
public void setComm(float comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List<Emp> getCur_emp() {
return cur_emp;
}
public void setCur_emp(List<Emp> cur_emp) {
this.cur_emp = cur_emp;
}
}
EmpDaoMapper接口
/**
* @author 一宿君(CSDN : qq_52596258)
* @date 2021-07-09 09:44:05
*/
public interface EmpDaoMapper {
/**
* 根据实体类对象的性别属性,调用存储过程
* @param emp
* @return
*/
List<Emp> selectAllByEmpSex(Emp emp);
/**
* 根据map集合,调用存储过程
* @param map
* @return
*/
List<Emp> selectAllByMap(Map map);
}
EmpDaoMapperImpl实现类
/**
* @author 一宿君(CSDN : qq_52596258)
* @date 2021-07-09 09:45:50
*/
public class EmpDaoMapperImpl implements EmpDaoMapper {
SqlSession sqlSession = null;
EmpDaoMapper empDaoMapper = null;
@Override
public List<Emp> selectAllByEmpSex(Emp emp) {
sqlSession = MyBatisUtil.createSqlSession();
empDaoMapper = sqlSession.getMapper(EmpDaoMapper.class);
empDaoMapper.selectAllByEmpSex(emp);
MyBatisUtil.closeSqlSession(sqlSession);
return emp.getCur_emp();
}
@Override
public List<Emp> selectAllByMap(Map map) {
sqlSession = MyBatisUtil.createSqlSession();
empDaoMapper = sqlSession.getMapper(EmpDaoMapper.class);
empDaoMapper.selectAllByMap(map);
List<Emp> empList = (List<Emp>) map.get("cur_emp");
return empList;
}
}
EmpDaoMapper.xml映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace是命名空间,要与对应的接口的路径保持一致(如果没有创建该接口,MyBatis在内部编译时会自动创建)-->
<mapper namespace="com.train.dao.EmpDaoMapper">
<!--autoMapping开启自动映射-->
<resultMap id="empMap" type="com.train.entity.Emp" autoMapping="true">
</resultMap>
<!--参数是实体类对象属性-->
<select id="selectAllByEmpSex" parameterType="com.train.entity.Emp" statementType="CALLABLE">
{call proc_emp(
#{sex,jdbcType=VARCHAR,mode=IN},
#{cur_emp,jdbcType=CURSOR,mode=OUT,resultMap=empMap,javaType=java.sql.ResultSet}
)}
</select>
<!--参数是Map集合的键-->
<select id="selectAllByMap" parameterType="Map" statementType="CALLABLE">
{call proc_emp(
#{sex,jdbcType=VARCHAR,mode=IN},
#{cur_emp,jdbcType=CURSOR,mode=OUT,resultMap=empMap,javaType=java.sql.ResultSet}
)}
</select>
</mapper>
实体类参数对应的mapper映射文件
实体类参数测试
Emp emp = new Emp();
emp.setSex("男");
List<Emp> empList = new EmpDaoMapperImpl().selectAllByEmpSex(emp);
for (Emp e:empList){
System.out.println(e);
}
map集合参数对应的mapper映射文件
map参数测试类
Map map = new HashMap();
map.put("sex","男");
List<Emp> empList = new EmpDaoMapperImpl().selectAllByMap(map);
for (Emp e:empList){
System.out.println(e);
}
以上是关于MyBatis调用oracle存储过程的主要内容,如果未能解决你的问题,请参考以下文章
从java调用oracle存储过程时在mybatis mapper中映射多个out参数
mybatis 调用 oracle 存储过程 select into 无记录时NO_DATA_FOUND异常处理分析