1. 程式人生 > >Spring整合hibernate乾貨

Spring整合hibernate乾貨

基本原理:就是由spring來管理hibernate的SessionFactory。

spring管理hibernate配置:不在需要hibernate.cfg.xml檔案,所有關於hibernate.cfg.xml檔案中的配置都在spring的配置檔案中來配置。

  1. 首先要配置資料來源

  2. 接下來引入properties檔案

  3. 建立LocalSessionFactoryBean來完成spring管理hibernate中的SessionFactory

  4. 載入hbm.xml配置檔案

applicationContext.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: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">

    <!-- 載入properties檔案 -->     <context:property-placeholder location="classpath:db.properties" />

    <!-- 開啟註解掃描  @Respostory  @Service  @Controller-->     <context:component-scan base-package="cn.itheima"/>

    <!-- 配置連線池 -->     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">         <property name="driverClass" value="${jdbc.driverClass}" />         <property name="jdbcUrl" value="${jdbc.url}" />         <property name="user" value="${jdbc.username}" />         <property name="password" value="${jdbc.password}" />     </bean>     <!-- 宣告sessionFactory -->     <bean id="sessionFactory"         class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">         <!-- 載入連線池 -->         <property name="dataSource" ref="dataSource" />         <property name="hibernateProperties">             <value>                 hibernate.show_sql=true                 hibernate.dialect=org.hibernate.dialect.MySQLDialect                 hibernate.hbm2ddl.auto=update                 hibernate.format_sql=true             </value>         </property>         <!-- 載入註解類 -->         <property name="packagesToScan">             <list>                 <value>cn.itheima.domain</value>             </list>         </property>     </bean>          <!-- 事務管理器 -->     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">         <property name="sessionFactory" ref="sessionFactory"/>     </bean>          <!-- 事務註解驅動 -->     <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

db.properties

jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///sshtest jdbc.username=root jdbc.password=123

spring整合hibernate後的DAO

spring整合hiberante後,我們的dao只需要繼承HibernateDaoSupport類

在HibernateDaoSupport中只需要注入SessionFactory就可以獲得到HibernateTemplate,它是對hibernate操作的一個簡單封裝,可以讓我們方便使用原來hibernate的操作.

@Repository("userDao") public class UserDAOImpl extends HibernateDaoSupport implements IUserDAO {

    @Autowired     @Qualifier("sessionFactory")     public void setSuperSessionFactory(SessionFactory factory) {         super.setSessionFactory(factory);     }

    @Override     public void add(User user) {         this.getHibernateTemplate().save(user); // session.save()     }

}