带你学会maven实现ssh整合完整篇

Java开发 同时被 2 个专栏收录
11 篇文章 0 订阅
3 篇文章 0 订阅

-------  前言:大家好,今天给大家带来的ssh框架的整合。由于本篇文章篇幅较长,文章会采用目录的形式对内容进行整理。也是为了方便大家按照顺序进行学习。分别介绍了idea(itelliJ IDEA)和eclipseEE两种开发工具下的maven项目构建。

一:开始前需要了解的知识

1.1 什么是Maven?(工具)

Maven是一个项目管理和综合工具。Maven提供了开发人员构建一个完整的生命周期框架。开发团队可以自动完成项目的基础工具建设,Maven使用标准的目录结构和默认构建生命周期。
在多个开发团队环境时,Maven可以设置按标准在非常短的时间里完成配置工作。由于大部分项目的设置都很简单,并且可重复使用,Maven让开发人员的工作更轻松,同时创建报表,检查,构建和测试自动化设置。

1.2 Maven Web是什么?(和普通web项目的区别)

即通过Maven创建的Web项目,同普通项目不同,是由Maven工具创建的web项目,它们的区别不仅仅在于项目结构的不同,最主要的Maven项目具有一个关键的 pom.xml文件。
项目结构的不同并不会影响web项目的运行,因为最终都会编译后运行,最关键的是其中的代码和资源文件。而不是项目的包结构。


二:开发前的准备工作

2.1 EclipseEE的Maven插件的安装

eclipseEE新的版本已经集成了Maven,但是我们通常会采用自己下载的Maven工具。 点击此处进入官网下载Maven工具包。

2.1.1在eclipse中添加安装插件的位置

点击Windows->preference->Maven->Installaction->add 填写下载的Maven工具包的地址。然后勾选自己的安装包。




2.1.2 更改远程中央仓库和设置本地仓库

由于默认的远程仓库拉取太慢,所以我们需要修改默认的远程仓库地址。点击Windows->preference->Maven->User setting看到如下所示,User setting选择设置为自定义安装的Maven的conf目录下的setting.xml。第三行为本地仓库的存储地址,本地仓库的作用就是以后不用每次都去远程的中央仓库里面去拉取,如果本地仓库有的话就从本地仓库选择,没有的话再会去远程拉取。



打开setting.xml,将下面的阿里云中央仓库地址插入mirrors节点标签中。然后保存即可。
<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>       
  </mirror>


2.2 IDEA的Maven插件安装

idea点击File->setting,选择maven,然后同样配置本地仓库和远程仓库地址。本文中不详细介绍,可参考上述Eclipse思路配置,步骤基本一致。


三:开始您的web项目

3.1创建Maven web项目

在eclipseEE中选择File->new->Maven Project->next->next,如下图,选择最后一个,



group Id是包名,Artifact Id是项目名。



然后点击finish。

3.2创建基本的包结构





  • advice:存放aop增强类
  • dao:存放pojo层操作接口
  • dao.impl:存放操作接口的实现类
  • pojos:存放实体类
  • service:存放业务逻辑封装接口
  • service:存放业务逻辑封装实现类
  • utils:存放相关的工具类
  • action包:存放action类

3.3在pom.xml中导入ssh整合所需要的所有包。







下面是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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jsdl</groupId>
  <artifactId>enshop</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>enshop Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <!--统一编码格式-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!--统一各个框架版本-->
    <struts.version>2.5.14</struts.version>
    <spring.version>5.0.3.RELEASE</spring.version>
    <hibernate.version>5.2.13.Final</hibernate.version>
      <log4j2.version>2.10.0</log4j2.version>
      <asm.version>3.3</asm.version>
  </properties>
  <!--增加junit支持-->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!--struts相关-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
      <exclusions>
        <exclusion>
          <artifactId>commons-io</artifactId>
          <groupId>commons-io</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
    <scope>test</scope>
