经过23天的艰苦斗争,终于搞定了数据采集系统~徐培成老师很厉害啊,明明只是用了10天就搞定的项目我却做了23天,还是模仿的。。。呵呵,算了,总之最后总算是完成了,现在该好好整理该项目了。
第一天的内容:SSH框架搭建
一、系统说明
1.该系统是数据采集系统,说白了就是问卷调查系统,它和考试系统使用的技术几乎完全相同。
2.开发环境:
Eclipse版本:Eclipse Java EE IDE for Web Developers,Mars Release (4.5.0)
Hibernate版本:hibernate-distribution-3.5.6-Final
Spring版本:spring 3.1.0
Struts2版本:struts2 2.3.1.2
使用的数据库:mysql 5.5
使用的数据库连接池:c3p0-0.9.1.2
其余第三方插件用到的时候再说。
二、环境搭建
1.引入类库
[struts2]
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
freemarker-2.3.18.jar
javassist-3.11.0.GA.jar
ognl-3.0.4.jar
struts2-core-2.3.1.2.jar
xwork-core-2.3.1.2.jar
[hibernate]
antlr-2.7.6.jar
commons-collections-3.1.jar
hibernate3.jar
javassist-3.11.0.GA.jar
jta-1.1.jar
log4j.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
[spring]
org.springframework.aop-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.aspects-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.context.support-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.orm-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
org.springframework.web-3.1.0.RELEASE.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
[struts-spring-plugin]
struts2-spring-plugin-2.3.1.2.jar
[driver]
mysql-connector-java-5.0.8-bin.jar
[datasource]
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
2.创建基本包结构
[src]
com.kdyzm.dao.base
com.kdyzm.dao.base.impl
com.kdyzm.dao.impl
com.kdyzm.domain
com.kdyzm.service
com.kdyzm.service.base
com.kdyzm.service.base.impl
com.kdyzm.service.impl
com.kdyzm.struts.action
com.kdyzm.struts.action.base
com.kdyzm.utils
[config]
struts.xml
hibernate
spring
struts2
[readSrc]
[init]
3.配置文件
(1)struts2配置
[web.xml配置]
<!-- 配置OSIV模式过滤器,该过滤器必须在struts2过滤器之前配置
缺点是耗费资源因为在前端页面渲染的时候Session不能关闭表示数据库连接一直打开
-->
<filter>
<filter-name>osivFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>osivFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置struts2过滤器 -->
<filter>
<filter-name>struts2filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2filter</filter-name>
<url-pattern>/*</url-pattern><!-- 对所有流量进行过滤 -->
</filter-mapping>
[config/strtus.xml] 为了加快tomcat启动速度,并且摆脱必须连接到Internet的限制,全部使用本地文件传输协议(file:///)
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"file:\\\D:\程序\java\struts2\dtd\struts-2.3.dtd">
<struts>
</struts>
[config/struts.properties]
struts.i18n.encoding=UTF-8
struts.action.extension=action,,
struts.serve.static.browserCache=false
struts.i18n.reload=true
struts.configuration.xml.reload=true
struts.devMode=true
struts.ui.theme=simple
struts.enable.DynamicMethodInvocation=true
struts.multipart.maxSize=2097152
struts.ognl.allowStaticMethodAccess=true
(2)hibernate配置
[config/hibernate/hibernate.cfg.xml配置]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"file:///D:/程序/java/Hibernate/dtd文件/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 这里的数据源不需要配置了,使用spring进行c3p0数据源的配置 -->
<!-- <property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">root</property>
<property name="connection.password">5a6f38</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/lsn_surveypark
</property> -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="javax.persistence.validation.mode">none</property>
</session-factory>
</hibernate-configuration>
[config/jdbc.properties]配置,该文件将被spring引用。
#jdbc configurations
jdbc.username=root
jdbc.password=5a6f38
jdbc.url=jdbc:mysql://localhost:3306/lsn_surveypark
jdbc.driverclass=com.mysql.jdbc.Driver #c3p0 configurations
c3p0.pool.maxsize=20
c3p0.pool.minsize=4
c3p0.pool.initsieze=4
c3p0.pool.increment=2
(3)config/spring/applicationContext.xml文件配置
[命名空间声明],同样使用本地文件传输协议(file:///)来声明xsd文件的位置。除了之后的spring缓存策略,其余使用spring 2.5即可解决。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/aop file:///D:\程序\java\Spring\spring-framework-4.2.1\spring-framework-4.2.1.RELEASE\schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans file:///D:\程序\java\Spring\spring-framework-4.2.1\spring-framework-4.2.1.RELEASE\schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/cache file:///D:\程序\java\Spring\spring-framework-4.2.1\spring-framework-4.2.1.RELEASE\schema\cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/tx file:///D:\程序\java\Spring\spring-framework-4.2.1\spring-framework-4.2.1.RELEASE\schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context file:///D:\程序\java\Spring\spring-framework-4.2.1\spring-framework-4.2.1.RELEASE\schema/context/spring-context-2.5.xsd">
</beans>
[组件扫描] 为了加快扫描速度,具体到每个包
<context:component-scan
base-package="com.kdyzm.dao.impl,com.kdyzm.service.impl,com.kdyzm.struts.action,com.kdyzm.dao.base.impl,com.kdyzm.listener"></context:component-scan>
[加载jdbc配置文件]
<context:property-placeholder location="classpath:jdbc.properties" />
[整合hibernate]
[配置数据源]
<bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverclass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property> <!-- 配置本身的数据源 -->
<property name="maxPoolSize" value="${c3p0.pool.maxsize}"></property>
<property name="minPoolSize" value="${c3p0.pool.minsize}"></property>
<property name="initialPoolSize" value="${c3p0.pool.initsieze}"></property>
<property name="acquireIncrement" value="${c3p0.pool.increment}"></property>
</bean>
[配置本地会话工厂bean]
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定hibernate配置文件 -->
<property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml"></property>
<!-- 指定映射文件目录 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/kdyzm/domain</value>
</list>
</property>
</bean>
[事务管理配置]
[配置事务管理器]
<!-- 事务管理器 -->
<bean name="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
[配置事务通知]
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 需要加上事务处理的 -->
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" />
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" />
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" />
<tx:method name="batch*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" />
<tx:method name="new*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" />
<tx:method name="create*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" />
<!-- 不需要加上事务处理的 -->
<tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED"
read-only="true" />
<tx:method name="load*" isolation="DEFAULT" propagation="REQUIRED"
read-only="true" />
<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED"
read-only="true" />
<!-- 其余的方法全部加上事务,防止某些方法应该加上事务但是方法名不匹配 -->
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
[aop事务配置]
<aop:config>
<aop:pointcut expression="execution(* *..*Service.*(..))"
id="txPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"
order="2" />
</aop:config>
[web.xml配置文件配置] 使得spring容器在tomcat启动的时候初始化
<!-- 通过上下文参数指定spring文件的位置,为什么tomcat会知道该位置? -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml,classpath:spring/schedual.xml</param-value>
</context-param>
<!-- spring上下文载入监听器,使得spring容器随着tomcat的启动而初始化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
OK,SSH框架整合结束。