zoukankan      html  css  js  c++  java
  • 调查管理系统 -(1)环境搭建

    一、官方简介

    Java视频教程为数据采集系统,主要面向市场,灵活定制各种市场调查,收集用户的反馈信息,供企业和个人对最终的决策提供数据支持。系统集成jfreechart插件,可生成各种类型的图表以及采用poi导出原始数据,形成excle。采用深度复制技术,可方便进行对象图全复制以及数据的移植。采用cxf引擎,将统计服务公开成WebService,与第三方系统无缝整合。通过对service进行缓存,大量降低数据库负载,提高性能。

    二、核心技术回顾

    SSH:Struts2 Spring Hibernate.

    1)Struts2:开发Web程序的框架,是更加整洁的MVC结构。具有以下主要特点:

      Ⅰ、分离关注:拦截器(AOP编程的体现);Struts2将处理数据的逻辑和处理业务的逻辑分配到2个不同的执行阶段。是我们对代码逻辑的关注点更为清晰。参:陆舟《Struts2技术内幕》

      Ⅱ、Action是原型对象,保证线程安全性;

      Ⅲ、耦合度低:和原生的servlet api,和自己编写的代码耦合度低。

    2)Hibernate:持久化技术,替代JDBC,封装了数据的访问细节,体现了oop的思想。
    3)Spring:业务层框架,管理bean。其核心理念如下:

      Ⅰ 、IOC:Inverse of Control,反转控制.

      Ⅱ、AOP:Aspect Oriented Program,不改变源代码,还给类增加新的功能,是对oop进行增强。
        AOP的本质是代理。

      Ⅲ、Sping切面具有的5种类型的通知:前置、后置、环绕(如事务管理的应用)、异常、引入。

    4)在将三大框架整合后,系统通常划分为四层,即:表现层(Struts2)、业务层(Service)、数据访问层(DAO)、持久化层(Hibernate),而Spring的作用则是将各个层次整合到一起。(注:关于分层问题以及MVC有很多不同的说法,能在不同场合下大概的明白就行了。)

    三、环境搭建

    1)新建工程,类型为Web Project,设置默认编码为UTF-8

    2)创建如下文件夹:

      Ⅰ 、Source Folder
        ① src     项目源码
        ② conf   配置文件
        ③ test   单元测试
      Ⅱ、普通文件夹
        ① WebRoot/style                css与图片等文件
        ② WebRoot/script               js脚本文件
        ③ WebRoot/WEB-INF/jsp    jsp页面文件

    2)创建包结构(包结构是软件分层的一种体现)

    ① 实体:

    com.atguigu.surveypark.model(实体)

    ② DAO:

    com.atguigu.surveypark.dao (DAO接口)

    com.atguigu.surveypark.dao.impl (DAO实现类)

    ③ Service:

    com.atguigu.surveypark.service(Service接口)

    com.atguigu.surveypark.service.impl(Service实现类)

    ④ View(Struts2):

    com.atguigu.surveypark.struts2.action (Struts2的Action类)

    ⑤ 其它:

    com.atguigu.surveypark.util (工具类)

    ....

    3)引入类库(MyEclipse:放在 WebRoot/WEB-INF/lib/ 下;Eclipse:放在 WebContent/WEB-INF/lib/ 下) 

    [环境]

      JDK_1.7、JavaEE_5.0

    [struts2-2.3.15]:添加 ${Struts2_Home}/apps/struts2-blank-version.war/WEB-INF/lib/*.jar

    asm-3.3.jar
    asm-commons-3.3.jar
    asm-tree-3.3.jar
    commons-fileupload-1.3.jar
    commons-io-2.0.1.jar
    commons-lang3-3.1.jar
    commons-logging-1.1.3.jar
    freemarker-2.3.19.jar
    javassist-3.11.0.GA.jar
    log4j-1.2.17.jar
    ognl-3.0.6.jar
    struts2-core-2.3.15.1.jar
    xwork-core-2.3.15.1.jar

    [hibernate-3.6]

    Ⅰ、添加 ${HIBERNATE_HOME}/hibernate3.jar
    Ⅱ、添加 ${HIBERNATE_HOME}/lib/required/*.jar
                ${HIBERNATE_HOME}/lib/jpa/hibernate-jpa-2.0-api-1.0.0.Final.jar
    Ⅲ、添加 slf4j-log4j12-1.5.8.jar、log4j-1.2.15.jar

    注:视频中徐培成老师原来是准备添加Hibernate4的jar包的,但是在与Spring整合时,由于在配置LocalSessionFactoryBean和HIbernateTransactionManager时,将相关的类引为了spring-orm.jar包中的Hibernate3相关的类,导致了测试时出错。这里Hibernate3.6的添加的相关jar包参考了汤阳光-OA中的总结。

    HIbernate4中的jar包相对于HIbernate3变化较大,关于Spring3.1.x与Hibernate4的整合参考:http://www.cnblogs.com/interdrp/p/3439635.html

    [spring-3.1.0]

    org.springframework.aop-3.1.0.CI-1162.jar
    org.springframework.asm-3.1.0.CI-1162.jar
    org.springframework.aspects-3.1.0.CI-1162.jar
    org.springframework.beans-3.1.0.CI-1162.jar
    org.springframework.context-3.1.0.CI-1162.jar
    org.springframework.context.support-3.1.0.CI-1162.jar
    org.springframework.core-3.1.0.CI-1162.jar
    org.springframework.expression-3.1.0.CI-1162.jar
    org.springframework.jdbc-3.1.0.CI-1162.jar
    org.springframework.orm-3.1.0.CI-1162.jar
    org.springframework.transaction-3.1.0.CI-1162.jar
    org.springframework.web-3.1.0.CI-1162.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

    [struts2-spring插件]:${Struts2_Home}/lib/struts2-spring-plugin-version.jar

    struts2-spring-plugin-2.3.15.1.jar

    [数据源]

    com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

    [驱动程序]

    mysql-connector-java-5.0.8-bin.jar

    [Junit]:junit-version.jar(一般为IDE内置的)

      junit-4.4.jar

    [其它的jar包]  

    commons-codec.jar  含有MD5的工具类
    commons-lang.jar     java.lang包中有关类的工具类

     

     提示:注意不要发生jar包冲突的问题。

    4)配置文件(除web.xml外,都放在源码文件夹 conf/ 下)

      Ⅰ、[struts2 + web]的配置文件:

        ① 在 web.xml 中配置Struts2的Filter:

    <!-- 配置Struts2的过滤器 -->
    <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>

        ② [config/struts.xml] 配置struts.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <!-- 设置开发模式 -->
        <constant name="struts.devMode" value="true" />
        <!-- 把action扩展名配置为action -->
        <constant name="struts.action.extension" value="action" />
        <!-- 指定主题使用simple(默认值为xhtml) -->
        <constant name="struts.ui.theme" value="simple" />
        
        <!-- 可以把相关的action都放在一个package中,以便于管理,形成模块 -->
        <package name="surveyparkPkg" extends="struts-default" namespace="/">
                    
        </package>
    </struts>

    Ⅱ、[config/hibernate.cfg.xml] 配置Hibernate:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <!-- 方言 -->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <!-- 其他配置 -->    
            <property name="hibernate.hbm2ddl.auto">update</property>
            <property name="hibernate.show_sql">true</property>
        </session-factory>
    </hibernate-configuration>
    <!--说明:在这里不配置数据库连接信息,是在后面Spring中整合Hibernate时配置的(配置c3p0数据库连接池)。-->

     Ⅲ、[config/beans.xml]配置Spring:

    <?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: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 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
        <!-- 基于注解,自动扫描与装配bean,指定到具体的包可以加快Spring的扫描速度 -->
        <context:component-scan base-package="com.atguigu.surveypark.dao.impl,com.atguigu.surveypark.service.impl,com.atguigu.surveypark.listener,com.atguigu.surveypark.struts2.action" />
        
        <!-- 分散配置:“属性占位符”扫描(即加载外部的properties配置文件) -->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        
        <!-- 配置数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <!-- 数据库连接信息 --> <property name="driverClass" value="${jdbc.driverclass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 池化信息 --> <property name="maxPoolSize" value="${c3p0.pool.size.max}" /> <property name="minPoolSize" value="${c3p0.pool.size.min}" /> <property name="initialPoolSize" value="${c3p0.pool.size.ini}" /> <property name="acquireIncrement" value="${c3p0.pool.size.increment}" /> </bean> </beans> <!--注:以上Spring的名空间并不是都需要加进来,只需根据需要加入相应的即可-->
    jdbc.properties配置文件的内容:
        jdbc.driverclass=com.mysql.jdbc.Driver
        jdbc.url=jdbc:mysql://locallhost:3306/lsn_surveypark001
        jdbc.username=root
        jdbc.password=root
    
        c3p0.pool.size.max=10
        c3p0.pool.size.min=2
        c3p0.pool.size.ini=3
        c3p0.pool.size.increment=2

    5)测试数据源:

     1 package com.atguigu.surveypark.test;
     2 import java.sql.SQLException;
     3 import javax.sql.DataSource;
     4 import org.junit.Test;
     5 import org.springframework.context.ApplicationContext;
     6 import org.springframework.context.support.ClassPathXmlApplicationContext;
     7 
     8 public class TestDataSource {
     9     @Test
    10     public void getConnection() throws SQLException{
    11       ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    12       DataSource ds = (DataSource) ac.getBean("dataSource");
    13       System.out.println(ds.getConnection());
    14     }
    15 }

    四、整合SSH

    1)集成 Spring-3.1.0 与 Hibernate-3.6:

    Ⅰ 、配置:配置Session工厂及事务管理

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xmlns:cache="http://www.springframework.org/schema/cache"
     8     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
     9         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    10         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
    11         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    12         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    13     <!-- 基于注解,自动扫描与装配bean,指定到具体的包可以加快Spring的扫描速度 -->
    14     <context:component-scan base-package="com.atguigu.surveypark.dao.impl,com.atguigu.surveypark.service.impl,com.atguigu.surveypark.listener,com.atguigu.surveypark.struts2.action" />
    15     
    16     <!-- 分散配置:“属性占位符”扫描(即加载外部的properties配置文件) -->
    17     <context:property-placeholder location="classpath:jdbc.properties"/>
    18     
    19     <!-- 配置数据源 -->
    20     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    21         <property name="driverClass" value="${jdbc.driverclass}" />
    22         <property name="jdbcUrl" value="${jdbc.url}" />
    23         <property name="user" value="${jdbc.username}" />
    24         <property name="password" value="${jdbc.password}" />
    25         
    26         <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
    27         <property name="minPoolSize" value="${c3p0.pool.size.min}" />
    28         <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
    29         <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
    30     </bean>
    31     
    32     <!-- 本地回话工厂bean(spring整合hibernate的核心入口) -->
    33     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    34         <property name="dataSource" ref="dataSource" />
    35         <!--引入Hibernate的配置文件-->
    36         <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    37         <!--映射目录位置集(可以配置多个):即指定Hibernate映射文件的目录的位置,之后Spring会自动扫描并载入所有的映射文件-->
    38         <property name="mappingDirectoryLocations">
    39             <list>
    40                 <value>classpath:com/atguigu/surveypark/model</value>
    41             </list>
    42         </property>
    43     </bean>
    44     
    45     <!-- hibnerate事务管理器,用来在service层面上实现事务管理,而且达到平台无关性 -->
    46     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    47         <property name="sessionFactory" ref="sessionFactory" />
    48     </bean>
    49     
    50     <!-- 事务通知 -->
    51     <tx:advice id="txAdvice" transaction-manager="txManager">
    52         <tx:attributes>
    53             <!-- 写操作 -->
    54             <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
    55             <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
    56             <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
    57             <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>
    58             
    59             <!-- 读操作 -->
    60             <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
    61             <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
    62             <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
    63             
    64             <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
    65         </tx:attributes>
    66     </tx:advice>
    67     
    68     <!-- aop配置 -->    
    69     <aop:config>
    70         <aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))"/>
    71     </aop:config>
    72 </beans>

    注:以上主要是添加了第32-71行的配置,用来整合Spring与Hibernate,并且添加了事务管理。在这里事务管理采用的是“声明式的事务管理”方式,除了配置事务管理器HibernateTransactionManager之外,还要配置AOP的事务通知相关内容。我们还可以使用“基于注解的事务管理”方式,其主要配置如下:

    <!-- 配置声明式的事务管理器(采用基于注解的方式) -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- 配置基于注解的事务支持,指定特定的事务管理器-->
    <tx:annotation-driven transaction-manager="txManager"/>

    这样,就可以不必在Spring的配置文件中配置事务通知的相关内容,只需在相应的类上使用@Transactional注解即可。

    Ⅱ、测试:插入用户

     1 package com.atguigu.surveypark.test;
     2 import java.sql.SQLException;
     3 import org.junit.BeforeClass;
     4 import org.junit.Test;
     5 import org.springframework.context.ApplicationContext;
     6 import org.springframework.context.support.ClassPathXmlApplicationContext;
     7 import com.atguigu.surveypark.model.User;
     8 import com.atguigu.surveypark.service.UserService;
     9 
    10 /**
    11  * 测试UserService
    12  */
    13 public class TestUserService {
    14     private static UserService us ;
    15     @BeforeClass //在测试方法执行之前执行
    16     public static void iniUserService(){
    17         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    18         us = (UserService) ac.getBean("userService");
    19     }
    20     /**
    21      * 插入用户 
    22      */
    23     @Test
    24     public void insertUser() throws SQLException{
    25         User u = new User();
    26         u.setEmail("xupccc@hotmail.com");
    27         u.setPassword("123456");
    28         u.setNickName("stone");
    29         us.saveEntity(u);
    30     }
    31 }

    Ⅲ、在web.xml中配置 spring 的 OpenSessionInView 过滤器(解决抛LazyInitializationException的问题)

    <!-- 配置 spring 的 OpenSessionInView 过滤器 -->
    <filter>
        <filter-name>OpenSessionInView</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInView</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

    LazyInitializationException异常说明:
    1,对于集合属性,默认是lazy="true"的,在第一次使用时才加载。
    2,但在加载时,如果Session已经关掉了就会抛LazyInitializationException异常

    2)集成 Spring-3.1.0 与 Struts2-2.3.15

    Ⅰ、配置:在web.xml配置spring的监听器

    <!-- 通过上下文参数配置spring文件的位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    
    <!-- 上下文载入器监听器,确保web服务器启动时,直接完成spring容器的初始化 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

      注:项目lib中要有struts2-spring插件:struts2-spring-plugin-version.jar。

    Ⅱ、测试(略)

      说明:

    1,在写Action时要指定 @Controller 与 @Scope("prototype")注解
    2,在struts.xml中配置action时,在class属性中写bean的名称。

     注:以上总结内容来自徐培成数据采集系统和汤阳光OA系统。

  • 相关阅读:
    31 把数组排成最小的数 + 仿函数的写法就记忆这个就行了
    30 整数中1出现的次数(从1到n整数中1出现的次数)这题很难要多看*
    29 连续子数组的最大和
    c/c++ struct的大小以及sizeof用法
    28 最小的K个数
    27 数组中出现次数超过一半的数字
    26 字符串的排列
    Python 实例2—购物车
    python_threading模块实现多线程详解(转)
    Ubuntu 16.04 安装Postman
  • 原文地址:https://www.cnblogs.com/KnightKitt/p/3947461.html
Copyright © 2011-2022 走看看