Eclipse下基于mavenssm框架的搭建

Eclipse下基于mavenssm框架的搭建

1.首先创建一个maven项目,项目的结构大体如图所示

2.编写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.pypua</groupId>
  <artifactId>ssm-maven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
<!-- 用来设置版本号 -->
  <properties>
    <spring_version>3.2.4.RELEASE</spring_version>
    <mybatis_version>3.2.6</mybatis_version>
    <slf4j_version>1.4.2</slf4j_version>
    <log4j_version>1.2.17</log4j_version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
<!-- 用到的jar包依赖 --> 
  <dependencies>
<!-- 单元测试 -->    
    <dependency>    
        <groupId>junit</groupId>    
        <artifactId>junit</artifactId>    
        <version>4.11</version>    
        <!-- 表示开发的时候引入,发布的时候不会加载此包 -->      
        <scope>test</scope>    
    </dependency>
<!-- maven 插件 -->
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
    </dependency>
 <!-- spring相关包 start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</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-beans</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring_version}</version>
        </dependency>
<!-- spring相关包 stop -->
<!-- 日志相关 start-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j_version}</version>
        </dependency>
        <dependency>    
            <groupId>org.slf4j</groupId>    
            <artifactId>slf4j-api</artifactId>    
            <version>${slf4j_version}</version>    
        </dependency>
        <dependency>    
            <groupId>log4j</groupId>    
            <artifactId>log4j</artifactId>    
            <version>${log4j_version}</version>    
        </dependency>
<!-- 日志相关 stop -->
<!-- 数据库驱动 -->
        <dependency>
            <groupId>com.oracle</groupId>
             <artifactId>ojdbc6</artifactId>
             <version>11.2.0.1.0</version>
        </dependency>
<!-- Json  -->    
        <!-- 格式化对象,方便输出日志 -->    
        <dependency>    
            <groupId>com.alibaba</groupId>    
            <artifactId>fastjson</artifactId>    
            <version>1.2.31</version>    
        </dependency>    
        <dependency>    
            <groupId>org.codehaus.jackson</groupId>    
            <artifactId>jackson-mapper-asl</artifactId>    
            <version>1.9.13</version>    
        </dependency>
        <!-- java 编译jar包 -->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
<!-- servletApi依赖 -->                 
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
<!-- AspectJ start-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.4</version>
        </dependency>
<!-- AspectJ stop-->
<!-- mybatis相关 start-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.3.1</version>
        </dependency>
<!-- mybatis相关 stop-->        
     
        <!-- jTom多数据源相关 -->
        <dependency>
            <groupId>jotm</groupId>
            <artifactId>jotm</artifactId>
            <version>2.0.10</version>
        </dependency>
        <dependency>
            <groupId>jotm</groupId>
            <artifactId>jotm_iiop_stubs</artifactId>
            <version>2.0.10</version>
        </dependency>
        <dependency>
            <groupId>javax.resource</groupId>
            <artifactId>connector-api</artifactId>
            <version>1.5</version>
        </dependency>
        <dependency>
            <groupId>com.experlog</groupId>
            <artifactId>xapool</artifactId>
            <version>1.5.0</version>
        </dependency>
        <!-- jstl el-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
        </dependency>
        <!--分页工具  -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
        </dependency>
        <!-- Redis相关 -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!-- 缓存相关 -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.3</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.11</version>
        </dependency>
        
        <!--rabbitmq依赖 -->  
        <dependency>  
            <groupId>org.springframework.amqp</groupId>  
            <artifactId>spring-rabbit</artifactId>  
            <version>1.3.5.RELEASE</version>  
        </dependency>
<!-- 工具类相关 start -->
        <!-- StringUtil等工具类 -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- 编码工具类 -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.8</version>
        </dependency>
        <!-- 集合工具类 -->
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2</version>
        </dependency>
        <!-- beans工具类 -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.3</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
        </dependency>
        <!-- 加载资源文件工具类 -->
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.4</version>
        </dependency>
<!-- http相关 start -->        
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxrpc-api</artifactId>
            <version>1.1</version>
        </dependency>
<!-- http相关 stop -->
<!-- 数据源连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.20</version>
        </dependency>
<!-- POI依赖包 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
    <!-- 上传附件 -->    
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
            
  </dependencies>
 <!-- 打包设置 --> 
  <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        <finalName>ssm-maven</finalName>
    </build>
</project>

3.编写jdbc.properties

