前言
前面已经学习了如何使用Spring与Struts2进行整合,本博文主要讲解如何使用Spring对Hibernate进行整合
Spring和Hibernate整合的关键点:
- SessionFactory对象交给Spring来创建
- Hibernate的事务交给Spring进行管理
Spring和Hibernate整合步骤
引入jar包
- 连接池/数据库驱动包
- Hibernate相关jar
- Spring 核心包(5个)
- Spring aop 包(4个)
- spring-orm-3.2.5.RELEASE.jar 【spring对hibernate的支持】
- spring-tx-3.2.5.RELEASE.jar 【事务相关】
配置文件
- hibernate.cfg.xml
- bean.xml
bean.xml
1 2 3 4 5 6
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
|
hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <hibernate-configuration> <session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///zhongfucheng</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">create</property>
</session-factory> </hibernate-configuration>
|
搭建配置环境测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package bb;
public class User {
private String name; private String password; private int id;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
@Override public String toString() { return "User{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
|
1 2 3
| public interface IUser { void save(); }
|
1 2 3 4 5 6 7 8
| public class UserDao implements IUser {
@Override public void save() {
}
}
|
1 2 3 4 5 6 7 8
| public class UserService { private UserDao userDao;
public void save() { userDao.save(); } }
|
测试Spring环境
首先,我们为userDao、userService使用Spring来创建对象,以及添加对象的依赖关系,看看Spring的环境是否成功
1 2 3 4 5 6 7 8 9
| @Repository public class UserDao implements IUser {
@Override public void save() {
}
}
|
- 创建userService实例,并注入userDao属性
1 2 3 4 5 6 7 8 9 10
| @Service public class UserService {
@Autowired private UserDao userDao;
public void save() { userDao.save(); } }
|
1 2 3 4 5 6 7 8 9
| <?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:context="http://www.springframework.org/schema/context" 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">
<context:component-scan base-package="bb"/>
</beans>
|
- 测试:成功得到userService对象,并且userService对象含有userDao属性的值
1 2 3 4 5 6 7 8 9 10 11
| public class Test2 {
@Test public void test33() { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-config.xml");
UserService userService = (UserService) ac.getBean("userService"); System.out.println(userService); } }
|
测试Hibernate环境
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="bb">
<class name="User" table="t_user"> <id name="id" column="user_id"> <generator class="native"></generator> </id> <property name="name" column="name"></property> <property name="password" column="password"></property> </class> </hibernate-mapping>
|
1
| <mapping resource="bb/User.hbm.xml" />
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Repository public class UserDao implements IUser {
@Override public void save(User user) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession(); session.beginTransaction();
session.save(user); session.getTransaction().commit(); session.close(); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12
| public class Test2 {
@Test public void test33() { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-config.xml");
UserService userService = (UserService) ac.getBean("userService"); userService.save(new User());
} }
|
使用Spring创建SessionFactory对象
Spring与Hibernate整合的关键点之一就是使用Spring来创建SessionFactory对象。其中又有三种方式来创建SessionFactory
直接加载hibernate主配置文件
1 2 3 4 5 6 7 8 9 10
|
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean>
|
那么在userDao中就不用我们自己手动来创建SessionFactory对象了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Repository public class UserDao implements IUser {
@Autowired private SessionFactory sessionFactory;
@Override public void save(User user) {
Session session = sessionFactory.openSession(); session.beginTransaction();
session.save(user);
session.getTransaction().commit(); session.close(); }
}
|
连接池交给Spring管理
我们知道Hibernate对C3P0的连接池支持度比不上Spring,因此我们可以使用Spring的连接池。因此我们加载Hibernate的主配置文件又使用Spring的数据库连接池
也就是说,一部分配置在hibernate.cfg.xml,一部分配置在Spring文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///zhongfucheng"></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>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/> <property name="dataSource" ref="dataSource"/> </bean>
|
配置文件全写Spring中【推荐】
上面我们一部分是加载Hibernate的主配置文件,一部分是使用Spring配置文件的数据库连接池…这样不好…我们应该在Spring中对其进行同一的管理!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?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:context="http://www.springframework.org/schema/context" 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">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///zhongfucheng"></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>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<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>
<property name="mappingLocations"> <list> <value>bb/User.hbm.xml</value> </list> </property>
</bean>
<context:component-scan base-package="bb"/>
</beans>
|
我们推荐的就是使用这一种,就可以少了Hibernate的配置文件了。并且容易统一管理。
Spring管理事务
到目前为止,我们是使用Hibernate编程式事务控制管理,Spring与Hibernate整合另一个关键就是使用Spring对Hibernate进行事务管理
1 2 3 4 5 6 7 8 9
| <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/> </bean>
<tx:annotation-driven transaction-manager="txManager"/>
|
值得注意的是:Spring与Hibernate整合,Spring只支持线程的Session,并且不用我们手动配置
userDao
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Repository public class UserDao implements IUser {
@Autowired private SessionFactory sessionFactory;
@Override public void save(User user) { sessionFactory.getCurrentSession().save(user); }
}
|
userService添加@Transactional注解就是为Hibernate添加了事务管理了。
1 2 3 4 5 6 7 8 9 10 11
| @Service @Transactional public class UserService {
@Autowired private UserDao userDao;
public void save(User user) { userDao.save(user); } }
|