使用spring集成hibernate
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/tx/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 方法一,直接配置hibernate.cfg.xml -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 方法二使用dataSource数据源 -->
<!--创建sessionFactoryt --> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"></bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 数据库连接 -->
<property name="url" value="jdbc:oracle:thin:@localhost:1521:jbit"></property>
<property name="username" value="rong"></property>
<property name="password" value="rong"></property>
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 添加配置 -->
<property name="hibernateProperties">
<props>
<prop key="dialect" >org.hibernate.dialect.OracleDialect</prop>
<prop key="show_sql" >true</prop>
<prop key="format_sql" >true</prop>
</props>
</property>
<!-- 关系映射 -->
<property name="mappingResource">
<list>
<value>cn/bdqn/jboa/entity/CheckResult.hbm.xml</value>
<value>cn/bdqn/jboa/entity/ClaimVoucher.hbm.xml</value>
<value>cn/bdqn/jboa/entity/ClaimVoucherDetail.hbm.xml</value>
<value>cn/bdqn/jboa/entity/Department.hbm.xml</value>
<value>cn/bdqn/jboa/entity/Dictionary.hbm.xml</value>
<value>cn/bdqn/jboa/entity/Employee.hbm.xml</value>
<value>cn/bdqn/jboa/entity/Position.hbm.xml</value>
</list>
</property>
</bean> </beans>
hibernate中的hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5 <hibernate-configuration>
6
7 <session-factory>
8 <property name="connection.url">
9 jdbc:oracle:thin:@localhost:1521:jbit
10 </property>
11 <property name="connection.username">rong</property>
12 <property name="connection.password">rong</property>
13 <property name="connection.driver_class">
14 oracle.jdbc.driver.OracleDriver
15 </property>
16 <property name="dialect">
17 org.hibernate.dialect.OracleDialect
18 </property>
19 <property name="show_sql">true</property>
20 <property name="format_sql">true</property>
21 <mapping resource="cn/bdqn/jboa/entity/CheckResult.hbm.xml" />
22 <mapping resource="cn/bdqn/jboa/entity/ClaimVoucher.hbm.xml" />
23 <mapping resource="cn/bdqn/jboa/entity/ClaimVoucherDetail.hbm.xml" />
24 <mapping resource="cn/bdqn/jboa/entity/Department.hbm.xml" />
25 <mapping resource="cn/bdqn/jboa/entity/Dictionary.hbm.xml" />
26 <mapping resource="cn/bdqn/jboa/entity/Employee.hbm.xml" />
27 <mapping resource="cn/bdqn/jboa/entity/Position.hbm.xml" />
28 </session-factory>
29
30 </hibernate-configuration>
更改后,调用事务就变得很简单了
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
@Override
public Employee findById(Serializable id) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().get(Employee.class, id);
}
}
创建接口类
public interface ClaimVoucherDao {
public List<ClaimVoucher> find(int first,int pageSize);
}
回调机制
/**
* 报销单类
* @author Administrator
*
*/
public class ClaimVoucherDaoImpl extends HibernateDaoSupport
implements ClaimVoucherDao{ @SuppressWarnings("unchecked")
@Override
public List<ClaimVoucher> find(final int first, final int pageSize) { return this.getHibernateTemplate().executeFind(new HibernateCallback() { @Override
public Object doInHibernate(Session arg0)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
return arg0.createQuery(" from ClaimVoucher c")
.setFirstResult(first)
.setMaxResults(pageSize)
.list();
}
});
}
}
在xml中添加dao的实例
1 <!-- dao -->
2 <bean id="userDao" class="cn.bdqn.jboa.dao.UserDaoImpl">
3 <property name="sessionFactory" ref="sessionFactory"></property>
4 </bean>
5 <bean id="claimVoucherDao" class="cn.bdqn.jboa.dao.ClaimVoucherDaoImpl">
6 <property name="sessionFactory" ref="sessionFactory"></property>
7 </bean>
测试类
public class testClaim {
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
ClaimVoucherDao claimVoucherDao = (ClaimVoucherDao) ctx.getBean("claimVoucherDao");
System.out.println(claimVoucherDao.find(0, 3));
System.out.println(claimVoucherDao.find(4, 3));
}
}
使用spring集成hibernate的更多相关文章
- Spring集成Hibernate映射文件的4种方式
概要: 在Spring的applicationContext.xml中集成Hibernate映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少 ...
- Spring 集成 Hibernate 和 Struts 2
在Spring中集成Hibernate,实际上就是将Hibernate中用到的数据源DataSource. SessionFactory实例(通常使用Hibernate访问数据库时,应用程序会先创建S ...
- Spring 集成Hibernate的三种方式
首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件 ...
- Spring集成hibernate错误
八月 25, 2016 7:55:31 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule ...
- Spring 集成hibernate时配置连接释放模式
http://zmfkplj.iteye.com/blog/220822 程序出现一个奇怪的现象,用Quartz作业调度启动任务,运行一段时间后,任务会卡在一个查询接口处,久久不能运行完毕. 我本能的 ...
- Spring集成Struts、Hibernate----三大框架SSH(Spring、Struts和hibernate)
Spring 框架可以完成业务层的设计任务,Struts框架可以将表示层和业务层分离,而Hibernate框架可以提供灵活的持久层支持.下面介绍三大框架的集成环境: 1.配置Struts2. I.导入 ...
- Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean
spring集成hibernate由两种形式 1.继续使用Hibernate的映射文件*.hbm.xml 2.使用jpa形式的pojo对象, 去掉*.hbm.xml文件 一.继续使用Hibernate ...
- spring整合hibernate配置文件
Spring对hibernate配置文件hibernate.cfg.xml的集成,来取代hibernate.cfg.xml的配置 spring对hibernate配置文件hibernate.cfg.x ...
- javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate
1.hibernate译为"越冬",指的是给java程序员带来春天,因为java程序员无需再关心各种sql了: 2.hibernate通过java类生成数据库表,通过操作对象来映射 ...
随机推荐
- ASP.net MVC自定义错误处理页面的方法
在ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那 ...
- 基于RESTful标准的Web Api
微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...
- Linux及安全——模块
Linux及安全——模块 一.模块的编译.生成.测试.删除 1.编写模块代码 编写:gedit test.c 查看:cat test.c 2.查看版本信息 3.编写Makefile obj-m :这个 ...
- 如何编写Iveely搜索引擎插件
如果一个搜索引擎仅仅是网页搜索,那么将会是非常枯燥的,也不能根据业务需求扩展,还好Iveely在设计之初,就考虑了扩展性,预留插件功能,在不关闭服务或者停用服务的情况下,可以随时启用新插件或者禁用. ...
- How to use VS2012 remote debug Windows Azure Cloud Services
Background: Windows Azure Cloud Services 可以在本地调试,使用Visual Studio 2012 + 模拟器 Emulator.但是模拟器的工作状态和环境和真 ...
- C#基础知识系列六(静态类和静态类成员)
静态类 静态类与非静态类基本相同,但存在一个区别:静态类不能实例化. 也就是说,不能使用 new 关键字创建静态类类型的变量. 因为没有实例变量,所以要使用类名本身访问静态类的成员. 例如,如果名为 ...
- WCF入门(22)
前言 本还想写一集WCF入门教程的,心情实在不好,明天又还有面试,改天再写吧. 说一下今天遇到的入职坑.面试能坑,上班能坑,完全没想到入职也能坑.切身经历. 今年10月份想换工作,更新了一下简历,接到 ...
- 第二十九课:javascript异步处理
大家知道javascript中有多少方法能够实现异步处理吗?setTimeout(),setInterval()是最常用的两个.XMLHttpRequest对象,进行ajax请求时.postMessa ...
- NServiceBus 结合 RabbitMQ 使用
参考官方教程: Step by Step Guide 新建4个项目: A Console Application named Client A Console Application named Se ...
- 你也可以当面霸-Servlet与JSP的原理及特点
既然是面试系列,就是面试官和应聘者之间的对话.本文是采用一问一答的形式呈现给读者的,这样能有一个明确的考察点,不像理论知识那么枯燥. 01.什么是Servlet技术 Servlet是和平台无关的服务器 ...