第07章 Spring 整合 Hibernate
一、概述
Spring 整合 Hibernate 整合什么 ?
1. 让 IOC 容器来管理 Hibernate 的 SessionFactory
2. 让 Hibernate 使用上 Spring 的声明式事务
二、整合步骤:
准备持久化类:
public class Account {
private Integer id;
private String username;
private int balance;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
Book类
public class Book {
private Integer id;
private String bookName;
private String isbn;
private int price;
private int stock;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
Dao层
@Repository
public class BookShopDaoImpl implements BookShopDao {
@Autowired
private SessionFactory sessionFactory;
//不推荐使用 HibernateTemplate 和 HibernateDaoSupport
//因为这样会导致 Dao 和 Spring 的 API 进行耦合
//可以移植性变差
// private HibernateTemplate hibernateTemplate;
//获取和当前线程绑定的 Session.
private Session getSession(){
return sessionFactory.getCurrentSession();
}
@Override
public int findBookPriceByIsbn(String isbn) {
String hql = "SELECT b.price FROM Book b WHERE b.isbn = ?";
Query query = getSession().createQuery(hql).setString(0, isbn);
return (Integer)query.uniqueResult();
}
@Override
public void updateBookStock(String isbn) {
//验证书的库存是否充足.
String hql2 = "SELECT b.stock FROM Book b WHERE b.isbn = ?";
int stock = (int) getSession().createQuery(hql2).setString(0, isbn).uniqueResult();
if(stock == 0){
throw new BookStockException("库存不足");
}
String hql = "UPDATE Book b SET b.stock = b.stock - 1 WHERE b.isbn = ?";
getSession().createQuery(hql).setString(0, isbn).executeUpdate();
}
@Override
public void updateUserAccount(String username, int price) {
//验证余额是否足够
String hql2 = "SELECT a.balance FROM Account a WHERE a.username = ?";
int balance = (int) getSession().createQuery(hql2).setString(0, username).uniqueResult();
if(balance < price){
throw new UserAccountException("余额不足!");
}
String hql = "UPDATE Account a SET a.balance = a.balance - ? WHERE a.username = ?";
getSession().createQuery(hql).setInteger(0, price).setString(1, username).executeUpdate();
}
}
Service层
@Service
public class BookShopServiceImpl implements BookShopService {
@Autowired
private BookShopDao bookShopDao;
/**
* Spring hibernate 事务的流程
* 1. 在方法开始之前
* ①. 获取 Session
* ②. 把 Session 和当前线程绑定, 这样就可以在 Dao 中使用 SessionFactory 的
* getCurrentSession() 方法来获取 Session 了
* ③. 开启事务
*
* 2. 若方法正常结束, 即没有出现异常, 则
* ①. 提交事务
* ②. 使和当前线程绑定的 Session 解除绑定
* ③. 关闭 Session
*
* 3. 若方法出现异常, 则:
* ①. 回滚事务
* ②. 使和当前线程绑定的 Session 解除绑定
* ③. 关闭 Session
*/
@Override
public void purchase(String username, String isbn) {
int price = bookShopDao.findBookPriceByIsbn(isbn);
bookShopDao.updateBookStock(isbn);
bookShopDao.updateUserAccount(username, price);
}
}
1. 加入hibernate
a.加入hibernate jar包
hibernate-release-4.2.4.Final\lib\required目录下的全部
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
导入数据库驱动
mysql-connector-java-5.1.7-bin.jar
c3p0-0.9.1.2.jar
b.添加 hibernate 的配置文件:hibernate.cfg.xml
c.编写持久化类对应的 .hbm.xml 文件。
2.加入 Spring
a.加入spring jar包
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.net.sf.cglib-2.2.0.jar
b.加入 Spring 的配置文件
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.atguigu.spring.hibernate"></context:component-scan>
<!-- 配置数据源 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 配置数据源属性 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置 hibernate 配置文件的位置及名称 -->
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
<!-- 使用 hibernateProperties 属相来配置 Hibernate 原生的属性 这样就无需配置hibernate.cfg.xml文件 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 配置 hibernate 映射文件的位置及名称, 可以使用通配符 -->
<property name="mappingLocations"
value="classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml"></property>
</bean>
<!-- 配置 Spring 的声明式事务 -->
<!-- 1. 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2. 配置事务属性, 需要事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="purchase" propagation="REQUIRES_NEW"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置事务切点, 并把切点和事务属性关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.atguigu.spring.hibernate.service.*.*(..))"
id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
3. 编写测试类
public class SpringHibernateTest {
private ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
private BookShopService bookShopService = ctx.getBean(BookShopService.class);
private Cashier cashier = ctx.getBean(Cashier.class);
@Test
public void testDataSource() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
@Test
public void testCashier(){
cashier.checkout("aa", Arrays.asList("1001","1002"));
}
@Test
public void testBookShopService(){
bookShopService.purchase("aa", "1001");
}
}