hibernate中调用存储过程
2013-01-25 17:08
274 查看
/**
* 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();
}
}
<!--定义一个调用存储过程的命名SQL查询-->
<sql-query name="login_function" callable="true">
<!-- 返回结果 -->
<return alias="users" class="">
<!-- 将查询到的数据列转换成实体的属性 -->
<return-property name="" column=""/>
</return>
{call f_login(?)}
</sql-query>
* 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();
}
}
<!--定义一个调用存储过程的命名SQL查询-->
<sql-query name="login_function" callable="true">
<!-- 返回结果 -->
<return alias="users" class="">
<!-- 将查询到的数据列转换成实体的属性 -->
<return-property name="" column=""/>
</return>
{call f_login(?)}
</sql-query>
相关文章推荐
- hibernate调用存储过程及处理返回集合
- hibernate5 调用 oracle 存储过程和函数
- Hibernate调用带返回值的存储过程的方法
- Hibernate3调用存储过程用法
- Hibernate(十二)命名查询-批量处理和调用存储过程
- Hibernate3调用带返回参数的mysql存储过程
- Hibernate性能优化之抓取策略和调用存储过程
- java调用存储过程(jdbc & hibernate)
- 通过 Hibernate 调用存储过程
- Hibernate 调用存储过程
- 今天查找使用Hibernate调用存储过程中发现的有价值的帖子
- Hibernate 调用SQL Server 2005 分页存储过程
- MySQL 存储过程实例 与 ibatis/mybatis/hibernate/jdbc 如何调用存储过程
- mysql 存储过程调用 mybatis/hibernate
- hibernate 调用mysql存储过程
- java调用存储过程的三种方式(包含通过hibernate调用)
- hibernate中调用存储过程
- Hibernate3调用存储过程用法
- Hibernate 调用无参存储过程
- hibernate 调用存储过程