<?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. C#获取枚举描述代码

    public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚 ...

  2. Ajax实现异步操作实例_针对XML格式的请求数据

    js分类中有一节[原生js异步请求,XML解析]主要说明了js前台是如何处理XML格式请求和如何接受由服务器返回的XML数据的解析,今天我将用一个实例来说明具体要如何操作. 前台的参数类型也是XML使 ...

  3. 在c++程序中执行DOS命令

    转自博客:http://blog.csdn.net/ypist/article/details/8485049 #1,system()方式 在C盘根目录下新建文件夹,名称为12: system(&qu ...

  4. initializer for conditional binding must have optional type not AVAudioPlayer

    if let buttonBeep = self.setupAudioPlayerWithFile("ButtonTap", type: "wav") {    ...

  5. Windows的Subversion备份脚本

    2015-12-08更新:备份时添加--revision head 只备份最新的版本,已从脚本中移除. 2013-12-09更新:forfiles命令添加错误输出日志. 2013-12-04更新:添加 ...

  6. 使用jQuery加载html页面到指定的div

    一.jQuery加载一个html页面到指定的div里 把a.html里面的某一部份的内容加载到b.html的一个div里.比如:加载a.html里面的<div id=“row"> ...

  7. unity3d 依赖关系获取预制件任意资源

    前段时间策划们想知道UI预制件中使用了哪些音效 N多预制件.N多音效!! 如果纯人工整理的话这还不累成狗? 累成狗不说,还容易出错 所以获取音频剪辑小工具就诞生了,将策划从死亡边缘拉了回来 我们先看一 ...

  8. springmore-让编程更容易

    这是我多年项目的总结,并将其抽象出来,形成一个开源的项目 部分借鉴springside,将更多的实践总结进来 基于spring+ibatis+springMVC springmore-core专注于一 ...

  9. js回调

    请先看着一片blog: http://www.jb51.net/article/53027.htm 回调的两种使用方法: 1.一般的传函数.2.匿名函数 3.使用回调函数再使用call方法. 判断一个 ...

  10. “未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”

    产生原因: 使用nuget管理程序包,同一个解决方案里面有不同版本的Entity Framework,有可能在不同时间安装不同版本的Entity Framework,所以出现这个问题. 解决方案: 1 ...