Spring boot 打成war包
- 修改POM的打包方式,修改部分dependency的配置,排除内置Tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
-
修改application.yml,删除如图配置,这个配置在外部Tomcat中不会起效
-
修改启动类 ,继承 SpringBootServletInitializer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Nonnull;
import javax.annotation.Resource;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class ServerBffApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ServerBffApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ServerBffApplication.class);
}
}
-
war使用多环境配置,分为两种方式,第一种是打包时指定,第二种是 Tomcat配置文件指定,两种修改Tomcat的配置优先级更高
- 打包时指定(打包命令:mvn clean package -Psit),在application.yml中使用@profiles.active@,获取打包命令指定的环境(spring boot的配置文件一般通过@标签@获取值)
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<!-- 默认的,不加参数时执行这个profile -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>sit</id>
<properties>
<profiles.active>sit</profiles.active>
</properties>
</profile>
</profiles>
- Tomcat指定使用spring boot包的环境配置
修改Tomcat的配置文件:catalina.properties
添加一行配置:spring.profiles.active=<profile>
mybatis分页功能,使用pagehelper
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
application文件添加如下配置
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql