<?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的更多相关文章

  1. Spring集成Hibernate映射文件的4种方式

    概要: 在Spring的applicationContext.xml中集成Hibernate映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少 ...

  2. Spring 集成 Hibernate 和 Struts 2

    在Spring中集成Hibernate,实际上就是将Hibernate中用到的数据源DataSource. SessionFactory实例(通常使用Hibernate访问数据库时,应用程序会先创建S ...

  3. Spring 集成Hibernate的三种方式

    首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件 ...

  4. Spring集成hibernate错误

    八月 25, 2016 7:55:31 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule ...

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

    http://zmfkplj.iteye.com/blog/220822 程序出现一个奇怪的现象,用Quartz作业调度启动任务,运行一段时间后,任务会卡在一个查询接口处,久久不能运行完毕. 我本能的 ...

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

    Spring 框架可以完成业务层的设计任务,Struts框架可以将表示层和业务层分离,而Hibernate框架可以提供灵活的持久层支持.下面介绍三大框架的集成环境: 1.配置Struts2. I.导入 ...

  7. Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean

    spring集成hibernate由两种形式 1.继续使用Hibernate的映射文件*.hbm.xml 2.使用jpa形式的pojo对象, 去掉*.hbm.xml文件 一.继续使用Hibernate ...

  8. spring整合hibernate配置文件

    Spring对hibernate配置文件hibernate.cfg.xml的集成,来取代hibernate.cfg.xml的配置 spring对hibernate配置文件hibernate.cfg.x ...

  9. javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate

    1.hibernate译为"越冬",指的是给java程序员带来春天,因为java程序员无需再关心各种sql了: 2.hibernate通过java类生成数据库表,通过操作对象来映射 ...

随机推荐

  1. ListView中多个EditText设置焦点 多次点击异常报错

    08-17 18:23:09.825: ERROR/AndroidRuntime(1608): FATAL EXCEPTION: main 08-17 18:23:09.825: ERROR/Andr ...

  2. [CareerCup] 8.3 Musical Jukebox 点唱机

    8.3 Design a musical jukebox using object-oriented principles. CareerCup这书实在是太不负责任了,就写了个半调子的程序,说是完整版 ...

  3. 软件工程(GZSD2015)第二次作业文档模板

    题目: (此处列出题目) 需求分析: 基本功能 基本功能点1 基本功能点2 ... 扩展功能(可选) 高级功能(可选) 设计 设计点1 设计点2 ... 代码实现 // code here 程序截图 ...

  4. 云计算之路-阿里云上:2014年6月12日12点IIS请求到达量突降

    今天中午12:00左右,在Windows性能监视器中突然发现SLB中的两台云服务器的IIS请求到达量(ArriveRate)突然下降,见下图: IIS日志中的情况如下: 综合以上情况,我们推测在12: ...

  5. Mtk Ft6306 touch 驱动 .

    1.1.    MTK Touch 驱动的组成Mtk  Touch  driver 驱动包括:Mtk platform 虚拟平台设备驱动.Module touch IC 驱动.Input subsys ...

  6. jsonp的后台怎么返回去数据

  7. 05.C#泛型类型(三章3.1-3.2)

    接下来说下C#中的泛型,熟练地使用泛型能提高代码的重用性,使用我们代码瞬间就高大上了,当然只有一点点,真的只有一点点,因为后面要学习和掌握的知识还有很多.先来看下一个使用Dictionary<T ...

  8. centos 6.5下安装mysql+nginx+redmine 3.1.0 笔记

    centos 6.5下安装mysql+nginx+redmine 3.1.0 笔记 目录[-] 过程 1.安装RVM 2.利用rvm安装 Ruby 1.9.3 并设为默认 3.安装rails 4.安装 ...

  9. Ibatis学习总结1--ibatis简介和SQL Maps

    最佳维护的一个项目使的是ibatis框架,在闲暇之余将手头的开发手册和平时开发的理解做一下总结,言归正传. 简介 使用 SQL Map,能够大大减少访问关系数据库的代码.SQL Map 使用简单的 X ...

  10. Linux进程管理工具——supervisor

    介绍 Supervisord是用Python实现的一款非常实用的进程管理工具 安装 这里用源码 supervisor-.tar.gz .tar.gz cd supervisor- sudo pytho ...