Hibernate 调用查询存储过程,以及持久化存储过程
2010-08-06 21:24
337 查看
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;
mso-font-charset:1;
mso-generic-font-family:roman;
mso-font-format:other;
mso-font-pitch:variable;
mso-font-signature:0 0 0 0 0 0;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:-1610611985 1073750139 0 0 159 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-unhide:no;
mso-style-qformat:yes;
mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-fareast-font-family:宋体;
mso-bidi-font-family:"Times New Roman";
mso-font-kerning:1.0pt;}
.MsoChpDefault
{mso-style-type:export-only;
mso-default-props:yes;
font-size:10.0pt;
mso-ansi-font-size:10.0pt;
mso-bidi-font-size:10.0pt;
mso-ascii-font-family:Calibri;
mso-fareast-font-family:宋体;
mso-hansi-font-family:Calibri;
mso-font-kerning:0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:36.0pt 36.0pt 36.0pt 36.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
/* List Definitions */
@list l0
{mso-list-id:182019087;
mso-list-template-ids:-1240400606;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->
Hibernate
调用查询存储过程,以及持久化存储过程
hibernate
提供了在*.hbm.xml
中配置调用存储过程,并通过向用户提供session.getNamedQuery("…")
方法来调用配置的调用查询相关的
存储过程的方法,另外,hibernate
还提供了取得sql
的connection
的方法,从而能够通过connection
中存储过程调用相关的方法
来实现存储过程的调用。
注意这两种存储过程的调用方式是不同的,在Hiberante
是方便了查询,它可以在*.hbm.xml
中配置存储过程返 回值的信息,然后通过配置,就可以把返回值封装成对象的集合。而使用持久化的存储过程,那就还是要直接使用到JDBC
调用存储过程的API
即使用到
CallableStatement
对象。
下面我就使用代码来介绍这两种存储过程的调用
调用返回对象集合的存储过程
1
、创建person
表
create table person
(
id int identity primary key,
[name] varchar(50),
age int
)
2
、创建查询person
表中的信息的存储过程和插入数据的存储过程
CREATE PROC proc_getlist_per @name VARCHAR(50) AS
SELECT * FROM person WHERE [name]=@name
GO
create proc proc_inert_per @name varchar(30),@age int as
insert into person(name,age) values(@name,@age)
go
3
、在你的工程中生成Hibernate
映射,这里就不介绍了,下面重点介绍映射文件中的配置信息
4
、Person
对象的映射配置文件
Person.hbm.xml
中的映射信息是:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="model.Person" table="Person" schema="dbo" catalog="testDB">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="Name" length="32" not-null="true" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" not-null="true" />
</property>
</class>
<!--
下面
sql-query
节点是你自己添加的要调用的存储过程的信息
-->
<!-- name="proc_getlist"
是在代码中
session.getNamedQuery("pro_insert")
要使用的名称
,
callable="true"
表示要调用的是存储过程,
下面的
return
节点是要配置
要封装
存储过程中的返回的数据集
的对象信息
class="model.Person"
表示要封装到哪个对象,
其中
alias="person"
是这个要封装的对象的别名,可以随便取名
后面的
return-property
是要对应存储过程返回的数据集中对应的列要对应到
封装对象的那个属性字段
{call proc_getlist_per(?)}
这就是要调用存储过程的语法
?
是你的参数占位符
随后配置存储过程中的要输入的参数信息
则使用
<query-param name="name" type="String"/>
-->
<sql-query name="proc_getlist" callable="true">
<return alias="person" class="model.Person">
<return-property name="id" column="id"/>
<return-property name="name" column="name"/>
<return-property name="age" column="age" />
</return>
{call proc_getlist_per(?)}
<query-param name="name" type="String"/>
</sql-query>
</hibernate-mapping>
下面是DAO
中调用的方法
public List<Person> listPerson(){
session = HibernateSessionFactory.getSession();
Query query= null;
//
使用存储过程获取数据
query=session.getNamedQuery("proc_getlist");
query.setString(0, "
小强
");
return query.list();
}
在main
方法中调用
List<Person> list=dao.listPerson();
for(Person person:list){
System.out.println(person);
}
即可
下面介绍持久化的存储过程,不返回数据集的 。
没有返回数据集的存储过程就不能在*.hbm.xml
文件中配置了,那么它可以直接在DAO
中这样写
session=HibernateSessionFactory.getSession();
Transaction tx=null;
try{
tx = session.beginTransaction();
Connection con=session.connection();
String procedure = "{call proc_inert_per(?,?) }";
CallableStatement cstmt = con.prepareCall(procedure);
cstmt.setString(1,per.getName());
cstmt.setInt(2, per.getAge());
cstmt.executeUpdate();
tx.commit();
con.setAutoCommit(true);
}catch(Exception e){
e.printStackTrace();
}
这样就可以了
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;
mso-font-charset:1;
mso-generic-font-family:roman;
mso-font-format:other;
mso-font-pitch:variable;
mso-font-signature:0 0 0 0 0 0;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:-1610611985 1073750139 0 0 159 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-unhide:no;
mso-style-qformat:yes;
mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-fareast-font-family:宋体;
mso-bidi-font-family:"Times New Roman";
mso-font-kerning:1.0pt;}
.MsoChpDefault
{mso-style-type:export-only;
mso-default-props:yes;
font-size:10.0pt;
mso-ansi-font-size:10.0pt;
mso-bidi-font-size:10.0pt;
mso-ascii-font-family:Calibri;
mso-fareast-font-family:宋体;
mso-hansi-font-family:Calibri;
mso-font-kerning:0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:36.0pt 36.0pt 36.0pt 36.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
/* List Definitions */
@list l0
{mso-list-id:182019087;
mso-list-template-ids:-1240400606;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->
Hibernate
调用查询存储过程,以及持久化存储过程
hibernate
提供了在*.hbm.xml
中配置调用存储过程,并通过向用户提供session.getNamedQuery("…")
方法来调用配置的调用查询相关的
存储过程的方法,另外,hibernate
还提供了取得sql
的connection
的方法,从而能够通过connection
中存储过程调用相关的方法
来实现存储过程的调用。
注意这两种存储过程的调用方式是不同的,在Hiberante
是方便了查询,它可以在*.hbm.xml
中配置存储过程返 回值的信息,然后通过配置,就可以把返回值封装成对象的集合。而使用持久化的存储过程,那就还是要直接使用到JDBC
调用存储过程的API
即使用到
CallableStatement
对象。
下面我就使用代码来介绍这两种存储过程的调用
调用返回对象集合的存储过程
1
、创建person
表
create table person
(
id int identity primary key,
[name] varchar(50),
age int
)
2
、创建查询person
表中的信息的存储过程和插入数据的存储过程
CREATE PROC proc_getlist_per @name VARCHAR(50) AS
SELECT * FROM person WHERE [name]=@name
GO
create proc proc_inert_per @name varchar(30),@age int as
insert into person(name,age) values(@name,@age)
go
3
、在你的工程中生成Hibernate
映射,这里就不介绍了,下面重点介绍映射文件中的配置信息
4
、Person
对象的映射配置文件
Person.hbm.xml
中的映射信息是:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="model.Person" table="Person" schema="dbo" catalog="testDB">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="Name" length="32" not-null="true" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" not-null="true" />
</property>
</class>
<!--
下面
sql-query
节点是你自己添加的要调用的存储过程的信息
-->
<!-- name="proc_getlist"
是在代码中
session.getNamedQuery("pro_insert")
要使用的名称
,
callable="true"
表示要调用的是存储过程,
下面的
return
节点是要配置
要封装
存储过程中的返回的数据集
的对象信息
class="model.Person"
表示要封装到哪个对象,
其中
alias="person"
是这个要封装的对象的别名,可以随便取名
后面的
return-property
是要对应存储过程返回的数据集中对应的列要对应到
封装对象的那个属性字段
{call proc_getlist_per(?)}
这就是要调用存储过程的语法
?
是你的参数占位符
随后配置存储过程中的要输入的参数信息
则使用
<query-param name="name" type="String"/>
-->
<sql-query name="proc_getlist" callable="true">
<return alias="person" class="model.Person">
<return-property name="id" column="id"/>
<return-property name="name" column="name"/>
<return-property name="age" column="age" />
</return>
{call proc_getlist_per(?)}
<query-param name="name" type="String"/>
</sql-query>
</hibernate-mapping>
下面是DAO
中调用的方法
public List<Person> listPerson(){
session = HibernateSessionFactory.getSession();
Query query= null;
//
使用存储过程获取数据
query=session.getNamedQuery("proc_getlist");
query.setString(0, "
小强
");
return query.list();
}
在main
方法中调用
List<Person> list=dao.listPerson();
for(Person person:list){
System.out.println(person);
}
即可
下面介绍持久化的存储过程,不返回数据集的 。
没有返回数据集的存储过程就不能在*.hbm.xml
文件中配置了,那么它可以直接在DAO
中这样写
session=HibernateSessionFactory.getSession();
Transaction tx=null;
try{
tx = session.beginTransaction();
Connection con=session.connection();
String procedure = "{call proc_inert_per(?,?) }";
CallableStatement cstmt = con.prepareCall(procedure);
cstmt.setString(1,per.getName());
cstmt.setInt(2, per.getAge());
cstmt.executeUpdate();
tx.commit();
con.setAutoCommit(true);
}catch(Exception e){
e.printStackTrace();
}
这样就可以了
相关文章推荐
- Hibernate(十三)命名查询-批量处理和调用存储过程
- 今日收获总结(关于存储过程时间查询的一些方法以及字段截取的函数及调用示例)
- Hibernate-Criteria查询(3)DetachedCriteria构造查询条件、命名查询、原生SQL、调用存储过程(使用JDBC)
- Ibatis调用存储过程实现增删改以及分页查询
- Ibatis调用存储过程实现增删改以及分页查询
- Oracle 创建存储过程以及PlSql 调用 存储过程
- hibernate如何调用存储过程
- 通过hibernate去调用存储过程
- oracle查询优化,存储过程select表循环插入另一个表,以及索引重建
- hibernate--一级和二级缓存(使用Ehcache)以及查询缓存
- Hibernate 中 load方法与get方法以及Query查询与Criteria查询
- Hive总结(十二)Hive查询进阶--以及调用外部脚本
- hibernate调用存储过程
- JDBC的分页查询以及hibernate高级查询分页
- hibernate全部查询方式以及批量抓取
- hibernate的查询方法以及返回的值类型
- Hibernate 一级缓存(session级别)、二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题
- 通过hibernate去调用存储过程
- Hibernate一级缓存、二级缓存以及查询缓存的关系
- Hibernate DAO层调用存储过程方法