1.build.gradle
注意:手动在main/java/lib下添加javax.servlet-api-3.0.1.jar
apply plugin: 'java'
apply plugin: 'war' //用来生成war
version = '1.0' //property
// Uses JDK 8
sourceCompatibility = 1.8
targetCompatibility = 1.8
// 1. Get dependencies from Maven local repository
// 2. Get dependencies from Maven central repository
repositories {
//mavenCentral()
maven{ url"http://maven.aliyun.com/nexus/content/groups/public"}
}
//Project dependencies
dependencies {
compile 'org.apache.tomcat:tomcat-servlet-api:8.0.24'
compile 'jstl:jstl:1.2'
compile 'org.springframework:spring-beans:4.3.5.RELEASE'
compile 'org.springframework:spring-context:4.3.5.RELEASE'
compile 'org.springframework:spring-context-support:4.3.5.RELEASE'
compile 'org.springframework:spring-web:4.3.5.RELEASE'
compile 'org.springframework:spring-webmvc:4.3.5.RELEASE'
compile 'org.springframework:spring-tx:4.3.5.RELEASE'
compile 'com.alibaba:druid:1.0.15'
compile 'org.aspectj:aspectjweaver:1.8.6'
compile 'mysql:mysql-connector-java:5.1.24'
compile 'org.mybatis:mybatis-spring:1.3.1'
compile 'org.mybatis:mybatis:3.4.1'
compile 'org.springframework:spring-jdbc:4.3.5.RELEASE'
compile 'junit:junit:4.12'
compile 'org.springframework:spring-test:4.0.5.RELEASE'
compile 'com.alibaba:fastjson:1.2.31'
compile 'log4j:log4j:1.2.17'
compile group: 'org.freemarker', name: 'freemarker', version: '2.3.25-incubating'
compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.1'
compile group: 'commons-io', name: 'commons-io', version: '2.2'
compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
//include in compile only, exclude in the war
providedCompile 'javax.servlet.jsp:jsp-api:2.2.1-b03'
}
2.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">
<!-- 编码过滤器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加入Spring监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mybatis.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。
因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认
执行它实现的方法。至于ApplicationContext.xml这个配置文件部署在哪,如何配置多个xml文件,
书上都没怎么详细说明。现在的方法就是查看它的API文档。在ContextLoaderListener中关联了
ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。看看它的API说明-->
<!-- Spring MVC配置servlet-->
<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.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!--/*标记容器是否在启动的时候就加载这个servlet。
当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;
当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。*/-->
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.spring-mybatis配置
<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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.ssm">
<!-- 不扫描@Controller注解的类。spring配置文件和springmvc配置文件的两个自动扫描的路径范围,最好不要重复,
使用<context:exclude-filter/>和<context:include-filter/>指定不扫描和扫描的条件-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 导入jdbc.properties配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<!-- 先配置数据源(数据库连接池)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<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}"/>
</bean>
<!-- 配置mybatis接口代理开发
* 接口类名和映射文件必须同名
* 接口类和映射文件必须在同一个目录 下
* 映射文件namespace名字必须是接口的全类路径名
* 接口的方法名必须和映射Statement的id一致
-->
<!-- spring 和mybatis整合,不需要mybatis的配置映射文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mapping.xml文件-->
<property name="mapperLocations" value="classpath*:com/ssm/mapper/*.xml"/>
</bean>
<!-- Dao接口所在包名,Spring会自动查找其下的类 动态代理实现 不用写dao的实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 拦截器方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.springmvc配置
<?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/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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包@Controller注解的类-->
<!--使用spring组件扫描,一次性配置此包下所有的Handler-->
<context:component-scan base-package="com.ssm.controller"/>
<!-- 静态资源处理-->
<mvc:default-servlet-handler/>
<!--自动注册 RequestMappingHandlerMapping、RequestMappingHandlerAdapter,mvc的注解驱动器,通过它可以替代下边的处理器映射器和适配器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 定义跳转的文件的前后缀 ,视图模式配置(视图解析器)-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5.log4j.properties配置
### direct log messages to stdout ###
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{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file springmvc.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=${catalina.base}/logs/ssm.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=debug,stdout,file
org.apache.jasper.servlet.TldScanner.level = FINE
6.mybatis/mybatis-config.xml
<configuration>
<settings>
<!-- 使用jdbc的getGeneratedKeys 获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列别名替换列名 默认:true -->
<setting name="useColumnLabel" value="true"/>
<!-- 全局懒加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 开启驼峰命名转换:Table{create_time} 》 Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
7.jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_ssm
username=root
password=root
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000