1、main \ resources \ datasource.properties
db.driverLocation= E:\\IntelliJ IDEA 2018.2.5\\project\\mysql-connector-java-8.0.17.jar
db.driverClassName= com.mysql.cj.jdbc.Driver
db.url= jdbc:mysql://localhost:3306/要连接的数据库名?serverTimezone= UTC
db.username= root
db.password= xxx
db.initialSize = 20
db.maxActive = 50
db.maxIdle = 20
db.minIdle = 10
db.maxWait = 10
db.defaultAutoCommit = true
db.minEvictableIdleTimeMillis = 3600000
2、main \ resources \ 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: aop= " http://www.springframework.org/schema/aop"
xmlns: tx= " http://www.springframework.org/schema/tx" xmlns: jdbc= " http://www.springframework.org/schema/jdbc"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= "
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
< context: component-scan base-package = " com.二级包名" annotation-config = " true" />
< aop: aspectj-autoproxy/>
< import resource = " applicationContext-datasource.xml" />
</ beans>
3、main \ resources \ applicationContext-datasource.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: jdbc= " http://www.springframework.org/schema/jdbc"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= "
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
< context: component-scan base-package = " com.二级包名" annotation-config = " true" />
< bean id = " propertyConfigurer"
class = " org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name = " order" value = " 2" />
< property name = " ignoreUnresolvablePlaceholders" value = " true" />
< property name = " locations" >
< list>
< value> classpath:datasource.properties</ value>
</ list>
</ property>
< property name = " fileEncoding" value = " utf-8" />
</ bean>
< bean id = " dataSource" class = " org.apache.commons.dbcp.BasicDataSource" destroy-method = " close" >
< property name = " driverClassName" value = " ${db.driverClassName}" />
< property name = " url" value = " ${db.url}" />
< property name = " username" value = " ${db.username}" />
< property name = " password" value = " ${db.password}" />
< property name = " initialSize" value = " ${db.initialSize}" />
< property name = " maxActive" value = " ${db.maxActive}" />
< property name = " maxIdle" value = " ${db.maxIdle}" />
< property name = " minIdle" value = " ${db.minIdle}" />
< property name = " maxWait" value = " ${db.maxWait}" />
< property name = " defaultAutoCommit" value = " ${db.defaultAutoCommit}" />
< property name = " minEvictableIdleTimeMillis" value = " ${db.minEvictableIdleTimeMillis}" />
< property name = " timeBetweenEvictionRunsMillis" value = " 40000" />
< property name = " testWhileIdle" value = " true" />
< property name = " validationQuery" value = " SELECT 1 FROM dual" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " mapperLocations" value = " classpath*:mappers/*Mapper.xml" > </ property>
< property name = " plugins" >
< array>
< bean class = " com.github.pagehelper.PageHelper" >
< property name = " properties" >
< value>
dialect=mysql
</ value>
</ property>
</ bean>
</ array>
</ property>
</ bean>
< bean name = " mapperScannerConfigurer" class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.二级包名.dao" />
</ bean>
< tx: annotation-driven transaction-manager = " transactionManager" proxy-target-class = " true" />
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
< property name = " rollbackOnCommitFailure" value = " true" />
</ bean>
</ beans>
4、main \ webapp \ WEB-INF \ 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> Archetype Created Web Application</ display-name>
< 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>
< listener>
< listener-class> org.springframework.web.context.request.RequestContextListener</ listener-class>
</ listener>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value>
classpath:applicationContext.xml
</ param-value>
</ context-param>
< servlet>
< servlet-name> dispatcher</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> dispatcher</ servlet-name>
< url-pattern> *.do</ url-pattern>
</ servlet-mapping>
</ web-app>
5、main \ webapp \ WEB-INF \ dispatcher-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: p= " http://www.springframework.org/schema/p"
xmlns: context= " http://www.springframework.org/schema/context"
xmlns: mvc= " http://www.springframework.org/schema/mvc" xmlns: aop= " http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< context: component-scan base-package = " com.二级包名" annotation-config = " true" />
< mvc: annotation-driven>
< mvc: message-converters>
< bean class = " org.springframework.http.converter.StringHttpMessageConverter" >
< property name = " supportedMediaTypes" >
< list>
< value> text/plain;charset=UTF-8</ value>
< value> text/html;charset=UTF-8</ value>
</ list>
</ property>
</ bean>
< bean class = " org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
< property name = " supportedMediaTypes" >
< list>
< value> application/json;charset=UTF-8</ value>
</ list>
</ property>
</ bean>
</ mvc: message-converters>
</ mvc: annotation-driven>
< bean id = " multipartResolver" class = " org.springframework.web.multipart.commons.CommonsMultipartResolver" >
< property name = " maxUploadSize" value = " 10485760" />
< property name = " maxInMemorySize" value = " 4096" />
< property name = " defaultEncoding" value = " UTF-8" > </ property>
</ bean>
</ beans>