使用spring集成hibernate

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 8     http://www.springframework.org/schema/context
 9     http://www.springframework.org/schema/tx/spring-context-3.2.xsd
10     http://www.springframework.org/schema/aop
11     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
12
13     <!-- 方法一,直接配置hibernate.cfg.xml -->
14     <bean id="sessionFactory"         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
15         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
16     </bean>
17     <!-- 方法二使用dataSource数据源 -->
18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
19         <!-- 数据库连接 -->
20         <property name="url" value="jdbc:oracle:thin:@localhost:1521:jbit"></property>
21         <property name="username" value="rong"></property>
22         <property name="password" value="rong"></property>
23         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">      </property>
24     </bean>
25     <bean id="sessionFactory"     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
26         <property name="dataSource" ref="dataSource"></property>
27         <!-- 添加配置 -->
28         <property name="hibernateProperties">
29             <props>
30                 <prop key="dialect" >org.hibernate.dialect.OracleDialect</prop>
31                 <prop key="show_sql" >true</prop>
32                 <prop key="format_sql" >true</prop>
33             </props>
34         </property>
35         <!-- 关系映射 -->
36         <property name="mappingResource">
37             <list>
38                 <value>cn/bdqn/jboa/entity/CheckResult.hbm.xml</value>
39                 <value>cn/bdqn/jboa/entity/ClaimVoucher.hbm.xml</value>
40                 <value>cn/bdqn/jboa/entity/ClaimVoucherDetail.hbm.xml</value>
41                 <value>cn/bdqn/jboa/entity/Department.hbm.xml</value>
42                 <value>cn/bdqn/jboa/entity/Dictionary.hbm.xml</value>
43                 <value>cn/bdqn/jboa/entity/Employee.hbm.xml</value>
44                 <value>cn/bdqn/jboa/entity/Position.hbm.xml</value>
45             </list>
46         </property>
47     </bean>
48
49 </beans>    

hibernate中的hibernate.cfg.xml

 1 <?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>

更改后,调用事务就变得很简单了

1 public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
2
3     @Override
4     public Employee findById(Serializable id) {
5         // TODO Auto-generated method stub
6         return this.getHibernateTemplate().get(Employee.class, id);
7     }
8
9 }

创建接口类

1 public interface ClaimVoucherDao {
2     public List<ClaimVoucher> find(int first,int pageSize);
3 }

回调机制

 1 /**
 2  * 报销单类
 3  * @author Administrator
 4  *
 5  */
 6 public class ClaimVoucherDaoImpl extends HibernateDaoSupport          implements ClaimVoucherDao{
 7
 8     @SuppressWarnings("unchecked")
 9     @Override
10     public List<ClaimVoucher> find(final int first, final int pageSize) {
11
12
13         return    this.getHibernateTemplate().executeFind(new HibernateCallback() {
14
15             @Override
16             public Object doInHibernate(Session arg0)
17                     throws HibernateException, SQLException {
18                 // TODO Auto-generated method stub
19                 return arg0.createQuery(" from ClaimVoucher c")
20                         .setFirstResult(first)
21                         .setMaxResults(pageSize)
22                         .list();
23             }
24         });
25     }
26 }

在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));
    }
}
时间: 2024-11-03 22:23:47

使用spring集成hibernate的相关文章

Spring集成Hibernate

在Spring中集成Hibernate,实际上就是将Hibernate中用到的数据源DataSource. SessionFactory实例(通常使用Hibernate访问数据库时,应用程序会先创建SessionFactory实例)以及事务管理器都交由Spring容器管理.整合时,可以只使用Spring配置文件(通常是applicationContext.xml文件,也可用其它命名)来完成两个框架初始化任务,不再使用hibernate.cfg.xml配置文件. 定义数据源DataSource <

Spring集成Hibernate(基于XML和注解配置)

配置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:context="http://www.springfram

Spring 集成Hibernate的三种方式

首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件 第一种集成方式:首先定义一个MySessionFactory的类 package com.tz.core; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.springfr

Spring集成hibernate错误

八月 25, 2016 7:55:31 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:struts3_wsdc_cdgl' did not find a matching property.八月

xml配置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:context="http://www.springframework.org/sch

Spring 集成hibernate时配置连接释放模式

http://zmfkplj.iteye.com/blog/220822 程序出现一个奇怪的现象,用Quartz作业调度启动任务,运行一段时间后,任务会卡在一个查询接口处,久久不能运行完毕. 我本能的发现是不是数据库连接池数量不够? 于是我加带了连接池的大小.但是,问题依然出现. 这时,我就只能使用debug+log来调试了.调试后发现,当发生查询接口执行卡住现象时,程序连接池的确是不够用了,但是其他的任务有的已提前运行完了,有始有终,当前面的任务运行完,它所占用的连接应该会释放啊,这样连接不会

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread spring集成Hibernate sessionfactory.getcurrentSession报错

sessionFactory.getCurrentSession()是要基于事务的,解决方法为在javaconfig配置类使用@EnableTransactionManagement注解  并且配置transactionManager bean. 在报错方法中使用@Transactional注解 原文地址:https://www.cnblogs.com/alexmason236/p/9767091.html

Spring集成Struts、Hibernate----三大框架SSH(Spring、Struts和hibernate)

Spring 框架可以完成业务层的设计任务,Struts框架可以将表示层和业务层分离,而Hibernate框架可以提供灵活的持久层支持.下面介绍三大框架的集成环境: 1.配置Struts2. I.导入相关的JAR包. II.修改web.xml文件.Struts2的核心控制是通过过滤器(Filter)来实习的,所以在web.xml文件中需要进行过滤器的配置一边加载strust2框架.web.xml文件的代码如下: 1 <!-- 配置Struts2框架的核心Filter --> 2 <fil

Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean

spring集成hibernate由两种形式 1.继续使用Hibernate的映射文件*.hbm.xml 2.使用jpa形式的pojo对象, 去掉*.hbm.xml文件 一.继续使用Hibernate的映射文件*.hbm.xml 此时Spring的配置文件中的SeesionFactory需要使用org.springframework.orm.hibernate.LocalSessionFactoryBean [html] view plain copy <bean id="sessionF