环境准备:
IDE:idea
JDK:8
Tomcat: 8.5.47
步骤:
一、IDEA中File -> new Project
依赖包如: spring-boot,lombok, tomcat,jdbc,servlet,mybatis,maven打包工具等。
pom.xml如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.xiaodian</groupId>
<artifactId>demo</artifactId>
<version>1.0.1</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<!--打包方式,war 或jar-->
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.16.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 使用外部tomcat, 如果打jar包则需注释掉 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--连接数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>8.5.47</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>demo</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>
2) 为了支持打包为war, 入口类的修改
public class DemoApplication extends SpringBootServletInitializer
3) application.properties配置如下:
#端口名
server.port=8086
#项目名
server.servlet.context-path=/demo6
#DB 配置
spring.datasource.url=jdbc:mysql://XXX:63764/XXX?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=XXXX
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#Mybatis
mybatis.type-aliases-package=com.xiaodian.demo.bean
mybatis.mapper-locations=classpath:sqlmapper/*.xml
4)导出包
a. 导出jar包
修改pom文件的导出方式,将tomcat插件放开,然后在右边的maven面板,clean -> package。通过log可以看到输出的包名
b) 导出war包
修改pom文件的导出方式,将tomcat插件放开,然后在右边的maven面板,clean -> package。通过log可以看到输出的包名
坑:
1 maven编译时,说maven报错,通过IDEA查看日志,原来是Maven的版本过高了:由3.6.2改为3.5.3就搞定了