1.项目结构 使用apache-tomcat-8.0.48版本(7.0版本会报空指针)
2.pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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.toren</groupId>
<artifactId>lock</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>lock</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 其次spring boot的web项目中内嵌tomcat服务器,所以如果我们想要发布war包到tomcat项目,要讲spring
boot中内嵌的tomcat包依赖排除,不然产生冲突,打开下面代码中的注释即可。 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<!-- druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!--配置servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--配置jsp jstl的支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--添加对tomcat的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope><!-- 打war包时加入此项, 告诉spring-boot tomcat相关jar包用外部的,不要打进去 -->
</dependency>
<!--对jsp的支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.1 首先要修改 package 方式<packaging>war</packaging>
2.2 对 spring-boot-starter-web 排除自带的tomcat包,使用Apache tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 其次spring boot的web项目中内嵌tomcat服务器,所以如果我们想要发布war包到tomcat项目,要讲spring
boot中内嵌的tomcat包依赖排除,不然产生冲突,打开下面代码中的注释即可。 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
2.3 添加servlet
<!--配置servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
3 application.yml文件 (无关精要)mybatis:
mapper-locations: "classpath:/lock/mapper/*.xml"
public-key: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJJa+f8Yuae4fXOrJGYT52OXrcAp0GgXA+mf1pDx3v4Y+GuzO3PSd6ykCrqb14mG7ICGVNBSzY/gOFOCl3pbyeUCAwEAAQ==
spring:
datasource:
druid:
driver-class-name: com.mysql.jdbc.Driver
url: 'jdbc:mysql://172.16.1.98:3306/lock?useUnicode=true&characterEncoding=UTF-8'
username: dev
password: Xyzs541GMinAb0UNKWEY4Cc2NOOxxEykzu4yc5IDgB7M54Fk85U2pu8V9pQXpogH3+mlI1EUTL0sJsXNCjM8Lg==
validation-query: SELECT 1
initial-size: 3
max-active: 10
min-idle: 0
max-wait: 60000
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
remove-abandoned: true
remove-abandoned-timeout: 300
log-abandoned: false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
filters: slf4j,wall,config
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;config.decrypt.key=${public-key}
useGlobalDataSourceStat: true
web-stat-filter.enabled: true
web-stat-filter.url-pattern: "/*"
web-stat-filter.exclusions: '*.jsp,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
web-stat-filter.session-stat-enable: true
web-stat-filter.session-stat-max-count: 100
web-stat-filter.principal-session-name: username
web-stat-filter.principal-cookie-name: false
web-stat-filter.profile-enable: true
stat-view-servlet.enabled: true
stat-view-servlet.url-pattern: '/druid/*'
stat-view-servlet.reset-enable: true
stat-view-servlet.login-username: admin
stat-view-servlet.login-password: 123
aop-patterns: 'com.example.springbootdemo.mapperdao.*,com.example.springbootdemo.service.*'
mvc:
view:
prefix: /jsp/
suffix: .jsp
aop:
proxy-target-class: true
redis:
host: localhost
port: 6379
database: 0
password: null
timeout: 0
pool:
max-idle: 0
max-active: 8
max-wait: -1
min-idle: 8
security:
basic:
enabled: false
logging:
# path: logs
level.lock.dao: debug
level.lock.controller: debug
4.启动类 LockApplication.java
package lock;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
@MapperScan(basePackages = "lock.dao")
@EnableTransactionManagement
public class LockApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(LockApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(LockApplication.class);
}
}
4.1 LockApplication类继承 SpringBootServletInitializer类并重写protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) 方法
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(LockApplication.class);
}