经过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配置]
1 <!-- 配置OSIV模式过滤器,该过滤器必须在struts2过滤器之前配置 2 缺点是耗费资源因为在前端页面渲染的时候Session不能关闭表示数据库连接一直打开 3 --> 4 <filter> 5 <filter-name>osivFilter</filter-name> 6 <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 7 </filter> 8 <filter-mapping> 9 <filter-name>osivFilter</filter-name> 10 <url-pattern>/*</url-pattern> 11 </filter-mapping> 12 <!-- 配置struts2过滤器 --> 13 <filter> 14 <filter-name>struts2filter</filter-name> 15 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 16 </filter> 17 <filter-mapping> 18 <filter-name>struts2filter</filter-name> 19 <url-pattern>/*</url-pattern><!-- 对所有流量进行过滤 --> 20 </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]
1 struts.i18n.encoding=UTF-8 2 struts.action.extension=action,, 3 struts.serve.static.browserCache=false 4 struts.i18n.reload=true 5 struts.configuration.xml.reload=true 6 struts.devMode=true 7 struts.ui.theme=simple 8 struts.enable.DynamicMethodInvocation=true 9 struts.multipart.maxSize=2097152 10 struts.ognl.allowStaticMethodAccess=true
(2)hibernate配置
[config/hibernate/hibernate.cfg.xml配置]
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "file:///D:/程序/java/Hibernate/dtd文件/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 这里的数据源不需要配置了,使用spring进行c3p0数据源的配置 --> 8 <!-- <property name="connection.driver_class"> 9 com.mysql.jdbc.Driver 10 </property> 11 <property name="connection.username">root</property> 12 <property name="connection.password">5a6f38</property> 13 <property name="connection.url"> 14 jdbc:mysql://localhost:3306/lsn_surveypark 15 </property> --> 16 <property name="show_sql">true</property> 17 <property name="hbm2ddl.auto">update</property> 18 <property name="dialect"> 19 org.hibernate.dialect.MySQLDialect 20 </property> 21 <property name="javax.persistence.validation.mode">none</property> 22 </session-factory> 23 </hibernate-configuration>
[config/jdbc.properties]配置,该文件将被spring引用。
1 #jdbc configurations 2 jdbc.username=root 3 jdbc.password=5a6f38 4 jdbc.url=jdbc:mysql://localhost:3306/lsn_surveypark 5 jdbc.driverclass=com.mysql.jdbc.Driver 6 7 #c3p0 configurations 8 c3p0.pool.maxsize=20 9 c3p0.pool.minsize=4 10 c3p0.pool.initsieze=4 11 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]
[配置数据源]
1 <bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 2 <property name="driverClass" value="${jdbc.driverclass}"></property> 3 <property name="jdbcUrl" value="${jdbc.url}"></property> 4 <property name="user" value="${jdbc.username}"></property> 5 <property name="password" value="${jdbc.password}"></property> 6 7 <!-- 配置本身的数据源 --> 8 <property name="maxPoolSize" value="${c3p0.pool.maxsize}"></property> 9 <property name="minPoolSize" value="${c3p0.pool.minsize}"></property> 10 <property name="initialPoolSize" value="${c3p0.pool.initsieze}"></property> 11 <property name="acquireIncrement" value="${c3p0.pool.increment}"></property> 12 </bean>
[配置本地会话工厂bean]
1 <bean id="sessionFactory" 2 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 3 <!-- 注入数据源 --> 4 <property name="dataSource" ref="dataSource"></property> 5 <!-- 指定hibernate配置文件 --> 6 <property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml"></property> 7 <!-- 指定映射文件目录 --> 8 <property name="mappingDirectoryLocations"> 9 <list> 10 <value>classpath:com/kdyzm/domain</value> 11 </list> 12 </property> 13 </bean>
[事务管理配置]
[配置事务管理器]
1 <!-- 事务管理器 --> 2 <bean name="txManager" 3 class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 4 <property name="sessionFactory" ref="sessionFactory"></property> 5 </bean>
[配置事务通知]
1 <!-- 配置事务通知 --> 2 <tx:advice id="txAdvice" transaction-manager="txManager"> 3 <tx:attributes> 4 <!-- 需要加上事务处理的 --> 5 <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" 6 read-only="false" /> 7 <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" 8 read-only="false" /> 9 <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" 10 read-only="false" /> 11 <tx:method name="batch*" isolation="DEFAULT" propagation="REQUIRED" 12 read-only="false" /> 13 <tx:method name="new*" isolation="DEFAULT" propagation="REQUIRED" 14 read-only="false" /> 15 <tx:method name="create*" isolation="DEFAULT" propagation="REQUIRED" 16 read-only="false" /> 17 <!-- 不需要加上事务处理的 --> 18 <tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED" 19 read-only="true" /> 20 <tx:method name="load*" isolation="DEFAULT" propagation="REQUIRED" 21 read-only="true" /> 22 <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" 23 read-only="true" /> 24 <!-- 其余的方法全部加上事务,防止某些方法应该加上事务但是方法名不匹配 --> 25 <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" /> 26 </tx:attributes> 27 </tx:advice>
[aop事务配置]
1 <aop:config> 2 <aop:pointcut expression="execution(* *..*Service.*(..))" 3 id="txPointcut" /> 4 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" 5 order="2" /> 6 </aop:config>
[web.xml配置文件配置] 使得spring容器在tomcat启动的时候初始化
1 <!-- 通过上下文参数指定spring文件的位置,为什么tomcat会知道该位置? --> 2 <context-param> 3 <param-name>contextConfigLocation</param-name> 4 <param-value>classpath:spring/applicationContext.xml,classpath:spring/schedual.xml</param-value> 5 </context-param> 6 <!-- spring上下文载入监听器,使得spring容器随着tomcat的启动而初始化 --> 7 <listener> 8 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 9 </listener>
OK,SSH框架整合结束。