我使用的是hibernate-release-4.3.5.Final、spring-framework-4.0.6.RELEASE-dist、struts-2.3.16.3-all
整合的前提是:你已经熟悉SSH三大框架的单独搭建,还有了解Spring、Hibernate的注解配置
1、先搭建Struts2环境
2、整合Spring(即Struts2整合Spring)
(1)加入Spring lib下的所有jar(不知那个没用,都加了就不会错了。。。),加入struts2-spring-plugin-version.jar
(2)让Spring启动起来(即加入applicationContext.xml和配置web.xml)
【(3)让Spring管理Struts2中action类的创建(即添加@Controller注解,struts2中的class不再使用全类名),这一步写了action类再配置】
错误总结:
1、注意:下载的spring中缺aopalliance-1.0.jar,要加入它
否则会报:springjava.lang.NoClassDefFoundError: org/aopalliance/aop/Advice)
2、出现错误:@EnableAsync annotation metadata was not injected
原因:<context:component-scan base-package="*" /> *扫描的范围太大
3、整合Hibernate(即Spring整合Hibernate)
(1)加入Hibernate的required下的jar、mysql连接jar、c3p0.jar(这个可选)
(2)加入Hibernate配置文件
(3)配置applicationContext.xml,如下(注意:标红的是要根据你的实际修改的,从配置文件可以看出,了解SSH的注解配置很有必要)
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:component-scan <span style="color:#ff0000;">base-package="com.judge"</span> />
<!-- 加载数据库属性配置文件 -->
<context:property-placeholder location="classpath:<span style="color:#ff0000;">db.properties</span>" />
<!-- 数据库连接池c3p0配置 -->
<bean id="dataSource" class="<span style="color:#ff0000;">com.mchange.v2.c3p0.ComboPooledDataSource</span>"
destroy-method="close">
<property name="jdbcUrl" value="${db.url}"></property>
<property name="driverClass" value="${db.driverClassName}"></property>
<property name="user" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="maxPoolSize" value="40"></property>
<property name="minPoolSize" value="1"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxIdleTime" value="20"></property>
</bean>
<!-- session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<span style="color:#ff0000;"> <!-- 自动扫描注解方式配置的hibernate类文件.... --> </span>
<property name="packagesToScan">
<list>
<value><span style="color:#ff0000;">com.judge.entity</span></value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务通知属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事务传播属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* <span style="color:#ff0000;">com.judge.service</span>..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
</beans>
4、写代码,然后给它们添加正确的注解即可。
5、结束
-------------------------------分隔线----------------------------------------
附说明:下面标红需要根据实际情况改(可见搭好一次SSH框架后,其他项目使用的时候需要修改的内容不多)
一、db.properties文件内容如下:
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://<span style="color:#ff0000;">localhost</span>:3306/<span style="color:#ff0000;">test</span>
db.username=<span style="color:#ff0000;">root</span>
db.password=<span style="color:#ff0000;">root</span>
二、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="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!-- 自动更新 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定自动生成数据表的策略,update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据
model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等
应用第一次运行起来后才会。 -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
三、web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CKEditorDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 添加对spring的支持 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 定义Spring监听器,加载Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>