</dependency>
    
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.7</version>
    </dependency>
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.23</version>
    </dependency>
    <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-json-plugin</artifactId>
    <version>${struts.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-lang3</artifactId>
                <groupId>org.apache.commons</groupId>
            </exclusion>
        </exclusions>
    </dependency>
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-convention-plugin</artifactId>
    <version>${struts.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>struts2-core</artifactId>
            <groupId>org.apache.struts</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-lang3</artifactId>
            <groupId>org.apache.commons</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.log4j</groupId>
    <artifactId>com.springsource.org.apache.log4j</artifactId>
    <version>1.2.15</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>${struts.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-io</artifactId>
                <groupId>commons-io</groupId>
            </exclusion>
            <exclusion>
                <artifactId>commons-lang3</artifactId>
                <groupId>org.apache.commons</groupId>
            </exclusion>
            <exclusion>
                <artifactId>log4j-api</artifactId>
                <groupId>org.apache.logging.log4j</groupId>
            </exclusion>
            <exclusion>
                <artifactId>freemarker</artifactId>
                <groupId>org.freemarker</groupId>
            </exclusion>
            <exclusion>
                <artifactId>javassist</artifactId>
                <groupId>org.javassist</groupId>
            </exclusion>
            <exclusion>
                <artifactId>ognl</artifactId>
                <groupId>ognl</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.struts.xwork</groupId>
      <artifactId>xwork-core</artifactId>
      <version>2.3.34</version>
      <exclusions>
        <exclusion>
          <artifactId>ognl</artifactId>
          <groupId>ognl</groupId>
        </exclusion>
          <exclusion>
              <artifactId>commons-lang3</artifactId>
              <groupId>org.apache.commons</groupId>
          </exclusion>
      </exclusions>
    </dependency>
    <!--hibernate相关-->
    <dependency>
      <groupId>antlr</groupId>
      <artifactId>antlr</artifactId>
      <version>2.7.7</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-collections4</artifactId>
      <version>4.1</version>
    </dependency>
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.2.Final</version>
</dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>hibernate-commons-annotations</artifactId>
                <groupId>org.hibernate.common</groupId>
            </exclusion>
            <exclusion>
                <artifactId>hibernate-jpa-2.1-api</artifactId>
                <groupId>org.hibernate.javax.persistence</groupId>
            </exclusion>
            <exclusion>
                <artifactId>javassist</artifactId>
                <groupId>org.javassist</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jandex</artifactId>
                <groupId>org.jboss</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jboss-logging</artifactId>
                <groupId>org.jboss.logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>${hibernate.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>jboss-logging</artifactId>
            <groupId>org.jboss.logging</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>mchange-commons-java</artifactId>
    <version>0.2.15</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.6.11</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${hibernate.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <groupId>org.hibernate.javax.persistence</groupId>
        </exclusion>
        <exclusion>
            <artifactId>hibernate-commons-annotations</artifactId>
            <groupId>org.hibernate.common</groupId>
        </exclusion>
        <exclusion>
            <artifactId>javassist</artifactId>
            <groupId>org.javassist</groupId>
        </exclusion>
        <exclusion>
            <artifactId>jboss-logging</artifactId>
            <groupId>org.jboss.logging</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
    <exclusions>
        <exclusion>
            <artifactId>mchange-commons-java</artifactId>
            <groupId>com.mchange</groupId>
        </exclusion>
    </exclusions>
</dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.jboss</groupId>
    <artifactId>jandex</artifactId>
    <version>2.0.4.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.logging</groupId>
    <artifactId>jboss-logging</artifactId>
    <version>3.3.2.Final</version>
</dependency>
     <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.18.0-GA</version>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
    </dependency>
    <!--spring-core整合-->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--spring-web相关-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--spring-aop相关-->
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.5.4</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.13</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>${spring.version}</version>
</dependency>
    
    <!--spring-orm相关-->
<dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-jta_1.1_spec</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>org.hibernate.common</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>5.0.2.Final</version>
    <exclusions>
        <exclusion>
            <artifactId>jboss-logging</artifactId>
            <groupId>org.jboss.logging</groupId>
        </exclusion>
    </exclusions>
</dependency>
    
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.9-rc</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>
    
    <dependency>
    	<groupId>javax.servlet</groupId>
    	<artifactId>javax.servlet-api</artifactId>
    	<version>4.0.0</version>
    	<scope>provided</scope>
    </dependency>
    <!--log4j2相关-->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
    <!--commons-pool支持-->
    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.6</version>
    </dependency>
    <dependency>
    	<groupId>asm</groupId>
    	<artifactId>asm</artifactId>
    	<version>${asm.version}</version>
    </dependency>
    <dependency>
    	<groupId>asm</groupId>
    	<artifactId>asm-commons</artifactId>
    	<version>${asm.version}</version>
    </dependency>
    <dependency>
    	<groupId>asm</groupId>
    	<artifactId>asm-tree</artifactId>
    	<version>${asm.version}</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts2-spring-plugin</artifactId>
    	<version>${struts.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-lang3</artifactId>
                <groupId>org.apache.commons</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-beans</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-context</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-core</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-web</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
    	<groupId>ognl</groupId>
    	<artifactId>ognl</artifactId>
    	<version>3.1.8</version>
        <exclusions>
            <exclusion>
                <artifactId>javassist</artifactId>
                <groupId>org.javassist</groupId>
            </exclusion>
        </exclusions>
    </dependency>
  </dependencies>
  <build>
    <finalName>enshop</finalName>
    <plugins>
      <plugin>
        <!--统一源码编译的版本-->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <!--打包时候跳过junit测试-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>
      <!--配置tomcat-->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <path>/${project.artifactId}</path>
          <port>8080</port>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

3.4在pom可视化配置导入依赖包

Group Id 需要填写包名,Artfact Id填写项目名。同时需要在下面填写过滤,填写Artifact Id就可以了。这个可以百度查询,比如struts-core.jar包,我们可以通过百度struts.core maven来获取填写方式。


3.5解决jar包的版本冲突

我们的整合框架导入的这几十个包中必然有冲突包,所以必须解决冲突,框架才能正常启动。这里给大家推荐一个方法:手动解决冲突非常麻烦,然而冲突势必会有。
同时安装EclipseEE和Idea,利用idea安装Maven Helper插件,从来快速地解决冲突。idea和eclipseEE打开同一Maven web项目无任何阻碍,非常方便,甚至可以同时打开两者同时编辑。

四:配置文件的整合

4.1编写web.xml如下:


<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- 配置spring的监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfiguration</param-name>
        <param-value>log4j2.xml</param-value>
    </context-param>
    <!--配置过滤器-->
    <!-- 设置监听加载上下文 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>  
        <param-name>filterConfig</param-name>  
        <param-value>classpath:struts.xml</param-value>  
    </init-param>
    </filter>
    <!--配置过滤器地址-->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>                       
    </filter-mapping>
    <!-- 开启监听 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

其中包含了log4j2日志工具的配置文件路径声明和application.xml配置文件的路径,以及struts2的核心过滤器和spring的启动监听器。


4.2 编写log4j2.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
         <!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
        <!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出-->
        <!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
    <configuration status="WARN" monitorInterval="30">
            <!--先定义所有的appender-->
        <appenders>
        <!--这个输出控制台的配置-->
             <console name="Console" target="SYSTEM_OUT">
             <!--输出日志的格式-->
                 <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             </console>
         <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
         <File name="log" fileName="log/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>
        <!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
            <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log"
                         filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
                <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
                <Policies>
                    <TimeBasedTriggeringPolicy/>
                    <SizeBasedTriggeringPolicy size="100 MB"/>
                </Policies>
             </RollingFile>
            <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/warn.log"
                          filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
                 <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
                 <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
                 <Policies>
                     <TimeBasedTriggeringPolicy/>
                    <SizeBasedTriggeringPolicy size="100 MB"/>
                </Policies>
             <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
                 <DefaultRolloverStrategy max="20"/>
             </RollingFile>
                 <RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/error.log"
                             filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
                    <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
                    <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
                   <Policies>
               <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
             </Policies>
                </RollingFile>
             </appenders>
             <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
             <loggers>
         <!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
         <logger name="org.springframework" level="INFO"></logger>
         <logger name="org.mybatis" level="INFO"></logger>
        <root level="all">
                 <appender-ref ref="Console"/>
                 <appender-ref ref="RollingFileInfo"/>
                 <appender-ref ref="RollingFileWarn"/>
                <appender-ref ref="RollingFileError"/>
            </root>
    </loggers>
        </configuration>

4.3编写struts.xml配置文件

此文件为struts.xml的配置文件。其中配置了action类对应的声明。类似配置如下格式:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
        <package name="user" extends="struts-default" namespace="/admin">
            <action name="login" class="userAction">
                <result name="success">/admin/main.jsp</result>
                <result name="input">/admin/login.jsp</result>
            </action>
        </package>
</struts>

4.4删除hbm.cfg.xml,将hibernate配置内容放在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"
       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.xsd">
    <context:component-scan base-package="com.babi.*"/>
    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <!--数据库驱动类-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <!--数据库连接URL-->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/enshop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
        <!--数据库用户名-->
        <property name="user" value="mero"/>
        <!--数据库用户密码-->
        <property name="password" value="1312273072"/>
        <!--连接池最小保留个数-->
        <property name="minPoolSize" value="5"/>
        <!--连接池最大保留个数-->
        <property name="maxPoolSize" value="30"/>
        <!--初始化时获得的连接数-->
        <property name="initialPoolSize" value="10"/>
        <!--最大空闲时间,为0即永不连接-->
        <property name="maxIdleTime" value="60"/>
        <!--当连接池连接耗尽时,一次性获取的连接数-->
        <property name="acquireIncrement" value="5"/>
        <!--预缓存的statement个数-->
        <property name="maxStatements" value="0"/>
        <!--每60检查连接池中所有的空闲连接-->
        <property name="idleConnectionTestPeriod" value="60"/>
        <!--和数据库连接失败后请求重新连接的次数-->
        <property name="acquireRetryAttempts" value="30"/>
        <!--连接失败会通知等待的数据源连接放弃连接,数据源仍保留-->
        <property name="breakAfterAcquireFailure" value="true"/>
    </bean>
    <!--session工厂的配置-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!--为session工厂注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--为hibernate配置部分属性-->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="show_sql">true</prop>
                <prop key="format_sql">true</prop>
                <prop key="hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="mappingLocations" value="classpath:com/babi/pojos/*.hbm.xml"/>
    </bean>
    <!--配置事务管理-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- 引入dao层 -->
    <import resource="applicationContext-dao.xml"/>
    <!-- 引入service层 -->
    <import resource="applicationContext-service.xml"/>
    <!-- 引入action层 -->
    <import resource="applicationContext-action.xml"/>
</beans>

4.5创建applicationContext-dao文件

参考形式如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    " default-autowire="byName">

    <!-- 配置DAO -->
    <bean id="userDao" class="com.babi.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

</beans>

4.6创建applicationContext-service文件

形式如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
        default-autowire="byName">
    <bean id="userService" class="com.babi.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
</beans>

4.7创建applicationContext-action文件

形式如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
        default-autowire="byName">
    <bean id="userAction" class="com.babi.action.UserAction" scope="prototype">
        <property name="userService" ref="userService"/>
    </bean>
</beans>



  • 1
    点赞
  • 2
    评论
  • 10
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值