创建Maven父工程
<!-- FIXME ======================================================================================= 【SpringBoot】 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 当前SpringBoot版本号 -->
<version>2.5.6</version>
<!-- 【SpringBoot版本号,凡是groupId为org.springframework.boot的都会自动根据当前版本号下载对应的版本jar】-->
<!-- 如果升级版本号,请全局替换 <version>2.5.6</version> -->
</parent>
创建Jar包工程
- 自定义User类
创建SpringBoot工程
- 添加POM文件
<dependencies>
<!-- FIXME =============================================================================== SpringBoot 通用模块 -->
<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> <!-- web模块所需的jar包 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <!-- aop模块所需的jar包 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency> <!-- mail模块所需的jar包 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency> <!-- 读取配置文件 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- TODO ============================================================================================ mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.49</version>
</dependency>
<!-- FIXME =======================================================================================mybatis -->
<dependency> <!-- MyBatis Spring Boot Starter -->
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency> <!-- PageHelper Spring Boot Starter -->
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<dependency> <!-- Mapper Spring Boot Starter (通用Mapper解决单表增删改查) -->
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!-- TODO =========================================================================================thymeleaf -->
<dependency> <!-- 开启内置视图模板 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency> <!-- 去除模板HTML5校验 -->
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<!-- FIXME ========================================================================================常用工具包 -->
<dependency> <!-- fastjson -->
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
- 添加启动类
package com.lixing.demo1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @描述: 启动类
* @作者: lixing
* @日期 2021/6/4 17:25
*/
@SpringBootApplication // 复合注解
@MapperScan("com.lixing.demo1.*.mapper") // 多个路径也可以以逗号分隔
@EnableAsync // 开启异步
@EnableTransactionManagement // 开启注解事务
@EnableRedisHttpSession(redisNamespace = "warboot") // 开启SpringSession共享功能
public class Dem1Application {
public static void main(String[] args) {
SpringApplication.run(Dem1Application.class, args);
}
}
- 添加配置文件
#### application.properties
################################################################## TODO 默认加载测试环境配置文件,生产环境请改成pro
spring.profiles.active=dev
###########################################
debug=false
################################################################## 去掉DEBUG模式下CONDITIONS EVALUATION REPORT
logging.level.org.springframework.boot.autoconfigure=ERROR
###########################################
#--------------全局配置文件-----------------#
################################################################## Spring Boot默认使用LogBack日志系统
logging.config=classpath:logback-spring.xml
################################################################## Spring Boot的spring.jmx资源管理是默认打开的,而两个项目同时使用会冲突
spring.jmx.default-domain=demo1
spring.jmx.enabled=false
spring.application.name=demo1
################################################################## 内置tomcat配置,本地调试使用,以WAR方式部署将失效
server.address=0.0.0.0
server.port=8080
server.servlet.context-path=/demo1
################################################################## devtools 热部署
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java
################################################################## 静态资源路径
spring.resources.static-locations=classpath:static/,file:static/
################################################################## 时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
################################################################## 文件上传
spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=0
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
################################################################## TODO Hikari数据源连接池 [springboot2.0内置,https://github.com/brettwooldridge/HikariCP]
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.pool-name=HikariCP
#池中维护的最小空闲连接数
spring.datasource.hikari.minimum-idle=2
#池中最大连接数,包括闲置和使用中的连接
spring.datasource.hikari.maximum-pool-size=30
#自动提交从池中返回的连接
spring.datasource.hikari.auto-commit=true
#连接允许在池中闲置的最长时间
spring.datasource.hikari.idle-timeout=600000
#池中连接最长生命周期
spring.datasource.hikari.max-lifetime=1800000
#等待来自池的连接的最大毫秒数
spring.datasource.hikari.connection-timeout=30000
#spring.datasource.hikari.connection-test-query=SELECT 1
################################################################## TODO mybatis 基于 mybatis plus 的配置
## mybatis-plus配置
mybatis-plus.config-location=classpath:mybatis-config.xml
mybatis-plus.type-aliases-package=com.lixing.demo1.*.enyity
# 如果xml与Mapper在同一目录下可省略该配置(classpath标识从src/main/java|resources目录下开始查找)
mybatis-plus.mapper-locations=classpath:com/lixing/demo1/*/mapper/xml/*.xml
################################################################## TODO thymeleaf 视图模板
# 使热加载即可生效,生产请设置成true
spring.thymeleaf.cache=false
# 取消html5校验
spring.thymeleaf.mode=LEGACYHTML5
#spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
##### application-dev.yml
spring:
#数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://1.116.1.203:3306/base_administration?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false
username: lixing
password: mysql密码
#redis(单机)
redis:
host: 1.116.1.203
port: 6379
database: 0
password: redis密码
timeout: 30000
#发送邮件
mail:
protocol: smtp
host: smtp.163.com
username: lixing_xcx@163.com
password: AZALOXKWMZUX
default-encoding: UTF-8
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
- 启动