#oracle
db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@localhost:1521:ORCL
db.user=test1
db.password=pypua
#\u914D\u7F6E\u6269\u5C55\u63D2\u4EF6 \u76D1\u63A7\u7EDF\u8BA1\u7528filters:stat \u65E5\u5FD7\u7528filters:log4j \u9632\u5FA1sql\u6CE8\u5165\u7528filters:wall
druid.filters:stat
#\u6700\u5927\u8FDE\u63A5\u6C60\u6570\u91CF  \u521D\u59CB\u5316\u5EFA\u7ACB\u7269\u7406\u8FDE\u63A5\u7684\u4E2A\u6570  \u83B7\u53D6\u8FDE\u63A5\u65F6\u6700\u957F\u7684\u7B49\u5F85\u65F6\u95F4  \u6700\u5C0F\u8FDE\u63A5\u6C60\u6570\u91CF  maxIdle\u5DF2\u7ECF\u5F03\u7528
druid.maxActive =20
druid.initialSize =1
druid.maxWait=60000
druid.minIdle=10
#\u6709\u4E24\u4E2A\u542B\u4E49 1.Destroy \u7EBF\u7A0B\u4F1A\u68C0\u6D4B\u8FDE\u63A5\u7684\u65F6\u95F4 2.testWhileIdle\u7684\u5224\u65AD\u4F9D\u636E
druid.timeBetweenEvictionRunsMillis:60000
#Destory\u7EBF\u7A0B\u4E2D\u5982\u679C\u68C0\u6D4B\u5230\u5F53\u524D\u8FDE\u63A5\u7684\u6700\u540E\u6D3B\u8DC3\u65F6\u95F4\u548C\u5F53\u524D\u65F6\u95F4\u7684\u5DEE\u503C\u5927\u4E8EminEvictableIdleTimeMillis\uFF0C\u5219\u5173\u95ED\u5F53\u524D\u8FDE\u63A5
druid.minEvictableIdleTimeMillis:300000
#\u7528\u6765\u68C0\u6D4B\u8FDE\u63A5\u662F\u5426\u7684sql\uFF0C\u8981\u6C42\u662F\u4E00\u4E2A\u67E5\u8BE2\u8BED\u53E5\u3002\u5728mysql\u4E2D\u901A\u5E38\u8BBE\u7F6E\u4E3ASELECT 'X'
druid.validationQuery:SELECT 1  FROM DUAL
#\u7533\u8BF7\u8FDE\u63A5\u7684\u65F6\u5019\u68C0\u6D4B\uFF0C\u5982\u679C\u7A7A\u95F2\u65F6\u95F4\u5927\u4E8EtimeBetweenEvictionRunsMillis\uFF0C\u6267\u884CvalidationQuery\u8FDE\u63A5\u662F\u5426\u6709\u6548
druid.testWhileIdle:true
#\u7533\u8BF7\u8FDE\u63A5\u65F6\u6267\u884CvalidationQuery\u68C0\u6D4B\u8FDE\u63A5\u662F\u5426\u6709\u6548 \u8FD9\u4E2A\u914D\u7F6E\u4F1A\u964D\u4F4E\u6027\u80FD
druid.testOnBorrow:false
#\u5F52\u8FD8\u8FDE\u63A5\u65F6\u6267\u884CvalidationQuery\u68C0\u6D4B\u8FDE\u63A5\u662F\u5426\u6709\u6548 \u8FD9\u4E2A\u914D\u7F6E\u4F1A\u964D\u4F4E\u6027\u80FD
druid.testOnReturn:false
#\u8981\u542F\u7528PSCache\uFF0C\u5FC5\u987B\u914D\u7F6E\u5927\u4E8E0\uFF0C\u5F53\u5927\u4E8E0\u65F6\uFF0CpoolPreparedStatements\u81EA\u52A8\u89E6\u53D1\u4FEE\u6539\u4E3Atrue
druid.maxOpenPreparedStatements:20
#\u5BF9\u4E8E\u5EFA\u7ACB\u8FDE\u63A5\u8D85\u8FC7removeAbandonedTimeout\u7684\u8FDE\u63A5\u5F3A\u5236\u5173\u95ED
druid.removeAbandoned:true
#\u6307\u5B9A\u8FDE\u63A5\u5EFA\u7ACB\u591A\u957F\u5C31\u88AB\u5F3A\u5236\u5173\u95ED
druid.removeAbandonedTimeout:1800
#\u6307\u5B9A\u53D1\u751Fremoveabandoned\u65F6\uFF0C\u662F\u5426\u8BB0\u5F55\u5F53\u524D\u7EBF\u7A0B\u7684\u5806\u6808\u4FE1\u606F\u5230\u65E5\u5FD7\u4E2D
druid.logAbandoned:true

4.log4j.properties

### set log levels ###
log4j.rootLogger = DEBUG, stdout , I , D, E
log4j.additivity.org.apache = true
### \u8f93\u51fa\u5230\u63a7\u5236\u53f0 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS}(%r) [%-5p] [%t] --> %l:%m%n

