使用Spring Jdbc (4)调用存储过程

private class ExcuteProcedure extends StoredProcedure{ private static final String SQL ="p_out";//pro_get为存储过程的名字 public ExcuteProcedure(){ this.setDataSource(getDataSource());//已经配置了dataSource setSql(SQL);// declareParameter(new SqlParameter("in_id",Types.INTEGER));//in_id为输入参数 declareParameter(new SqlOutParameter("out_num",Types.INTEGER));//为输出参数 compile();//编译 } public int execute(int userId){ Map map = new HashMap(); map.put("in_id", userId); Map outMap = execute(map);//执行 return (Integer) outMap.get("out_num"); } } public int executPro(int userId){ return new ExcuteProcedure().execute(userId); }

 

public void testExecutPro() throws Exception { /* * mysql> delimiter // mysql> create procedure p_out(in in_id int,out out_num int) -> begin -> select userId from tb_user where userId = in_id; -> end -> delimiter ; Query OK, 0 rows affected (0.03 sec) */ ApplicationContext ctx = new ClassPathXmlApplicationContext("app*.xml"); FormJdbcDao3 dao = (FormJdbcDao3) ctx.getBean("formDao3"); int userId = 17; System.out.println(dao.executPro(userId)); }

你可能感兴趣的:(使用Spring Jdbc (4)调用存储过程)