这里我直接利用之前使用的 Springboot+Mybatis-Plus+Mysql 的项目,在其基础上进行升级改造,大体结构如下:
一、添加依赖
需要增加的核心依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入Log4j2的starter依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
这里我贴出我的全部依赖,仅供大家酌情参考选择:
XML
<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.example</groupId>
<artifactId>WeMall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入Log4j2的starter依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.12</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.4.1</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- Druid 数据连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<!-- mybatis plus 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.8.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、修改配置文件
application.yml
spring: profiles: active: dev
application-dev.yml
server:
port: 8081
servlet:
context-path: /
logging:
config: classpath:log4j2.xml
level:
#这个指定你的dao层,也就是mapper文件的所在包
com.xu.wemall.mapper: DEBUG
spring:
devtools:
restart:
enabled: true
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/xu_demo?serverTimezone=GMT%2B8&&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
serialization:
write-dates-as-timestamps: false
redis:
host: 127.0.0.1
port: 6379
password:
# lettuce:
pool:
#连接超时时间
timeout: 5000
jedis:
pool:
#最大连接数
max-active: 8
#最大阻塞时间
max-wait: 6000
#最大空闲数
max-idle: 8
#最小空闲数
min-idle: 0
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/*Mapper.xml
global-config:
# 逻辑删除配置
db-config:
# 删除前
logic-not-delete-value: 1
# 删除后
logic-delete-value: 0
特别说明:
1、上面我使用了多profile配置,大家直接拷贝application-dev.yml中配置到你的application.yml文件即可
2、上面数据源部分的配置和你的mapper-locations配置,大家根据自己的实际情况修改配置即可。
3、这里我为了演示,还故意临时注释了mybatis-plus中的log-impl配置。
三、创建log4j2.xml日志配置文件
1、在resouces目录下新建log4j2.xml文件
2、这里我方便演示,临时注释了将日志保存到本地文件部分的配置,大家酌情选择是否需要选择放开。
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF" monitorInterval="1800">
<Properties>
<property name="LOG_PATTERN" value="[ %date{yyyy-MM-dd HH:mm:ss.SSS} ] Hello,World [ %thread ] %-5level %logger{36} - %msg%n" />
<property name="FILE_NAME" value="admin" />
<property name="FILE_PATH" value="/data/logs/" />
</Properties>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<!-- <RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/${FILE_NAME}/${FILE_NAME}_info.log"-->
<!-- filePattern="${FILE_PATH}/${FILE_NAME}-INFO-%d{yyyy-MM-dd-HH}_%i.log.gz">-->
<!-- <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>-->
<!-- <PatternLayout pattern="${LOG_PATTERN}"/>-->
<!-- <Policies>-->
<!-- <TimeBasedTriggeringPolicy interval="1"/>-->
<!-- <SizeBasedTriggeringPolicy size="10MB"/>-->
<!-- </Policies>-->
<!-- <DefaultRolloverStrategy max="15"/>-->
<!-- </RollingFile>-->
<!-- <RollingFile name="RollingFileError" fileName="${FILE_PATH}/${FILE_NAME}/${FILE_NAME}_error.log"-->
<!-- filePattern="${FILE_PATH}/${FILE_NAME}-ERROR-%d{yyyy-MM-dd-HH}_%i.log.gz">-->
<!-- <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>-->
<!-- <PatternLayout pattern="${LOG_PATTERN}"/>-->
<!-- <Policies>-->
<!-- <TimeBasedTriggeringPolicy interval="1"/>-->
<!-- <SizeBasedTriggeringPolicy size="10MB"/>-->
<!-- </Policies>-->
<!-- <DefaultRolloverStrategy max="15"/>-->
<!-- </RollingFile>-->
</appenders>
<loggers>
<!-- 此处 定义到Mapper接口所在的包路径即可 也可指定某个文件的日志级别-->
<logger name="com.xu.wemall.mapper" level="DEBUG" additivity="false">
<AppenderRef ref="Console"/>
</logger>
<!-- 此处指定 整个的 日志级别 info -->
<root level="info">
<appender-ref ref="Console"/>
<!-- <appender-ref ref="RollingFileInfo"/>-->
<!-- <appender-ref ref="RollingFileError"/>-->
</root>
</loggers>
</configuration>
3、特别说明:<logger name="com.xu.wemall.mapper" level="DEBUG" additivity="false">中的name是你自己的dao接口包路径 。
四、启动项目验证
1、这里我随便创建了一个Controller并利用mybatis-plus对一张表进行了分页查询
java
@ApiOperation("分页查询")
@GetMapping("/getPageList")
public IPage<User> getPageList() {
Page<User> iPage = new Page<>(1, 10);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
IPage<User> userRecord = iUserService.page(iPage, queryWrapper);
userRecord.getRecords().forEach(System.out::println);
return userRecord;
}
2、启动项目,调用该接口,查看控制台可以看到日志如下:
可以看到后台的SQL语句已经成功打印出来了,而且日志中包含我们配置的"Hello,World",说明我们的配置的确生效了,今天的课程就到这里,我们下回再见。