新手使用idea整合Spring+Struts+Hibernate項目
項目所需jar下載:
https://download.csdn.net/download/weixin_44906002/12505287
1、如圖,使用idea新建一個struts的web項目,點擊next,編輯項目名稱,點擊finish

2、在web/WEB-INF下創建lib文件夾,導入下載好的jar包,然后鼠標右鍵點擊lib,選擇添加為庫
3、構建如下圖的項目結構:

4、編輯beans.xml,該配置文件可以創建項目的時候選擇spring項目,自動創建,路徑隨意,一般自動創建的applicationContext.xml在web/WEB-INF文件夾下面。
對beans.xml做如下配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-4.1.xsd 11 http://www.springframework.org/schema/cache 12 http://www.springframework.org/schema/cache/spring-cache.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx.xsd"> 17 <!--開啟包掃描--> 18 <context:component-scan base-package="com.cheng.*"/> 19 <!--配置數據源,這里使用的是c3p0連接池--> 20 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 21 <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/> 22 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shape?userUnicode=true&characterEncoding=utf-8"/> 23 <property name="user" value="root"/> 24 <property name="password" value="lijc19980218"/> 25 </bean> 26 <!--配置Hibernate的會話工廠bean--> 27 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 28 <!--注入數據源--> 29 <property name="dataSource" ref="dataSource"/> 30 <!--Hibernate配置文件位置--> 31 <property name="configLocation" value="classpath:hibernate.cfg.xml"/> 32 </bean> 33 34 <!--配置事務管理器,將事務管理交給spring--> 35 <bean id="txManger" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 36 <property name="sessionFactory" ref="sessionFactory"/> 37 </bean> 38 <!--定義事務通知--> 39 <tx:advice id="txAdvice" transaction-manager="txManger"> 40 <tx:attributes> 41 <!--事務方法--> 42 <tx:method name="save*" propagation="REQUIRED"/> 43 <tx:method name="add*" propagation="REQUIRED"/> 44 <!--...--> 45 </tx:attributes> 46 </tx:advice> 47 48 <!--配置AOP--> 49 <aop:config> 50 <aop:pointcut id="service" expression="execution(* com.cheng.service..*.*(..))"/> 51 <aop:advisor advice-ref="txAdvice" pointcut-ref="service"/> 52 </aop:config> 53 54 </beans>
5、配置hibernate.cfg.xml文件
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!--映射文件--> 8 <mapping resource="com/cheng/bean/circle.hbm.xml"/> 9 </session-factory> 10 </hibernate-configuration>
6、配置struts.xml,讓spring融合struts
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 5 "http://struts.apache.org/dtds/struts-2.5.dtd"> 6 7 <struts> 8 <!--配置struts為開發模式--> 9 <constant name="struts.devMode" value="true"/> 10 <!--配置主題為簡單主題--> 11 <constant name="struts.ui.theme" value="simple"/> 12 <!--配置請求后綴--> 13 <constant name="struts.action.extension" value="do"/> 14 <!--使用Spring的對象工廠--> 15 <constant name="struts.objectFactory" value="spring"/> 16 <package name="default" extends="struts-default" namespace="/"> 17 18 </package> 19 </struts>
7、配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 <context-param> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>classpath:beans.xml</param-value> 9 </context-param> 10 <listener> 11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12 </listener> 13 <filter> 14 <filter-name>struts2</filter-name> 15 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 16 </filter> 17 <filter-mapping> 18 <filter-name>struts2</filter-name> 19 <url-pattern>/*</url-pattern> 20 </filter-mapping> 21 </web-app>
<context-param></context-param>存放的是beans.xm配置文件路徑,添加監聽以便于在部署Web項目時,啟動和初始化Spring容器。
8、hibernate映射文件,例如:
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping> 7 <class name="完全限定類名" table="映射數據庫表名"> 8 <id name="主鍵"> 9 <generator class="assigned"></generator> 10 </id> 11 12 <property name="其他字段"></property> 13 14 </class> 15 16 </hibernate-mapping>
然后爽一波試試好不好使
浙公網安備 33010602011771號