### \u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6 ###
log4j.appender.I = org.apache.log4j.DailyRollingFileAppender
log4j.appender.I.File = ${catalina.home}/logs/info.log
log4j.appender.I.Append = true
log4j.appender.I.DatePattern = '.'yyyy-MM-dd
## \u8f93\u51faINFO\u7ea7\u522b\u4ee5\u4e0a\u7684\u65e5\u5fd7
log4j.appender.I.Threshold = INFO 
log4j.appender.I.layout = org.apache.log4j.PatternLayout
log4j.appender.I.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS}(%r) [%-5p] [%t] --> %l:%m%n


### \u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${catalina.home}/logs/debug.log
log4j.appender.D.Append = true
## \u8f93\u51faDEBUG\u7ea7\u522b\u4ee5\u4e0a\u7684\u65e5\u5fd7
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS}(%r) [%-5p] [%t] --> %l:%m%n

### \u4fdd\u5b58\u5f02\u5e38\u4fe1\u606f\u5230\u5355\u72ec\u6587\u4ef6 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
## \u5f02\u5e38\u65e5\u5fd7\u6587\u4ef6\u540d
log4j.appender.E.File = ${catalina.home}/logs/error.log
log4j.appender.E.Append = true
## \u53ea\u8f93\u51faERROR\u7ea7\u522b\u4ee5\u4e0a\u7684\u65e5\u5fd7
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS}(%r) [%-5p] [%t] --> %l:%m%n

log4j.logger.com.crs.ticket=DEBUG
log4j.logger.com.mchange=INFO
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache.ibatis.logging.Log=INFO
log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.Statement=INFO
log4j.logger.java.sql.PreparedStatement=INFO

5.mybatis-configuration.xml

<?xml version="1.0" encoding="UTF-8" ?>  
  
<!DOCTYPE configuration PUBLIC   
    "-//mybatis.org//DTD Config 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration>
<!-- 配置数据库方言 -->
     <properties>
        <!--property name="dialect" value="mysql"/--> 
        <property name="dialect" value="oracle"/>
     </properties>
<!-- 注入log4j -->
     <settings>
        <setting name="logImpl" value="LOG4J"/>
     </settings>
</configuration>

6.spring-config.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       ">
<!-- spring配置文件 -->

<!-- 引入资源配置文件 -->
 <import resource="spring-proploader.xml" />
 
<!-- 引入mybatis配置文件 -->
<import resource="spring-mybatis.xml" />
<!-- 引入rabbitmq配置文件
<import resource="rabbitmq.xml" />
 -->
<!-- 配置扫描注解beans  扫描指定package下所有带有如@controller,@services,@resource,@ods并把所注释的注册为Spring Beans -->
 <context:component-scan base-package="com.crs">
     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>
 
 <!-- 引入Redis配置文件 -->
 <!--  <import resource="spring-redis.xml" />-->
 
 </beans>

