【Java EE 學習 69 中】【數據採集系統第一天】【SSH框架搭建】

通過23天的艱苦鬥爭,終於搞定了數據採集系統~徐培成老師很厲害啊,明明只是用了10天就搞定的項目我卻作了23天,仍是模仿的。。。呵呵,算了,總之最後總算是完成了,如今該好好整理該項目了。前端

第一天的內容:SSH框架搭建java

1、系統說明mysql

  1.該系統是數據採集系統,說白了就是問卷調查系統,它和考試系統使用的技術幾乎徹底相同。web

  2.開發環境:spring

    Eclipse版本:Eclipse Java EE IDE for Web Developers,Mars Release (4.5.0)sql

    Hibernate版本:hibernate-distribution-3.5.6-Final數據庫

    Spring版本:spring 3.1.0express

    Struts2版本:struts2 2.3.1.2apache

    使用的數據庫:mysql 5.5api

    使用的數據庫鏈接池:c3p0-0.9.1.2

    其他第三方插件用到的時候再說。

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框架整合結束。

相關文章
相關標籤/搜索