zoukankan      html  css  js  c++  java
  • 项目环境搭建

    1.项目整体架构

    2.划分工程
    ①Survey_1_UI[Web工程]
    JSP
    静态资源:jQuery库、bootstrap.js、bootstrap.css、图片
    配置文件:Spring配置文件、Log4j、连数据库的信息、EHCache……
    ②Survey_2_Component[Java工程]
    组件:handler、service、mapper接口
    SpringMVC自定义拦截器
    自定义切面
    Spring监听器
    自定义标签
    ……
    ③Survey_3_Public[Java工程]
    实体类
    工具类
    自定义异常
    ……
    ④Survey_4_Environment[Java工程]
    引入框架和第三方工具的依赖

    3、maven 创建Survey_1_UI[Web工程]

    maven创建web工程

    1)在webapp下面创建web目录

    2)加入jsp的依赖

      <dependencies>
          <!-- web工程需要依赖jsp -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1.3-b06</version>
            <scope>provided</scope>
        </dependency>
      </dependencies>

    Survey_2_Component工程的package结构

    com.lamsey.survey
    com.lamsey.survey.component
    com.lamsey.survey.component.handler
    com.lamsey.survey.component.handler.guest
    com.lamsey.survey.component.handler.manager
    com.lamsey.survey.component.mapper
    com.lamsey.survey.component.service
    com.lamsey.survey.component.service.i
    com.lamsey.survey.component.service.m
    com.lamsey.survey.interceptors
    com.lamsey.survey.log
    com.lamsey.survey.log.aspect
    com.lamsey.survey.log.listener
    com.lamsey.survey.log.router
    com.lamsey.survey.log.thread
    Survey_3_Public工程的package结构
    com.atguigu.survey
    com.atguigu.survey.e
    com.atguigu.survey.entities
    com.atguigu.survey.entities.guest
    com.atguigu.survey.entities.manager
    com.atguigu.survey.utils

    4.这几个工程的依赖关系

    5.加入框架的依赖信息(在Survey_4_Environment中)

    <dependencies>
         <!-- 辅助 -->
        <dependency>
        <!-- Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地 -->
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- CGLIB是一个功能强大,高性能的代码生成包。它为没有实现接口的类提供代理,为JDK的动态代理提供了很好的补充。 -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>
        <!-- AspectJ:Java社区里最完整最流行的AOP框架。
        在Spring2.0以上版本中,可以使用基于AspectJ注解或基于XML配置的AOP。
         -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        <!-- SLF4J,即简单日志门面(Simple Logging Facade for Java),
        不是具体的日志解决方案,它只服务于各种各样的日志系统。 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>  
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.7</version>
        </dependency>
          <!-- springMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
        <!-- JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。 -->
          <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
          <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
          <!-- 数据库连接池 -->
          <dependency>
              <groupId>com.mchange</groupId>
              <artifactId>c3p0</artifactId>
              <version>0.9.2</version>
          </dependency>
          <!-- spring的IOC容器 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>     
          <!-- 数据库的依赖信息 -->
          <!--Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发  -->
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
        <!-- 对象-关系映射(OBJECT/RELATIONALMAPPING,简称ORM),
        是随着面向对象的软件开发方法发展而产生的。用来把对象模型表示的对象映射到
        基于S Q L 的关系模型数据库结构中去。 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
        <!-- mysql的java驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
      </dependencies>

    spring整合mybatis
    1)创建spring容器
    2)spring整合数据源,事务管理等
    3)整合mybatis
    创建sqlSessionFactoryBean
    配置映射文件
    创建数据库survey170228_main

    ②创建数据库
    CREATE DATABASE `survey170228_main`CHARACTER SET utf8;
    ③创建jdbc.properties
    [1]allowMultiQueries=true
    含义:允许执行用“;”分开的多条SQL语句。
    [2]useUnicode=true&characterEncoding=utf8
    含义:设置Java程序连接数据库时使用的字符集

    创建log4j.properties和log4j.xml一样配置log4j在mybatis使用时的属性。

    spring整合springMVC

    applicationContext-mvc.xml

    <?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:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            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">
        <!--1.扫描控制器的组件  
        将默认扫描所有组件关闭后再帅选
        -->
        <context:component-scan base-package="com.lamsey.survey.component.*" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
        <!-- 2.配置视图解析器,简化 
        常用的视图解析器实现类
        InternalResourceViewResolver网络资源视图解析器
        将视图名解析为一个url文件
        -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"/>
            <property name="suffix" value=".jsp"/>    
        </bean>
        <!--3.开启增强版  -->
        <mvc:annotation-driven/>
        <mvc:default-servlet-handler/>
    </beans>

    经过整合后的spring的配置文件

    applicationContext.xml

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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">
        <!-- 1.扫描所有包 -->
    <!--     <context:component-scan base-package="">
            
        </context:component-scan> -->
        <context:component-scan base-package="com.lamsey.survey.component.*">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
        <!--2.配置数据源  -->
        <context:property-placeholder location="classpath:dbconfig.properties"/>
        <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${prop.user}"></property>
            <property name="password" value="${prop.password}"></property>
            <property name="jdbcUrl" value="${prop.jdbcUrl}"></property>
            <property name="driverClass" value="${prop.driverClass}"></property>
        </bean>
        <!--3.配置数据库事务  -->
        <!-- 配置事务管理器 -->
        <bean id="dataSourcetransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
            <property name="dataSource" ref="comboPooledDataSource"></property>
        </bean>
        <aop:config>
            <!-- 数据库事务切入点,精确定位 -->
            <!-- 不限返回值,不限包名,以service结尾的所有方法 -->
            <aop:pointcut expression="execution(* *..*service.*(..))" id="txPoint"/>
            <!--增强版事务管理  -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
        </aop:config>
        <tx:advice id="txAdvice" transaction-manager="dataSourcetransactionManager">
            <!--配置切入的每一个事务方法的细节  -->
            <tx:attributes>
                <!--查询只需读就可以,设置为只读提高效率  -->
                <tx:method name="get*" read-only="true"/>
                <!-- 保存操作,无论什么异常皆回滚 -->
                <tx:method name="save*" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/>
                <!-- Spring的声明式事务默认使用的传播行为是:REQUIRED,含义是:必须在事务中运行,但如果已经有事务则在现有事务中运行,不开启新事务 -->
                <!-- 在其他事务中运行的风险是:如果同一个事务中的其他操作回滚了,当前操作也会被连累。 -->
                <!-- 所以应该使用REQUIRES_NEW,必须运行在事务中,且必须开启新事务。 -->
                <tx:method name="delete*" propagation="REQUIRES_NEW"/>
                <tx:method name="update*" propagation="REQUIRES_NEW"/>
            </tx:attributes>
        </tx:advice>
        <!-- 整合mybatis -->
        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="comboPooledDataSource"></property>
            <!-- 加载mybatis的配置 -->
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
            <!-- 给实体类批量起别名 -->
            <!-- 指定实体类所在的包 -->
            <property name="typeAliasesPackage" value="com.lamsey.survey.entities"></property>
        </bean>
        <!-- 配置mapper扫描器 -->
        <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.lamsey.survey.component.mapper"></property>
        </bean>
    </beans>

    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>123</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <!-- 1)创建spring容器 -->
      <!-- needed for ContextLoaderListener -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <!-- Bootstraps the root web application context before servlet initialization -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!--2.设置springMVC前段控制器  -->
        <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
        <servlet>
            <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- Map all requests to the DispatcherServlet for handling -->
        <!-- 必须写/  -->
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!--3.编码格式设置  -->
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>    
        </filter-mapping>
        <!-- 4.HiddenHttpMethodFilter将from下的post和get转化支持REST模式
        资源表现层状态转化,同样的URI路径请求,可以设置GET、POST、PUT 与 DELETE 请求
         -->
        <filter>
            <filter-name>hiddenHttpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>hiddenHttpMethodFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>

    小结:1)对于maven工程配置文件放在resources文件夹下,resources和java文件夹下都相当于classpath

       2)在web.xml 的DispatcherServlet映射拦截必须为“/”

       3)通过<base>标签确定资源的访问路径,其会与后续路径连接起一个完整的路径

    <base href="http://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />

    mybatis的逆向工程(MyBatis Generator)

    简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写

    参考文档:

    https://github.com/mybatis/generator

    ①创建一个专门的工程用于生成Java文件
    ②pom.xml配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.lamsey.maven</groupId>
      <artifactId>SurveyInverse</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <dependencies>
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                    <version>3.2.8</version>
                </dependency>
     </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                    <dependency>
                        <groupId>com.mchange</groupId>
                        <artifactId>c3p0</artifactId>
                        <version>0.9.2</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.8</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    </project>

    ③创建generatorConfig.xml,保存到src/main/resources目录下
    说明信息参见:mybatis-generator-core-1.3.2的官方文档

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE generatorConfiguration
      PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
      "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    
    <generatorConfiguration>
      <context id="DB2Tables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
          <commentGenerator>
              <!-- 是否去除自动生成的注释 true:是;false:否 -->
              <property name="suppressAllComments" value="true"/>
          </commentGenerator>
          <!-- 连接数据库 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/survey170228_main"
            userId="root"
            password="537423Ab">
        </jdbcConnection>
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
                            和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver >
          <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 用于指定生成的Java模型对象的目标包和目标项目的元素
        .表示当前目录自身
        ..表示当前目录的父目录
         -->
        <javaModelGenerator targetPackage="com.lamsey.survey.entities.guest" targetProject=".srcmainjava">
          <!-- enableSubPackages:是否让schema作为包的后缀 -->
          <property name="enableSubPackages" value="true" />
          <!-- 从数据库返回的值被清理前后的空格 -->
          <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 用于指定生成的SQL映射文件的目标包和目标项目的元素 -->
        <sqlMapGenerator targetPackage="com.lamsey.survey.component.mapper"  targetProject=".srcmainjava">
          <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- targetPackage:Mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.lamsey.survey.component.mapper"  targetProject=".srcmainjava">
          <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
    
        <!-- 指定逆向生成的数据表
        tableName指定表名
        domainObjectName指定生成对象的名字
         -->
        <table tableName="test_employee" domainObjectName="Employee"></table>
      </context>
    </generatorConfiguration>

    Eclispe插件运行maven命令:

    mybatis-generator:generate

  • 相关阅读:
    警告:ORA-00600 2252 错误正在SCN问题下不断爆发(转)
    Linux批量清除木马文件photo.scr
    500 OOPS: vsftpd: refusing to run with writable root inside chroot() Login failed. 421 Service not available, remote server has closed connection
    Linux后门入侵检测工具(转)
    解决Docker无法删除镜像
    通过DataX从Oracle同步数据到MySQL-安装配置过程
    Server2008 R2安装、配置Freesshd(Jenkins持续集成-Windows)
    Mysql死锁解决办法
    Mssql 2017修改master默认排序规则
    可能需要用到的Mac技巧or软件
  • 原文地址:https://www.cnblogs.com/limingxian537423/p/7348064.html
Copyright © 2011-2022 走看看