RT,如果你的懂得编写Oracle的存储过程那就更好懂了,下面直接上代码
spring配置Oracle数据源
这个是properties文件的内容
jdbc.driverClassName2=oracle.jdbc.driver.OracleDriver jdbc.url2=jdbc\:oracle\:thin\:@192.168.202.129\:1521\:orcl jdbc.username2=scott jdbc.password2=tiger <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName2}" /> <property name="url" value="${jdbc.url2}" /> <property name="username" value="${jdbc.username2}" /> <property name="password" value="${jdbc.password2}" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="baseDao" class="com.will.userDao.BaseDao" abstract="true"> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean> <bean id="userDao" class="com.will.userDao.UserDao" parent="baseDao" /> </beans>
先写个BaseDao的抽象类,然后直接继承这个抽象类就可以使用通用的方法了
public abstract class BaseDao { private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } }
public class UserDaoImpl extends BaseDao{ @SuppressWarnings("unchecked") public List<Emp> testProcedure2(String sql) { List<Emp> list = (List<Emp>) this.jdbcTemplate.execute(sql, new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { List<Emp> emps = new ArrayList<Emp>(); // TODO Auto-generated method stub cs.setInt(1, 10); cs.registerOutParameter(2, OracleTypes.CURSOR); cs.execute(); ResultSet rs = (ResultSet) cs.getObject(2); while (rs.next()) { Emp emp = new Emp(); emp.setEmpno(rs.getInt("empno")); emp.setEname(rs.getString("ename")); emp.setJob(rs.getString("job")); emp.setMgr(rs.getInt("mgr")); emp.setHiredate(rs.getDate("hiredate")); emp.setSal(rs.getInt("sal")); emp.setComm(rs.getInt("comm")); emp.setDeptno(rs.getInt("deptno")); emps.add(emp); } return emps; } }); return list; }
用junit测试是否调用成功,之间有通过server层再到dao层,
@Test public void testJTProcedure() { String sql = "{call sp_pro15(?,?)}"; List<Emp> list = this.um.testProcedure2(sql); for (Emp emp : list) { System.out.println(emp.getEmpno() + " --- " + emp.getEname() + " --- " + emp.getHiredate()); } }
结果:
能成功的,我就不显示了,因为Oracle装置虚拟机里的Linux系统里面,请见谅……