<?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. Silverlight自定义控件开发:温度计

    由于在实际项目中需要实时显示采集到的空气温湿度,土壤温湿度值,需要用比较显眼并且清楚明了的方式来展示,这里我们准备采用温度计的方式来进行.一方面是因为大家都熟悉这个,知道怎么去看:同时,温度计本身也比 ...

  2. Viewpager模仿微信主布局的三种方式 ViewPager,Fragment,ViewPager+FragmentPagerAdapter

    效果大概就是这样 很简单 : 1 创建 top 和bottom 2主界面布局 添加top 和bottom 中间添加一个ViewPage 3 给ViewPager 和 底部View设置点击事件 源码下载 ...

  3. Android调用蓝牙打印机

    首先需要一个jar包,bluesdk,请自行百度. 具体排版样式跟网络打印机打印排版样式实现一样,这里不多叙述,只贴一个实现方法代码.蓝牙打印机使用前需要先跟手机配对,可以保存在本地,记录下地址,这里 ...

  4. [CareerCup] 6.2 Dominos on Chess Board 棋盘上的多米诺

    6.2 There is an 8x8 chess board in which two diagonally opposite corners have been cut off. You are ...

  5. [CareerCup] 8.3 Musical Jukebox 点唱机

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

  6. Solr(5.1.0) 与Tomcat 从0开始安装与配置

    1.什么是Solr? Solr是一个基于Lucene的Java搜索引擎服务器.Solr 提供了层面搜索.命中醒目显示并且支持多种输出格式(包括 XML/XSLT 和 JSON 格式).它易于安装和配置 ...

  7. (转)shell实例手册

    原文地址:http://hi.baidu.com/quanzhou722/item/f4a4f3c9eb37f02d46d5c0d9 实在是太好的资料了,不得不转 shell实例手册 0说明{ 手册制 ...

  8. SDAccel-FPGA将带来至多25倍单位功耗性能提升

    很久没有看FPGA了,本来想继续学习HLS,就上Xilinx的网站看了看.结果发现了SDx 开发环境,很新的一个东西.由于我对这方面了解不多,本篇博文仅仅只是资料的整合和介绍. 1.SDx开发环境 X ...

  9. Java学习笔记(二一)——Java 泛型

    [前面的话] 最近脸好干,掉皮,需要买点化妆品了. Java泛型好好学习一下. [定义] 一.泛型的定义主要有以下两种: 在程序编码中一些包含类型参数的类型,也就是说泛型的参数只可以代表类,不能代表个 ...

  10. Bootstrap系列 -- 27. 下拉菜单对齐方式

    Bootstrap框架中下拉菜单默认是左对齐,如果你想让下拉菜单相对于父容器右对齐时,可以在“dropdown-menu”上添加一个“pull-right”或者“dropdown-menu-right ...