JdbcTemplate调用存储过程!
程序员文章站
2022-03-28 08:36:13
JdbcTemplate调用存储过程!过程语句为:--根据员工编号,查询所在部门名称和职位信息create or replace procedure getDnameJob(empno1 in varchar2,dname2 out varchar2,job2 out varchar2)isbeginselect e.job,d.dnameinto job2,dname2from ......
JdbcTemplate调用存储过程!
过程语句为:
--根据员工编号,查询所在部门名称和职位信息
create or replace procedure getDnameJob(empno1 in varchar2,dname2 out varchar2,job2 out varchar2)
is
begin
select e.job,d.dname
into job2,dname2
from emp e,dept d
where e.empno=empno1 and d.deptno=e.deptno;
end;
package com.dqw.dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import javax.annotation.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.dqw.po.Dept;
import com.dqw.po.Emp;
/**
*
* <p>Title: EmpDaoImpl</p>
* <p>Description:员工持久层实现类 </p>
* @author 丁乾文
* @date 2019年1月6日
* @version 1.0
*/
@Repository
public class EmpDaoImpl implements EmpDao {
@Resource
private JdbcTemplate jdbcTemplate;
@Override
public Emp getDnameJob(Integer empno) {
Emp emp = jdbcTemplate.execute(new CallableStatementCreator() {
@Override
public CallableStatement createCallableStatement(Connection conn) throws SQLException {
String sql="{call getDnameJob(?,?,?)}";
CallableStatement prepareCall = conn.prepareCall(sql);
prepareCall.setInt(1, empno);
prepareCall.registerOutParameter(2, Types.VARCHAR);
prepareCall.registerOutParameter(3, Types.VARCHAR);
return prepareCall;
}
}, new CallableStatementCallback<Emp>() {
@Override
public Emp doInCallableStatement(CallableStatement call) throws SQLException, DataAccessException {
call.execute();
Emp ep=new Emp();//定义员工对象
Dept dept=new Dept();//定义职位对象
dept.setDname(call.getString(2));
ep.setJob(call.getString(3));
ep.setDept(dept);
return ep;
}
});
return emp;
}
}
本文地址:https://blog.csdn.net/weixin_42756198/article/details/85936526