1.Spring整合Hibernate整合什么?
1)由IOC容器管理hibernate的SessionFactory
2)让Hibernate使用Spring声明式事务
1.整合步骤:
1)加入Hibernate:加入jar,添加Hibernate配置文件,编写持久化类对应.hbm.xml文件
2)加入Spring
创建两个持久化类Account.java和Book.java生成对应hbm.xml
1)由IOC容器管理hibernate的SessionFactory
2)让Hibernate使用Spring声明式事务
1.整合步骤:
1)加入Hibernate:加入jar,添加Hibernate配置文件,编写持久化类对应.hbm.xml文件
2)加入Spring
3)整合
Hibernate.cfg.xml文件
<hibernate-configuration>
<session-factory>
<!-- 配置Hibernate 的基本属性 -->
<!-- 1.数据源配置到IOC容器中,在此处不再配置数据源 -->
<!-- 2.关联的.hbm.xml也在IOC容器 中配置-->
<!-- 3.只配置基本属性:方言,sql,格式化,生成数据表策略,二级缓存等-->
<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">update</property>
</session-factory>
</hibernate-configuration>
创建两个持久化类Account.java和Book.java生成对应hbm.xml
<hibernate-mapping>
<class name="com.hcx.hibernate.entities.Account" table="SH_ACCOUNT">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="USER_NAME" />
</property>
<property name="balance" type="int">
<column name="BALANCE" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.hcx.hibernate.entities.Book" table="SH_BOOK">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="bookName" type="java.lang.String">
<column name="BOOK_NAME" />
</property>
<property name="isbn" type="java.lang.String">
<column name="ISBN" />
</property>
<property name="price" type="int">
<column name="PRICE" />
</property>
<property name="stock" type="int">
<column name="STOCK" />
</property>
</class>
</hibernate-mapping>
DAO实现类,这里的SessionFactory也要加入到Spring容器中
@Repository
public class BookShopDAOImpl implements BookShopDAO {
@Autowired
private SessionFactory sessionFeactory;// 线程安全
/**
* 不推荐使用HibernateTemplate:HIbernateSupportDao
* 因为这样会导致DAO和Spring的API进行耦合,可移植性变差
*
* @return
*/
// 需要获取currentSession,跟当前线程绑定。
private Session getSession() {
return sessionFeactory.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= ?";
Query query=getSession().createQuery(hql).setString(0, isbn);
query.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();
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置数据源 -->
<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">
<!-- 配置数据源、配置cfg文件、配置持久化映射文件位置 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/hcx/ssh/entities/*.hbm.xml"></property>
</bean>
<!-- 配饰事务管理器 事务属性 事务切入点 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.hcx.ssh.service.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>