一,概述
1)Spring与Hibernate整合关键点:Hibernate的SessionFactory对象交给Spring创建;Hibernate事务交给Spring的声明式事务管理.
2)SH整合步骤:
a)引入jar包
C3P0连接池/数据库驱动jar包
Hibernate相关jar包
Spring核心jar包 5个
Spring aop包 4个
Spring-orm-3.2.5.RELEASE.jar [spring对hibernate的支持]
Spring-tx-3.2.5.RELEASE.jar [事务相关]
3)hibernate.cfg.xml/bean.xml
4)环境搭建/测试
二,实现
1)实体(setter和getter方法省略)
public class Dept {
public Dept(){
}
private int deptId;
private String deptName;
}
2)映射文件:Dept.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bighuan.entity"
auto-import="true">
<class name="Dept" table="t_dept1">
<id name="deptId" column="deptId">
<generator class="native"></generator>
</id>
<property name="deptName" column="deptName" length="20"></property>
</class>
</hibernate-mapping>
3)DeptDao.java
public class DeptDao {
//Spring与Hibernate整合:Spring的IOC容器注入
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* 保存一条部门记录
* Spring与Hibernate整合:事务交给Spring管理
*
* @param dept
*/
public void save(Dept dept) {
//Hibernate与Spring整合,用这种方式无法将数据插入数据库,但是不会报错
// sessionFactory.openSession().save(dept);
sessionFactory.getCurrentSession().save(dept);
}
}
4)DeptService.java
public class DeptService {
private DeptDao deptDao;
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}
public void save(Dept dept) {
deptDao.save(dept);
//int i=1/0;
}
}
5)hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">abc</property> -->
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql
<property name="hibernate.format_sql">true</property> -->
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- session的创建方式:Hibernate与Spring整合,默认就是通过线程方式创建session,添加这个配置反而会报错
<property name="hibernate.current_session_context_class">thread</property>-->
<!-- *********连接池配置*********** -->
<!-- 配置连接池驱动管理类 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 配置连接池参数信息 -->
<property name="hibernate.c3p0.max_size">5</property> <!-- 最大连接数 -->
<property name="hibernate.c3p0.min_size">2</property> <!-- 最小连接数 -->
<property name="hibernate.c3p0.timeout">5000</property> <!-- 超时时间 -->
<property name="hibernate.c3p0.max_statements">100</property> <!-- 最大执行的命令格个数 -->
<property name="hibernate.c3p0.idle_test_period">30000</property> <!-- 空闲测试时间 -->
<property name="hibernate.c3p0.acquire_increment">2</property> <!-- 连接不够用时,每次增加的个数 -->
<!--
#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false
-->
<!-- 3. 加载所有映射 -->
<mapping resource="com/bighuan/entity/Dept.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6)bean.xml
<?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/tx
http://www.springframework.org/schema/tx/spring-tx.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">
<!-- 1,数据源对象:C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="abc"></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>
<!-- 方式一:直接加载Hibernate.cfg.xml文件的方式 -->
<!-- SessionFactory实例 -->
<!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="configLocation" value="hibernate.cfg.xml"></property>作用同上
</bean> -->
<!-- 方式二:连接池交给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> -->
<!-- 方式三:所有的配置文件都在Spring配置文件中配置,把hibernate.cfg.xml删除 -->
<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.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- hibernate映射配置
<property name="mappingLocations">
<list>
<value>classpath:com/bighuan/entity/*.hbm.xml</value>
</list>
</property>
-->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/bighuan/entity/</value>
</list>
</property>
</bean>
<!-- 事务配置 -->
<!-- 配置事务管理器类 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- AOP配置:拦截哪些方法(切入点表达式)+应用上面的事务 -->
<aop:config>
<aop:pointcut expression="execution(* com.bighuan.service.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
<!-- DeptDao实例 -->
<bean id="deptDao" class="com.bighuan.dao.DeptDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- DeptService实例 -->
<bean id="deptService" class="com.bighuan.service.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>
</beans>
7)测试:
public class App {
private ApplicationContext ac = new ClassPathXmlApplicationContext(
"bean.xml");
@Test
public void testApp() throws Exception {
DeptService deptService=(DeptService) ac.getBean("deptService");
System.out.println(deptService.getClass()+","+deptService);//Spring环境OK
deptService.save(new Dept());//Hibernate环境OK
}
}
能获得DeptService,能保存部门说明环境配置成功,整合成功!