7.spring-mybatis.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
    <!-- mybatis 配置 -->
    <!-- destroy-method="close"当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用. -->
    <bean id="myBatisDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!-- 数据库驱动 -->
        <property name="driverClassName" value="${db.driver}"></property>
        <!-- 数据库url -->
        <property name="url" value="${db.url}"></property>    
        <!-- 数据库用户名 -->
        <property name="username" value="${db.user}"></property>
        <!-- 数据库密码 -->
        <property name="password" value="${db.password}"></property>
        
        <!-- 配置扩展插件 监控统计用filters:stat 日志用filters:log4j 防御sql注入用filters:wall -->
        <property name="filters" value="${druid.filters}"></property>
        
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="${druid.maxActive}" />
        
        <!--初始化连接数量 -->   
        <property name="initialSize" value="${druid.initialSize}" />
        
        <!--配置获取连接等待超时的时间 -->   
        <property name="maxWait" value="${druid.maxWait}" />
        
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="${druid.minIdle}" />
        
        <!--配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒-->   
        <property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
        
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <!-- Destory线程中如果检测到当前连接的最后活跃时间和当前时间的差值大于minEvictableIdleTimeMillis,则关闭当前连接 -->
        <property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
        <!-- 用来检测连接是否的sql,要求是一个查询语句。在mysql中通常设置为SELECT 'X' -->
        <property name="validationQuery" value="${druid.validationQuery}" />
        <!-- 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery连接是否有效 -->
        <property name="testWhileIdle" value="${druid.testWhileIdle}" />
        <!-- 申请连接时执行validationQuery检测连接是否有效 这个配置会降低性能 -->
        <property name="testOnBorrow" value="${druid.testOnBorrow}" />
        <!-- 归还连接时执行validationQuery检测连接是否有效 这个配置会降低性能 -->
        <property name="testOnReturn" value="${druid.testOnReturn}" />
        <!-- 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true -->
        <property name="maxOpenPreparedStatements" value="${druid.maxOpenPreparedStatements}" />
        
        <!-- 对于建立连接超过removeAbandonedTimeout的连接强制关闭 -->
        <property name="removeAbandoned" value="${druid.removeAbandoned}" />
        <!-- 指定连接建立多长就被强制关闭,1800秒,也就是30分钟 -->
         <property name="removeAbandonedTimeout" value="${druid.removeAbandonedTimeout}" />
         <!-- 指定发生removeabandoned时,是否记录当前线程的堆栈信息到日志中,关闭abanded连接时输出错误日志 -->   
         <property name="logAbandoned" value="${druid.logAbandoned}" />
    </bean>
    
    <!-- 配置session工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="myBatisDataSource"></property>
        <property name="configLocation" value="classpath:config/mybatis-configuration.xml"></property>
    </bean>
    
    <!-- 配置mybatis Mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.crs.**.mapper" />
    </bean>
    
    <!-- 配置声明式事务管理 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="myBatisDataSource" />
    </bean>
    
    <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="REQUIRED" />
            <tx:method name="query*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="REQUIRED" />
            <tx:method name="select*" propagation="REQUIRED" />
            <tx:method name="list*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置spring AOP --><!-- expression="execution(* com.crs..service..*.*(..)))任意返回值的定义在service包以及子包中的任意方法,..代表参数 -->
    <aop:config>
        <aop:pointcut id="pc"
            expression="execution(* com.crs..service..*.*(..)))" />
        <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
    </aop:config>
</beans>

8.spring-proploader.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       ">
<!-- 配置加载资源文件 -->
<context:property-placeholder 
location="classpath:config/jdbc.properties"
/>

</beans>

9.spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    
<!-- 配置国际化支持 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <!-- 国际化信息所在的文件名 -->                     
        <property name="basename">
            <value>messages/message</value>
        </property>   
        <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称  -->               
        <property name="useCodeAsDefaultMessage" value="true" />           
    </bean>
    
    <mvc:interceptors>
        <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 --> 
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />  
    </mvc:interceptors>
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />    
    
    <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 -->
    <mvc:annotation-driven />
    <mvc:default-servlet-handler/>
    <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean 
    开启controller注解支持-->
    <context:component-scan base-package="com.crs">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan> 
    
    
    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:resources mapping="/upload/**" location="/upload/" />
    <mvc:resources mapping="/script/**" location="/script/" />
    <mvc:resources mapping="/style/**" location="/style/" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/view/" p:suffix=".jsp" />
        
<!-- 上传附件 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
        p:defaultEncoding="utf-8" />
        
    <!-- 自动解析action返回值 解析成json格式 -->
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=utf-8</value>
                <value>text/plain;charset=utf-8</value>
            </list>
        </property>
    </bean>
</beans>

10.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_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 配置spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            classpath:config/spring-config.xml
        </param-value>
  </context-param>
  
      
  <!-- 配置监听类 -->
  <listener>
      <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>  
     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
 
   <!-- log4j   -->
  <context-param>
      <param-name>webAppRootKey</param-name>
      <param-value>gp.webapp.root</param-value>
  </context-param>
  <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:config/log4j.properties</param-value>
  </context-param>
  <context-param>
          <param-name>log4jRefreshInterval</param-name>
          <param-value>60000</param-value>
  </context-param>

  <!-- spring mvc -->
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:config/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
    
     <!-- spring mvc  character set -->
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 连接池 启用Web监控统计功能   start-->
    <filter>
        <filter-name>DruidWebStatFilter</filter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
        <init-param>
            <param-name>exclusions</param-name> <!-- 经常需要排除一些不必要的url -->
            <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>DruidWebStatFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class><!-- 这个StatViewServlet的用途包括:提供监控信息展示的html页面;提供监控信息的JSON API -->
          <init-param>    
        <!-- 用户名,用户名和密码可以不写,不写时不需要输入,直接登录 -->  
            <param-name>loginUsername</param-name>    
            <param-value>admin</param-value>    
        </init-param>    
        <init-param>    
        <!-- 密码 -->  
            <param-name>loginPassword</param-name>    
            <param-value>admin</param-value>    
      </init-param> 
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>
    <!-- 连接池 启用Web监控统计功能   end-->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

以上是所有配置!!!

posted @ 2017-08-29 10:43  十月围城小童鞋  阅读(423)  评论(0编辑  收藏  举报