如何屏蔽MyBatis-Plus启动时的Banner显示
在日常开发中,我们经常使用MyBatis-Plus作为ORM框架来提升开发效率。但是在应用启动时,MyBatis-Plus会显示一个ASCII艺术字样的Banner:
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.5.2
虽然这个Banner很有特色,但在某些场景下(如生产环境、日志精简等)我们可能希望禁用它。本文将详细介绍几种屏蔽MyBatis-Plus Banner的方法。
为什么要屏蔽Banner?
- 生产环境要求:生产环境通常需要简洁的日志输出
- 日志美观:避免Banner影响日志的可读性
- 安全考虑:减少暴露框架版本信息
- 性能优化:略微减少启动时的输出处理
方法一:通过配置文件禁用(推荐)
这是最简单也是最常用的方法,只需要在配置文件中添加一行配置即可。
YAML格式配置
# application.yaml
# MyBatis-Plus配置
mybatis-plus:
global-config:
banner: false # 禁用MyBatis-Plus的banner
# 其他常用配置(可选)
configuration:
map-underscore-to-camel-case: true # 驼峰命名转换
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl # 日志实现
Properties格式配置
# application.properties
# 禁用MyBatis-Plus banner
mybatis-plus.global-config.banner=false
# 其他配置
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
方法二:通过配置类禁用
如果你更喜欢通过Java配置类的方式进行配置,可以使用以下方式:
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* MyBatis-Plus配置类
* 通过编程方式禁用banner
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
// 设置banner为false
globalConfig.setBanner(false);
// 可选:其他全局配置
GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
dbConfig.setLogicDeleteField("isDeleted"); // 逻辑删除字段
globalConfig.setDbConfig(dbConfig);
return globalConfig;
}
}
方法三:同时禁用Spring Boot和MyBatis-Plus的Banner
如果你希望同时禁用Spring Boot自身的Banner和MyBatis-Plus的Banner,可以使用组合配置:
配置文件方式
# application.yaml
# 禁用Spring Boot banner
spring:
main:
banner-mode: off
# 禁用MyBatis-Plus banner
mybatis-plus:
global-config:
banner: false
编程方式
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// 创建Spring应用实例
SpringApplication app = new SpringApplication(Application.class);
// 禁用Spring Boot banner
app.setBannerMode(Banner.Mode.OFF);
// 启动应用
app.run(args);
}
}
方法四:通过启动参数临时禁用
如果你只是在某些特定情况下需要禁用Banner,可以通过启动参数来实现:
命令行启动
# 禁用MyBatis-Plus banner
java -jar your-app.jar --mybatis-plus.global-config.banner=false
# 同时禁用Spring Boot banner
java -jar your-app.jar --spring.main.banner-mode=off --mybatis-plus.global-config.banner=false
IDE配置
在IntelliJ IDEA或Eclipse中,可以在启动配置的VM参数中添加:
-Dmybatis-plus.global-config.banner=false
-Dspring.main.banner-mode=off
方法五:完整配置示例
以下是一个完整的MyBatis-Plus配置示例,包含了常用的配置项:
# application.yaml
# Spring配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
# 禁用Spring Boot banner(可选)
main:
banner-mode: off
# MyBatis-Plus配置
mybatis-plus:
# 全局配置
global-config:
banner: false # 禁用banner
db-config:
id-type: auto # 主键策略
logic-delete-field: isDeleted # 逻辑删除字段名
logic-delete-value: 1 # 逻辑已删除值
logic-not-delete-value: 0 # 逻辑未删除值
table-underline: true # 表名和下划线转换
# 配置信息
configuration:
map-underscore-to-camel-case: true # 驼峰命名转换
cache-enabled: true # 开启缓存
lazy-loading-enabled: true # 开启延迟加载
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 日志实现
# mapper文件位置
mapper-locations: classpath*:/mapper/**/*.xml
# 类型别名包
type-aliases-package: com.example.entity
验证配置是否生效
配置完成后,启动应用时可以通过观察日志来验证配置是否生效:
配置前(有Banner):
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.5.2
2023-12-01 10:00:00.000 INFO [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080
配置后(无Banner):
2023-12-01 10:00:00.000 INFO [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080
常见问题解答
Q: 配置后Banner仍然显示?
A: 检查以下几点:
- 配置文件名称和位置是否正确(application.yaml或application.properties)
- 配置项拼写是否正确(mybatis-plus.global-config.banner)
- 配置是否被其他配置覆盖
Q: 是否可以在测试环境启用,生产环境禁用?
A: 可以,使用Spring Profile来实现环境区分:
# application-prod.yaml(生产环境)
mybatis-plus:
global-config:
banner: false
# application-dev.yaml(开发环境)
mybatis-plus:
global-config:
banner: true
Q: 禁用Banner会影响MyBatis-Plus的功能吗?
A: 完全不会。禁用Banner只是关闭了启动时的视觉显示,不会影响任何功能特性。
Q: 如何自定义MyBatis-Plus的Banner?
A: MyBatis-Plus目前不支持自定义Banner,只能选择启用或禁用。
总结
通过本文介绍的几种方法,你可以轻松地控制MyBatis-Plus启动时Banner的显示。推荐使用方法一(配置文件方式),因为它简单、直观且易于维护。根据你的具体需求选择合适的方法,让应用启动日志更加整洁和专业。
最佳实践建议:
- 开发环境可以保留Banner便于识别框架版本
- 生产环境建议禁用Banner以保持日志简洁
- 使用配置中心管理不同环境的配置差异