夜光带你走进Spring(九)
夜光序言:
不甘是
不愿为
不予回
正文:
2. Spring 与 Hibernate 整合
Spring与Hibernate整合,
* 单例的SessionFactory对象,交给spring的IOC容器创建~
* 事务管理,交给spring声明式事务管理器
演示步骤:
1. 没有整合案例
2. 整合
整合步骤:
1. 引入Hibernate/spring框架相关包
* hibernate jar
*spring – core
* spring – aop
* spring – orm 对orm支持
spring-jdbc-3.2.5.RELEASE.jar
spring-orm-3.2.5.RELEASE.jar
spring-tx-3.2.5.RELEASE.jar
2. hibernate.cfg.xml
3. dao/service
* Spring创建SessionFactory几种方式
<?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">
<~-- 连接池, 通过spring管理 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="6"></property> </bean>
<~-- Spring 与 Hibenate整合 (Spring创建SessionFactory) -->
<~-- 方式1: 直接加载hibernate.cfg.xml的方式,创建sessionFactory对象 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> -->
<~-- 方式2: 连接池交给spring管理,其他配置还是写到hibernate.cfg.xml中 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> -->
<~-- 方式3:(推荐) 所有的配置都在spring中完成--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <~-- a. 注入连接池 --> <property name="dataSource" ref="dataSource"></property>
<~-- b. hibernate常用配置: 方言、自动建表、显示sql --> <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>
<~-- c. 加载所有的映射(根据路径加载) <property name="mappingLocations"> <list> <value>classpath:cn/Genius/entity/*.hbm.xml</value> </list> </property> --> <~-- c. 根据目录加载所有的映射 --> <property name="mappingDirectoryLocations"> <list> <value>classpath:cn/Genius/entity</value> </list> </property> </bean>
<~-- 创建dao实例 --> <bean id="deptDao" class="cn.Genius.dao.DeptDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
<~-- 创建service实例 --> <bean id="deptService" class="cn.Genius.service.DeptService"> <property name="deptDao" ref="deptDao"></property> </bean>
<~-- Spring声明式事务管理配置 --> <~-- 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:advisor advice-ref="txAdvice" pointcut="execution(* cn..*Service.*(..))"/> </aop:config>
</beans>
|
* Spring对dao操作的支持
如下:
1. JDBC
Spring 提供了JdbcTemplate模板工具类,对原始的jdbc操作进行简化~
2. Hibernate
Spring 提供了对hibernate的sessionFactory创建的支持 (整合)
à 直接在dao中使用sessionFactory对象操作数据库
à 使用Spring提供的 HibernateTemplate 工具类操作数据库
优点: 对session的常用操作进行封装~ 比较方便~
à (推荐)HibernateDaoSupport工具类
Dao类直接继承HibernateDaoSupport工具类即可
HibernateDaoSupport对hibernateTemlate类进行了封装
3. SSH整合
SSH 整合:
Spring 与 Struts 整合
à Action创建交给Spring完成
Spring 与 Hibernate整合
à SessionFactory创建,交给spring完成 (管理事务)
夜光:步骤
1. 引入jar文件
Struts核心jar
Hibernate核心jar
Spring
SpringCore 核心jar文件 (5个)
SpringWeb 对struts支持(2个)
SpringAop 声明式事务管理(4个)
SpringORM 对hibernate支持 (3个)
Orm + jdbc + tx jar文件
其他
驱动 + 连接池
2. 配置
Web.xml 配置struts核心过滤器 + Spring容器初始化
Struts.xml 配置访问路径与action类的映射关系
applicationContext-public.xml Spring容器配置 【公用配置】
applicationContext-dao.xml Spring容器配置 【dao配置】
applicationContext-service.xml Spring容器配置 【service配置】
applicationContext-action.xml Spring容器配置 【action配置】
3. 代码
cn.Genius.entity 实体类: 封装数据/业务
cn.Genius.dao 数据访问层接口: 定义功能
cn.Genius.dao.impl 接口实现: 功能实现
cn.Genius.service 业务逻辑层 (控制事务)
cn.Genius.service.impl 实现
cn.Genius.action 控制层: 接收请求数据、处 理请求、返回结果视图标记跳转
任务:
Action中写add/update/delete/findById/showAll 方法
1. 数据要在页面输入
2. 查询到的结果要在jsp页面显示