spring3-hibernate3整合
Spring与Hibernate整合关键点:
1) Hibernate的SessionFactory对象交给Spring创建;
2) hibernate事务交给spring的声明式事务管理。
SH整合步骤:
1)引入jar包
连接池/数据库驱动包
Hibernate相关jar
Spring 核心包(5个)
Spring aop 包(4个)
spring-orm-3.2.5.RELEASE.jar 【spring对hibernate的支持】
spring-tx-3.2.5.RELEASE.jar 【事务相关】
2)配置
hibernate.cfg.xml
bean.xml
3)搭建环境、单独测试
步骤实现
// 数据访问层
public class DeptDao { // Spring与Hibernate整合: IOC容器注入
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} // 保存一个记录
// Spring与Hibernate整合:事务管理交给Spring
public void save(Dept dept) {
sessionFactory.getCurrentSession().save(dept);
}
}
2. DeptService
public class DeptService { private DeptDao deptDao;
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
} public void save(Dept dept){
deptDao.save(dept);
}
} 3. App.java 测试
public class App { // 容器
private ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); @Test
public void testApp() throws Exception {
DeptService deptServie = (DeptService) ac.getBean("deptService");
System.out.println(deptServie.getClass()); deptServie.save(new Dept());
}
}
4. bean.xml 配置 【Spring管理SessionFactory的3中方式】
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- dao 实例 -->
<bean id="deptDao" class="cn.itcast.dao.DeptDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- service 实例 -->
<bean id="deptService" class="cn.itcast.service.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean> <!-- 数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean> <!-- ###########Spring与Hibernate整合 start########### --> <!-- 方式(1)直接加载hibernate.cfg.xml文件的方式整合
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean> --> <!-- 方式(2)连接池交给spring管理 【一部分配置写到hibernate中,一份分在spring中完成】
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean> --> <!-- 【推荐】方式(3)所有的配置全部都在Spring配置文件中完成 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池对象 -->
<property name="dataSource" ref="dataSource"></property> <!-- hibernate常用配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property> <!-- hibernate映射配置
<property name="mappingLocations">
<list>
<value>classpath:cn/itcast/entity/*.hbm.xml</value>
</list>
</property>
-->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:cn/itcast/entity/</value>
</list>
</property>
</bean> <!-- ###########Spring与Hibernate整合 end########### --> <!-- 事务配置 -->
<!-- a. 配置事务管理器类 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- b. 配置事务增强(拦截到方法后如果管理事务?) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- c. Aop配置 -->
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.service.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config> </beans>
spring3-hibernate3整合的更多相关文章
- Spring3+MyBatis3整合log4j无法输出SQL语句问题的解决
今天遇到了跟下面文章一模一样的问题,下面文章的解决方案很好,在这里记录保存一下. Spring3+MyBatis3整合无法输出SQL语句问题的解决
- Struts2.0+Spring3+Hibernate3(SSH~Demo)
Struts2.0+Spring3+Hibernate3(SSH~Demo) 前言:整理一些集成框架,发现网上都是一些半成品,都是共享一部分出来(确实让人很纠结),这是整理了一份SSH的测试案例,完全 ...
- Struts2,Spring3,Hibernate4整合--SSH框架
Struts2,Spring3,Hibernate4整合--SSH框架(学习中) 一.包的导入 1.Spring包 2.Hibernate 包 3.struts 包 (还欠 struts2-sprin ...
- spring3+structs2整合hibernate4时报org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void sy.dao.impl.UserDaoImpl.setSessionFactory(org.hibernate.SessionFactory);
今天在spring3+structs2整合hibernate4时报如下错误,一直找不到原因: org.springframework.beans.factory.BeanCreationExcepti ...
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
一.开篇 这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能.所以在出来数据库方言的时候基 ...
- SSH项目整合教学Eclipse搭建SSH(Struts2+Spring3+Hibernate3)
这篇博文的目的 尝试搭建一个完整的SSH框架项目. 给以后的自己,也给别人一个参考. 读博文前应该注意: 本文提纲:本文通过一个用户注册的实例讲解SSH的整合.创建Struts项目,整合Hiberna ...
- Flex4+Spring3+Hibernate3+BlazeDS整合笔记
普通Java Web工程流行使用ssh框架,而当前台使用Flex制作的时候,后台就不需要用Struts了,通过使用BlazeDS远程方法调用即可. 首先,新建Java Web工程,然后添加Flex项目 ...
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源方法
一.开篇 这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能.所以在出来数据库方言的时候基 ...
- spring3+hibernate3+(dbcp+oracle+拦截器事务配置)整合(一)
1.applicationContext-base.xml文件 <?xml version="1.0" encoding="UTF-8"?>< ...
- spring3+struts2+hibernate3整合出现的问题,No mapping found for dependency [type=java.lang.String, name='struts.objectFactory.spring.enableAopSupport']
七月 11, 2016 3:49:24 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule ...
随机推荐
- 使用VS2010开发Qt程序的一点经验(转载)
转载:http://www.cnblogs.com/csuftzzk/p/VS_Qt_Experience.html 导读 相比于Qt Creator,我更喜欢用VS2010来进行开发.虽然启动时间相 ...
- web应用中web.xml配置详解
Web.xml常用元素 <web-app> <display-name></display-name>定义了WEB应用的名字 <description> ...
- Apply Root Motion
Apply Root Motion 应用根动作: Should we control the character's position from the animation itself or fro ...
- Creating Dynamic LOV in Oracle D2k Forms
Dynamic Lov is a good idea for the form where too many Lov requirement is there with different recor ...
- [HDOJ5543]Pick The Sticks(DP,01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...
- RNAseq分析软件STAR的安装
wget https://github.com/alexdobin/STAR/releases/STAR-2.5.2a.tar.gz tar -xzf STAR-2.5.2a.tar.gz cd ST ...
- [C程序设计语言]第四部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- nancy中的诊断功能面板1
nancyfx中有一项 诊断功能 ,可以查看网站的基本信息和其他相关信息,还包括查看会话信息,请求输出信息等. 假设你已经安装完了nancyfx.现在开始使用诊断功能: 一 安装 在你的Bootstr ...
- 概念学习(Concept Learning)
从特殊的训练样例中归纳出一般函数是机器学习的核心问题.一般函数是对理想目标函数的函数逼近(function approximation).简而言之,从特殊到普通.与此对应的是演绎推理(deductiv ...
- kakfa源码编译打包
kakfa项目编译: cd /home/zhaofuxin/workspace/kafka-0.8.2.1-src ./gradlew releaseTarGz 会出现如下异常: zhaofuxin@ ...