在项目中遇到了一个要同时查询多好多张表的问题,在效率上的考虑,就用了存储过程。
try {
return (EShopVO)getHibernateTemplate().execute(new HibernateCallback() {
@Override
public EShopVO doInHibernate(Session session)
throws HibernateException, SQLException {
Connection conn = null;
CallableStatement cstmt = null;
session.flush();
//获取connection
conn = session.connection();
//把事物设为FALSE
conn.setAutoCommit(false);
String sql ="{call bplan.findproductbytype(?,?," +
"?,?,?,?,?,?)}";
cstmt = conn.prepareCall(sql);
//注册输出的参数的类型。
cstmt.registerOutParameter(3, Types.BIGINT);
cstmt.registerOutParameter(4, Types.VARCHAR);
cstmt.registerOutParameter(5, Types.VARCHAR);
cstmt.registerOutParameter(6, Types.INTEGER);
cstmt.registerOutParameter(7, Types.VARCHAR);
cstmt.registerOutParameter(8, Types.INTEGER);
cstmt.setString(1, type);
cstmt.setLong(2, proId);
//执行
cstmt.execute();
EShopVO shopVO = new EShopVO();
shopVO.setProductId(cstmt.getLong(3));
shopVO.setCommentNum(cstmt.getInt(8));
shopVO.setProductNum(cstmt.getInt(6));
shopVO.setProductPic1(cstmt.getString(5));
shopVO.setProductPrice(cstmt.getString(4));
shopVO.setProductTitle(cstmt.getString(7));
return shopVO;
}
});
} catch (Exception e) {
e.printStackTrace();
throw e;}
我把重要的步骤贴出来。
我这个事struts2+spring+hibernate做的。