1、導入依賴jar包:
1.1 hibernate相關(5.2.12.Final)
hibernate-core
hibernate-c3p0(數據庫連接池)
hibernate-ehcache
mysql-connector-java(5.1.44)
1.2 spring相關(5.0.1.RELEASE)
spring-context
spring-orm
spring-web
spring-aspects
1.3 struts2相關(2.5.13)
struts2-core
struts2-spring-plugin
1.4 log配置
1.4.1 log4j(1.X版)
不建議使用
1.4.2 log4j2(2.9.1)
log4j-core
log4j-api
log4j-web
不建議使用
1.4.3 log4j2 + slf4j(使用ehcache開啓hibernate二級緩存必須使用第二種方式配置日誌)
1.5 other
junit(4.12)
javax.servlet-api(4.0.0)
1.6 jstl
jstl(1.2)
standard(1.1.2)
1.7 jsp自定義標籤依賴(必須與tomcat的版本一致)
tomcat-jsp-api
<plugins>
<!--第一步就是配置maven-compiler-plugin插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
注1:如何解決項目中不支持EL表達式的問題?將web.xml的DTD的版本由2.3升級到3.0即可。因爲web2.3是不支持EL表達式的
2、配置文件:
spring-context.xml:
分模塊
db.properties :
db.username=root
db.password=123
db.driverClass=com.mysql.jdbc.Driver
db.jdbcUrl=jdbc:mysql://127.0.0.1:3306/odinms?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
db.initialPoolSize=10
db.maxPoolSize=20
db.minPoolSize=5
db.maxIdleTime=60
db.acquireIncrement=5
db.maxStatements=0
db.idleConnectionTestPeriod=60
db.acquireRetryAttempts=30
db.breakAfterAcquireFailure=true
db.testConnectionOnCheckout=false
spring-hibernate.xml:
1、註冊jdbc相關的配置文件
<context:property-placeholder location="classpath:db.properties" />
2、配置數據庫連接池C3P0
<!-- 2、配置數據庫連接池C3P0 -->
<!-- 註冊數據庫連接文件db.properties -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0連接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="driverClass" value="${db.driverClass}"></property>
<property name="jdbcUrl" value="${db.jdbcUrl}"></property>
<!--初始化時獲取的連接數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="${db.initialPoolSize}"></property>
<!--連接池中保留的最大連接數。Default: 15 -->
<property name="maxPoolSize" value="${db.maxPoolSize}"></property>
<!--連接池中保留的最小連接數。 -->
<property name="minPoolSize" value="${db.minPoolSize}" />
<!--最大空閒時間,60秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->
<property name="maxIdleTime" value="${db.maxIdleTime}" />
<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
<property name="acquireIncrement" value="${db.acquireIncrement}" />
<!--JDBC的標準參數,用以控制數據源內加載的PreparedStatements數量。但由於預緩存的statements 屬於單個connection而不是整個連接池。
所以設置這個參數需要考慮到多方面的因素。如果maxStatements與maxStatementsPerConnection均爲0,則緩存被關閉。Default:
0 -->
<property name="maxStatements" value="${db.maxStatements}" />
<!--每60秒檢查所有連接池中的空閒連接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="${db.idleConnectionTestPeriod}" />
<!--定義在從數據庫獲取新連接失敗後重復嘗試的次數。Default: 30 -->
<property name="acquireRetryAttempts" value="${db.acquireRetryAttempts}" />
<!--獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數據源仍有效 保留,並在下次調用getConnection()的時候繼續嘗試獲取連接。
如果設爲true,那麼在嘗試 獲取連接失敗後該數據源將申明已斷開並永久關閉。Default: false -->
<property name="breakAfterAcquireFailure" value="${db.breakAfterAcquireFailure}" />
<!--因性能消耗大請只在需要的時候使用它。如果設爲true那麼在每個connection提交的 時候都將校驗其有效性。建議使用idleConnectionTestPeriod
或automaticTestTable 等方法來提升連接測試的性能。Default: false -->
<property name="testConnectionOnCheckout" value="${db.breakAfterAcquireFailure}" />
</bean>
3、配置sessionfactory相關信息
<!-- 3、配置sessionfactory相關信息 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 數據源 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- hibernate相關屬性 -->
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<!--spring與Hibernate集成無法顯示sql語句問題,請見集成後hibernate無法顯示sql語句.txt -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 實體映射文件 -->
<property name="mappingResources">
<list>
<value>com/zking/book/entity/Book.hbm.xml</value>
</list>
</property>
</bean>
4、配置事務
<!--1) 開啓自動代理 -->
<aop:aspectj-autoproxy /><!-- 動態代理 -->
<!--2) 事務管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--3) 定義事務特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="do*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!--4) 定義切入點 -->
<aop:config>
<!-- pointcut屬性用來定義一個切入點,分成四個部分理解 [* ][*..][*Biz][.*(..)] -->
<!-- A: 返回類型,*表示返回類型不限 -->
<!-- B: 包名,*..表示包名不限 -->
<!-- C: 類或接口名,*Biz表示類或接口必須以Biz結尾 -->
<!-- D: 方法名和參數,*(..)表示方法名不限,參數類型和個數不限 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Biz.*(..))" />
</aop:config>
<!-- 聲明式事務配置結束 -->
5、配置HibernateTemplate
<!-- 5、配置HibernateTemplate -->
<bean class="org.springframework.orm.hibernate5.HibernateTemplate"
id="hibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
6、分模塊開發(base模塊)
<!-- 6、分模塊開發(base模塊) -->
<!-- 抽象類: abstract="true" -->
<bean class="com.zking.base.entity.BaseEntity" abstract="true" id="baseEntity"></bean>
<bean class="com.zking.base.dao.BaseDao" abstract="true" id="baseDao">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean class="com.zking.base.biz.BaseBiz" abstract="true" id="baseBiz"></bean>
<bean class="com.zking.base.web.BaseAction" abstract="true" id="baseAction"></bean>
資源文件:
3、web整合spring-hibernate、struts
配置請注意順序:
1、整合spring
<!-- 1、整合spring -->
<!-- Spring上下文applicationContext.xml加載 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<!-- 啓動Web容器時,自動裝配ApplicationContext.xml的配置信息 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
2、整合Struts2
<!-- 2、整合Struts2 -->
<!-- Struts2核心過濾器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
3、添加過濾器
<!-- 3、添加過濾器 -->
<!-- 中文亂碼過濾器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>