/*** hibernate调用函数的例子* 不用配置*.hbm.xml文件,直接调用*/public void queryFunction(){try{session = sessionFactory.openSession();SQLQuery query = session.createSQLQuery("{Call f_login(?,?,?)}");//输入参数query.setInteger(0, 1);query.setString(1, "");query.setString(2, "");List list =query.list();}catch (RuntimeException re){if (null != tran){tran.rollback();}throw re;}finally{session.close();}}/*** hibernate调用函数的例子* @throws SQLException*/public void queryFunction2() throws SQLException{try{//第二种方法session = sessionFactory.openSession();Connection conn = session.connection();ResultSet rs =null;CallableStatement call = conn.prepareCall("{Call f_login(?,?,?)}");//输入参数call.setString(0,"abc");//输出参数call.registerOutParameter(1, Types.VARCHAR);call.registerOutParameter(2,Types.VARCHAR);rs = call.executeQuery();if(rs.next()){System.out.println("存储过程得到的第一个返回值是:"+rs.getString(1));System.out.println("存储过程得到的第二个返回值是:"+rs.getString(2));}}catch (RuntimeException re){if (null != tran){tran.rollback();}throw re;}finally{session.close();}}/*** hibernate调用函数的例子* @throws SQLException*/public void queryFunction3() throws SQLException{try{//第三种方法session = sessionFactory.openSession();tran = session.beginTransaction();//调用*.hbm.xml定义的存储过程(也可以调用*.hbm.xml定义的是SQL语句)Query query=session.getNamedQuery("login_function");List list = query.list();tran.commit();}catch (RuntimeException re){if (null != tran){tran.rollback();}throw re;}finally{session.close();}}{call f_login(?)}
java hibernate调用存储过程_java–hibernate中调用存储过程 | 学步园
最新推荐文章于 2021-03-13 10:35:26 发布