使用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类生成数据库表,通过操作对象来映射 ...
随机推荐
- HMAC-MD5算法原理及实现
以下是分析节选,对于更详细的描述可以查阅RFC2104文档. HMAC需要一个加密用散列函数(表示为H)和一个密钥K. 假设H是一个将数据块用一个基本的迭代压缩函数来加密的散列函数. 用B来表 ...
- ZooKeeper学习第二期--ZooKeeper安装配置
一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式. ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境:■ 伪集群模式:就是在一台物 ...
- 【MySQL】10条SQL优化语句,让你的MySQL数据库跑得更快!
慢SQL消耗了70%~90%的数据库CPU资源: SQL语句独立于程序设计逻辑,相对于对程序源代码的优化,对SQL语句的优化在时间成本和风险上的代价都很低: SQL语句可以有不同的写法: 1 不使用子 ...
- 20135220谈愈敏Blog7_可执行程序的装载
可执行程序的装载 谈愈敏 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1000029000 一. ...
- IOS开发之—— 上传头像的使用
static NSString *const uploadSuccess = @"更改头像成功"; @interface DMAccountInformationViewContr ...
- Anaconda安装更新库
平台:win64+anaconda 1. 如何查看已安装的库 打开 Anaconda Command Prompt ,在命令提示符窗口中输入以下命令: pip list # 或者 conda list ...
- C#基础之内存分配
1.创建一个对象 一个对象的创建过程主要分为内存分配和初始化两个环节.在.NET中CLR管理的内存区域主要有三部分:栈.GC堆.LOH堆,栈主要用来分配值类型数据.它的管理是有系统控制的,而不是像GC ...
- 浅谈JS面向对象之创建对象
hello,everybody,今天要探讨的问题是JS面向对象,其实面向对象呢呢,一般是在大型项目上会采用,不过了解它对我们理解JS语言有很大的意义. 首先什么是面向对象编程(oop),就是用对象的思 ...
- 网页音乐突破金币(RMB)下载限制
我平时有时间会跳跳舞 跳舞肯定要有音乐呀 于是在网上找音乐 好不容易找到了一个网站,里面有很多很全的音乐 正准备下载呢,尼玛居然要金币! 在这里解释一下,金币你可以通过回复帖子或者发帖子得到,但是数量 ...
- jQuery问题:$XXX is not a function
用火狐浏览器打开,js代码一段不执行,F12以后看见下面的错误: 网上查看说是jQuery文件引用的问题,把jQuery.js引入语句修改了一下,果然没有错了. 我原来的引用语句是:<scrip ...