结构如下图:
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.youke</ groupId>
< artifactId> SSMS1.0</ artifactId>
< packaging> war</ packaging>
< version> 1.0-SNAPSHOT</ version>
< name> SSMS1.0 Maven Webapp</ name>
< url> http://maven.apache.org</ url>
< properties>
< project.build.sourceEncoding> UTF-8</ project.build.sourceEncoding>
< spring.version> 4.3.14.RELEASE</ spring.version>
< security.version> 4.2.5.RELEASE</ security.version>
< slf4j.version> 1.7.25</ slf4j.version>
< log4j.version> 2.8.2</ log4j.version>
< maven.compiler.source> 1.8</ maven.compiler.source>
< maven.compiler.target> 1.8</ maven.compiler.target>
</ properties>
< dependencies>
< 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-tx</ 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-webmvc</ 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-expression</ 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-test</ artifactId>
< version> ${spring.version}</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.45</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.8.10</ version>
</ dependency>
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjrt</ artifactId>
< version> 1.8.10</ version>
</ dependency>
< dependency>
< groupId> commons-fileupload</ groupId>
< artifactId> commons-fileupload</ artifactId>
< version> 1.3.3</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.4.5</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 1.3.1</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.1.6</ version>
</ dependency>
< dependency>
< groupId> commons-dbcp</ groupId>
< artifactId> commons-dbcp</ artifactId>
< version> 1.4</ version>
</ dependency>
< dependency>
< groupId> commons-pool</ groupId>
< artifactId> commons-pool</ artifactId>
< version> 1.6</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-core</ artifactId>
< version> 2.9.0.pr3</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-databind</ artifactId>
< version> 2.9.0.pr3</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-annotations</ artifactId>
< version> 2.9.0.pr3</ version>
</ dependency>
< dependency>
< groupId> com.google.code.gson</ groupId>
< artifactId> gson</ artifactId>
< version> 2.8.2</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> fastjson</ artifactId>
< version> 1.2.28</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context-support</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.freemarker</ groupId>
< artifactId> freemarker</ artifactId>
< version> 2.3.23</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 3.1.0</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> jstl</ artifactId>
< version> 1.2</ version>
</ dependency>
< dependency>
< groupId> org.slf4j</ groupId>
< artifactId> slf4j-api</ artifactId>
< version> ${slf4j.version}</ version>
</ dependency>
< dependency>
< groupId> org.slf4j</ groupId>
< artifactId> jcl-over-slf4j</ artifactId>
< version> ${slf4j.version}</ version>
< scope> runtime</ scope>
</ dependency>
< dependency>
< groupId> org.apache.logging.log4j</ groupId>
< artifactId> log4j-api</ artifactId>
< version> ${log4j.version}</ version>
</ dependency>
< dependency>
< groupId> org.apache.logging.log4j</ groupId>
< artifactId> log4j-core</ artifactId>
< version> ${log4j.version}</ version>
</ dependency>
< dependency>
< groupId> org.apache.logging.log4j</ groupId>
< artifactId> log4j-slf4j-impl</ artifactId>
< version> ${log4j.version}</ version>
</ dependency>
< dependency>
< groupId> org.apache.logging.log4j</ groupId>
< artifactId> log4j-web</ artifactId>
< version> ${log4j.version}</ version>
< scope> runtime</ scope>
</ dependency>
< dependency>
< groupId> com.lmax</ groupId>
< artifactId> disruptor</ artifactId>
< version> 3.3.7</ version>
</ dependency>
< dependency>
< groupId> org.apache.shiro</ groupId>
< artifactId> shiro-core</ artifactId>
< version> 1.4.0</ version>
</ dependency>
< dependency>
< groupId> org.apache.shiro</ groupId>
< artifactId> shiro-spring</ artifactId>
< version> 1.4.0</ version>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId> org.mybatis.generator</ groupId>
< artifactId> mybatis-generator-maven-plugin</ artifactId>
< version> 1.3.5</ version>
< configuration>
< verbose> true</ verbose>
< overwrite> true</ overwrite>
</ configuration>
</ plugin>
</ plugins>
< finalName> SSMS1.0</ finalName>
< resources>
< resource>
< directory> ${basedir}/src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
</ resource>
< resource>
< directory> ${basedir}/src/main/resources</ directory>
</ resource>
</ resources>
</ build>
</ project>
database.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/kd31?useUnicode=true&characterEncoding=utf-8
user=root
password=abc123
minIdle=45
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
applicationContext-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: jdbc= " http://www.springframework.org/schema/jdbc"
xmlns: mvc= " http://www.springframework.org/schema/mvc"
xmlns: tx= " http://www.springframework.org/schema/tx"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= " http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" >
< context: component-scan base-package = " cn.wedding.service" />
< context: component-scan base-package = " cn.wedding.dao" />
< context: property-placeholder location = " classpath:database.properties" />
< bean id = " dataSource" class = " org.apache.commons.dbcp.BasicDataSource" destroy-method = " close" scope = " singleton" >
< property name = " driverClassName" value = " ${driver}" />
< property name = " url" value = " ${url}" />
< property name = " username" value = " ${user}" />
< property name = " password" value = " ${password}" />
< property name = " initialSize" value = " ${initialSize}" />
< property name = " maxActive" value = " ${maxActive}" />
< property name = " maxIdle" value = " ${maxIdle}" />
< property name = " minIdle" value = " ${minIdle}" />
< property name = " maxWait" value = " ${maxWait}" />
< property name = " removeAbandonedTimeout" value = " ${removeAbandonedTimeout}" />
< property name = " removeAbandoned" value = " ${removeAbandoned}" />
<property name= "testWhileIdle" value="true"/>
<property name= "testOnBorrow" value="false"/>
<property name= "testOnReturn" value="false"/>
<property name= "validationQuery" value="select 1"/>
<property name= "timeBetweenEvictionRunsMillis" value="60000"/>
<property name= "numTestsPerEvictionRun" value="${maxActive}"/>
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " configLocation" value = " classpath:mybatis-config.xml" />
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " cn.wedding.dao" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< mvc: annotation-driven/>
< tx: annotation-driven transaction-manager = " transactionManager" />
</ beans>
mybatis-config.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>
< settings>
< setting name = " lazyLoadingEnabled" value = " false" />
</ settings>
< typeAliases>
< package name = " cn.wedding.pojo" />
</ typeAliases>
</ configuration>
springmvc-servlet.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"
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-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" >
< context: component-scan base-package = " cn.wedding.controller" />
< mvc: annotation-driven>
< mvc: message-converters>
< bean class = " org.springframework.http.converter.StringHttpMessageConverter" >
< property name = " supportedMediaTypes" >
< list>
< value> application/json;charset=UTF-8</ value>
</ list>
</ property>
</ bean>
< bean class = " com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" >
< property name = " supportedMediaTypes" >
< list>
< value> text/html;charset=UTF-8</ value>
< value> application/json</ value>
</ list>
</ property>
< property name = " features" >
< list>
< value> WriteDateUseDateFormat</ value>
</ list>
</ property>
</ bean>
</ mvc: message-converters>
</ mvc: annotation-driven>
< mvc: default-servlet-handler/>
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /" />
< property name = " suffix" value = " .jsp" />
</ bean>
< bean id = " multipartResolver" class = " org.springframework.web.multipart.commons.CommonsMultipartResolver" >
< property name = " maxUploadSize" value = " 500000000" />
< property name = " defaultEncoding" value = " UTF-8" />
</ 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_3_0.xsd"
id = " WebApp_ID" version = " 3.0" >
< display-name> ssm-mss</ display-name>
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:applicationContext-*.xml</ param-value>
</ context-param>
< context-param>
< param-name> log4jConfigLocation</ param-name>
< param-value> classpath:log4j2.xml</ param-value>
</ context-param>
< 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>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
< 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:springmvc-servlet.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> springDispatcherServlet</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
< listener>
< listener-class> org.apache.logging.log4j.web.Log4jServletContextListener</ listener-class>
</ listener>
< filter>
< filter-name> log4jServletFilter</ filter-name>
< filter-class> org.apache.logging.log4j.web.Log4jServletFilter</ filter-class>
</ filter>
< filter-mapping>
< filter-name> log4jServletFilter</ filter-name>
< url-pattern> /*</ url-pattern>
< dispatcher> REQUEST</ dispatcher>
< dispatcher> FORWARD</ dispatcher>
< dispatcher> INCLUDE</ dispatcher>
< dispatcher> ERROR</ dispatcher>
</ filter-mapping>
</ web-app>
log4j2.xml(spring4.3以上需要使用最新的log4j2,不支持原来的log4j):
<?xml version="1.0" encoding="UTF-8"?>
< configuration status = " DEBUG " >
< appenders>
< console name = " Console" target = " SYSTEM_OUT" >
< PatternLayout pattern = " %d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %n%msg%n" />
</ console>
< RollingFile name = " RollingFileInfo" fileName = " D:\\demo\\allOut.log"
filePattern = " D:\\demo\\$${date:yyyy-MM-dd}/allOut-%d{yyyy-MM-dd}-%i.log.gz" >
< Filters>
< ThresholdFilter level = " all" onMatch = " ACCEPT" onMismatch = " DENY" />
</ Filters>
< PatternLayout pattern = " %d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
< Policies>
< TimeBasedTriggerzingPolicy interval = " 1" />
< SizeBasedTriggeringPolicy size = " 2 MB" />
</ Policies>
< DefaultRolloverStrategy max = " 50" />
</ RollingFile>
</ appenders>
< loggers>
< logger name = " org.springframework" level = " INFO" >
</ logger>
< logger name = " org.hibernate" level = " INFO" >
</ logger>
< root level = " all" >
< appender-ref ref = " Console" />
< appender-ref ref = " RollingFileInfo" />
</ root>
</ loggers>
</ configuration>