第五章 Spring3.0 、Hibernate3.3與Struts2的整合
5.1整合Spring與Hibernate
5.1.1使用MyEclipse加入Spring與Hibernate功能
A、加入Spring功能


B、加入Hibernate功能
設置Hibernate的相關功能在Spring的配置文件中設置。如果還沒有Spring的功能(配置文件),就選擇New Spring configuration file,否則就按下麵的下麵的設置。SessionFactory Id是把SessionFactory類交由Spring管理時,它的Bean Id。


選擇和配置數據源,這和Hibernate單獨設置的時候是一樣的。可參考Hibernate的文檔(如下鏈接)。
https://blog.csdn.net/p_3er/article/details/8965305
C、移除MyEclipse給我們提供的Jar包,並導入我們的Jar包

5.1.2 配置數據源與SessionFactory
這個在上麵5.1.1的過程中,已由MyEclipse自動幫我們配置好了。而映射文件或實體類的路徑那是在通過MyEclipse中的工具把數據表生成實體類及映射的時候自動生成的。
<!-- 配置數據源 這個與JDBC整合的時候是一樣的--> <bean destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/> <property name="username" value="root"/> <property name="password" value="test"/> <property name="initialSize" value="5"/> <property name="maxIdle" value="20"/> <property name="minIdle" value="5"/> <property name="maxActive" value="500"/> </bean> <!-- 配置SessionFactory --> <bean > <!-- 引入數據源 --> <property name="dataSource"> <ref bean="dataSource" /> </property> <!-- Hibernate的相關配置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <!-- 映射文件的路徑。這是通過MyEclipse中的工具把數據表生成實體類及映射的時候自動生成的。 --> <property name="mappingResources"> <list> <value>cn/framelife/ssh/entity/User.hbm.xml</value> </list> </property> </bean>
5.1.3 配置事務
<!-- 配置事務 --> <bean > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="add*" rollback-for="Exception"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.framelife.ssh.service..*(..))" /> <aop:advisor pointcut-ref="pointcut" advice-ref="txAdvice" /> </aop:config>
5.1.4 HibernateDaoSupport類的使用
Spring提供了org.springframework.orm.hibernate3.support.HibernateDaoSupport工具類,使Spring與Hibernate整合後,不再需要使用Hibernate的API,而使用HibernateDaoSupport類就可以擁有Hibernate API的功能。
數據層,*DaoImpl類實現*Dao接口,並繼承HibernateDaoSupport類:
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { /** * 保存或更新對象 */ public void saveOrUpdateUser(User user) { getHibernateTemplate().saveOrUpdate(user); } /** * 根據id刪除一條記錄 */ public void deleteById(Integer id) { getHibernateTemplate().delete(this.findUserById(id)); } /** * 根據id獲取一個記錄對象 */ public User findUserById(Integer id) { return (User) getHibernateTemplate().get(User.class, id); } /** * 根據Hql語句,以及開始位置、最大多少條數進行分頁查詢 */ public List<User> findUserByLimit(final String hql, final int start, final int limit) { List<User> list = getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery(hql); query.setFirstResult(start); query.setMaxResults(limit); List list = query.list(); return list; } }); return list; } /** * 根據一個User對象查詢與這個對象中的非空屬性一致的數據 */ public List<User> findUserByExample(User user) { return getHibernateTemplate().findByExample(user); } /** * 根據Hql語句查詢多條記錄對象 */ public List<User> findUserByHql(String hql) { return getHibernateTemplate().find(hql); } }
5.2集成Struts2
5.2.1 web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- OpenSessionInViewFilter的主要功能是用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- struts2的設置 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>cn.framelife.ssh.struts</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- 配置contextConfigLocation參數設置Spring配置文件的路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 通過配置ContextLoaderListener監聽器,使服務器啟動時,自動加載applicationContext配置,啟動Spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
5.2.2把*DaoImpl、*ServiceImpl及*Action等類交由Spring管理
<bean > <!-- 這裏的sessionFactory屬性是供HibernateDaoSupport使用的 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean > <property name="userDao" ref="userDao"></property> </bean> <bean scope="prototype"> <property name="userService" ref="userService"></property> </bean>
5.2.3配置struts2的Action
<struts> <package name="a" extends="struts-default"> <!-- 整合spring後,這裏的class是spring配置文件中的Action的Bean id --> <action name="add" > <result name="success">/success.jsp</result> </action> </package> </struts>
5.3 完整的Spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p" xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop" xmlns:tx="https://www.springframework.org/schema/tx" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-3.0.xsd https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 數據源 --> <bean > <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="test"></property> </bean> <!-- 配置SessionFactory --> <bean > <!-- 引入數據源 --> <property name="dataSource"> <ref bean="dataSource" /> </property> <!-- Hibernate的相關配置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <!-- 映射文件或者映射類的路徑。這是通過MyEclipse中的工具把數據表生成實體類及映射的時候自動生成的。 --> <property name="mappingResources"> <list> <value>cn/framelife/ssh/entity/User.hbm.xml</value> </list> </property> </bean> <!-- 配置事務 --> <bean > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="add*" rollback-for="Exception"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.framelife.ssh.service..*(..))" /> <aop:advisor pointcut-ref="pointcut" advice-ref="txAdvice" /> </aop:config> <!-- 管理Bean --> <bean > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean > <property name="userDao" ref="userDao"></property> </bean> <bean > <property name="userService" ref="userService"></property> </bean> </beans>
最後更新:2017-04-03 16:49:02