最近没什么事,看核心技术,偶然看到jdbc调用存储过程那里:
{call 存储过程名(?,?)}
这种语法,用习惯了hql,初次看起来有些怪异,不过在某些需要大批量修改数据库的时候,调用存储过程,可以合理的利用数据亏本身的性能,同时优化逻辑,简化代码,确实比起在代码中执行要好。
下面给出hibernate的调用代码:
tx = session.beginTransaction();
Connection con=session.connection();
String procedure = "{call batchUpdateStudent(?) }";
CallableStatement cstmt = con.prepareCall(procedure);
cstmt.setInt(1,0); //把年龄参数设为0
cstmt.executeUpdate();
tx.commit();
感觉上,还是jdbc的操作,
下面是带返回值的
CallableStatement statement = getSession().connection().prepareCall(
"{call sp_get_keyword_code(?, ? ,? ,?, ?)}");
statement.setString(1, parentCode);
statement.setInt(2, 2);
statement.setString(3, "W");
statement.setInt(4, 1);
statement.registerOutParameter("out_code", Types.VARCHAR);
statement.executeUpdate();
String keycode = statement.getString("out_code");
out_code应该是输出参数的名字,因为环境问题,没法直接测试,后面测试完会更新的。