SpringBoot整合MyBatis-Plus完整配置指南 📅 2026/7/22 3:37:25 1. SpringBoot整合MyBatis-Plus完整方案解析作为Java开发者最常用的ORM框架组合SpringBoot与MyBatis-Plus的整合能显著提升持久层开发效率。最近在团队项目中升级到SpringBoot 3.1.5 MyBatis-Plus 3.5.3.1时发现官方文档的配置示例存在版本适配问题这里分享经过生产验证的完整配置方案。关键提示MyBatis-Plus从3.5.13版本开始新增了对SpringBoot 4.x的支持但当前主流项目仍以SpringBoot 2.x/3.x为主需特别注意starter依赖的差异。2. 环境准备与依赖配置2.1 基础环境要求JDK 17SpringBoot 3.x最低要求Maven 3.6.3 或 Gradle 7.xIDE支持IntelliJ IDEA推荐MySQL 8.0 或其它兼容数据库2.2 依赖管理关键点在pom.xml中需要配置的核心依赖!-- SpringBoot父POM定义 -- parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.1.5/version relativePath/ /parent dependencies !-- 核心starter -- dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-spring-boot3-starter/artifactId version3.5.3.1/version /dependency !-- 数据库驱动 -- dependency groupIdcom.mysql/groupId artifactIdmysql-connector-j/artifactId scoperuntime/scope /dependency !-- 辅助工具 -- dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency /dependencies版本匹配对照表SpringBoot版本MyBatis-Plus Starter备注2.xmybatis-plus-boot-starter经典版本支持3.xmybatis-plus-spring-boot3-starter需JDK174.xmybatis-plus-spring-boot4-starter3.5.13版本支持3. 核心配置详解3.1 基础数据源配置application.yml典型配置示例spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/demo?useUnicodetruecharacterEncodingutf8serverTimezoneAsia/Shanghai username: root password: 123456 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # SQL日志打印 map-underscore-to-camel-case: true # 自动驼峰转换 global-config: db-config: id-type: auto # 主键策略 logic-delete-field: deleted # 逻辑删除字段 logic-not-delete-value: 0 logic-delete-value: 13.2 启动类配置需添加MapperScan注解指定Mapper接口路径SpringBootApplication MapperScan(com.example.mapper) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }4. 常见问题解决方案4.1 依赖冲突处理当出现类似Failed to configure a DataSource错误时可按以下步骤排查执行mvn dependency:tree查看依赖树排除冲突的依赖exclusions exclusion groupIdcom.alibaba/groupId artifactIddruid/artifactId /exclusion /exclusions4.2 分页插件配置需单独配置分页插件Configuration public class MybatisPlusConfig { Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }5. 高级功能集成5.1 自动填充功能实现定义元对象处理器Component public class MyMetaObjectHandler implements MetaObjectHandler { Override public void insertFill(MetaObject metaObject) { this.strictInsertFill(metaObject, createTime, LocalDateTime.class, LocalDateTime.now()); } Override public void updateFill(MetaObject metaObject) { this.strictUpdateFill(metaObject, updateTime, LocalDateTime.class, LocalDateTime.now()); } }实体类注解配置TableField(fill FieldFill.INSERT) private LocalDateTime createTime; TableField(fill FieldFill.INSERT_UPDATE) private LocalDateTime updateTime;5.2 多数据源配置采用dynamic-datasource-spring-boot-starterdependency groupIdcom.baomidou/groupId artifactIddynamic-datasource-spring-boot-starter/artifactId version3.6.1/version /dependency配置示例spring: datasource: dynamic: primary: master datasource: master: url: jdbc:mysql://localhost:3306/master username: root password: 123456 slave: url: jdbc:mysql://localhost:3306/slave username: root password: 1234566. 性能优化建议启用二级缓存Configuration EnableCaching public class CacheConfig { Bean public MybatisRedisCache mybatisRedisCache() { return new MybatisRedisCache(); } }批量操作优化// 批量插入 ListUser userList new ArrayList(); userMapper.insertBatchSomeColumn(userList); // 批量更新 userMapper.updateBatchById(userList);SQL性能分析插件Bean public PerformanceInterceptor performanceInterceptor() { PerformanceInterceptor interceptor new PerformanceInterceptor(); interceptor.setMaxTime(1000); // SQL执行最大时长(ms) interceptor.setFormat(true); // 格式化SQL return interceptor; }在实际项目中使用这套配置方案时建议先在小规模测试环境验证稳定性。特别是分页插件和多数据源配置不同数据库类型需要调整对应的Dialect参数。对于高并发场景还需要结合连接池配置进行针对性优化。