1. 程式人生 > >【SSH框架】系列之 Spring 整合 Hibernate 框架

【SSH框架】系列之 Spring 整合 Hibernate 框架

操作 enter pda 就是 負責 spring配置 1.0 port -s

1、SSH 三大框架整合原理

技術分享圖片

  • Spring 與 Struts2 的整合就是將 Action 對象交給 Spring 容器來負責創建。

  • Spring 與 Hibernate 的整合就是將 SessionFactory 交給 Spring 容器來負責維護,並且 Spring 容器負責 Session 維護以及相關的 AOP 事務。

2、Spring 整合 Hibernate 框架

(1)、新建 web 項目,導入 Spring 和 Hibernate 框架所需要的 jar 包,如下圖所示:

技術分享圖片

(2)、單獨配置 Spring 容器,具體配置如下:

applicationContext.xml

  • 創建配置文件,並導入約束
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

</beans>

web.xml

  • 配置 Spring 隨項目啟動
 <!-- 讓spring隨web啟動而創建的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置文件位置參數 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

(3)、單獨配置 Hibernate

  • 書寫實體類與 orm 元數據

  • 配置主配置文件

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>

<!-- 數據庫驅動 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 數據庫url -->
<property name="hibernate.connection.url">jdbc:mysql:///hbDB</property>
<!-- 數據庫連接用戶名 -->
<property name="hibernate.connection.username">root</property>
<!-- 數據庫連接密碼 -->
<property name="hibernate.connection.password">root</property>
<!-- 數據庫方言
註意: MYSQL在選擇方言時,請選擇最短的方言.
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


<!-- 將hibernate生成的sql語句打印到控制臺 -->
<property name="hibernate.show_sql">true</property>
<!-- 將hibernate生成的sql語句格式化(語法縮進) -->
<property name="hibernate.format_sql">true</property>
<!--
自動導出表結構. 自動建表
-->
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- 引入實體配置文件 -->
<mapping resource="com/spring/domain/*.hbm.xml" />
<mapping resource="com/spring/domain/*.hbm.xml" />
<mapping resource="com/spring/domain/*.hbm.xml" />

</session-factory>
</hibernate-configuration>

(4)、Spring 整合 Hibernate

  • 在 Spring 容器中配置SessionFactory
<!-- 在 Spring 配置中放置 hibernate 配置信息 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<!-- 將連接池註入到 sessionFactory, hibernate 會通過連接池獲得連接 -->
<property name="dataSource" ref="dataSource" ></property>
<!-- 配置 hibernate 基本信息 -->
<property name="hibernateProperties">
<props>
<!-- 必選配置 -->
<!-- <prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url" >jdbc:mysql:///crm_32</prop>
<prop key="hibernate.connection.username" >root</prop>
<prop key="hibernate.connection.password" >1234</prop> -->
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>

<!-- 可選配置 -->
<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.format_sql" >true</prop>
<prop key="hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<!-- 引入 orm 元數據,指定orm元數據所在的包路徑,spring 會自動讀取包中的所有配置 -->
<property name="mappingDirectoryLocations" value="classpath:com/spring/domain" ></property>
</bean>

(5)、Spring 整合 hibernate 環境操作數據庫

  • 創建 Dao 類繼承 HibernateDaoSupport

  • Spring 中配置 action service dao

<!-- action -->
<!-- Action對象作用範圍一定是多例的 -->
<bean name="*Action" class="com.spring.web.action.*Action" scope="prototype" >
<property name="*Service" ref="*Service" ></property>
</bean>
<!-- service -->
<bean name="*Service" class="com.spring.service.impl.*ServiceImpl" >
<property name="*demo" ref="*Dao" ></property>
</bean>
<!-- dao -->
<bean name="*Dao" class="com.spring.dao.impl.*DaoImpl" >
<!-- 註入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
  • 使用 hibernate 模板進行具體操作

掃描關註微信公眾號,了解更多

技術分享圖片

【SSH框架】系列之 Spring 整合 Hibernate 框架