MyBatis-Plus与tkMapper代码生成器实战指南

发布时间:2026/7/30 3:33:44
MyBatis-Plus与tkMapper代码生成器实战指南 1. MyBatis-Plus与tkMapper代码生成器深度解析在Java后端开发领域持久层框架的选择直接影响着开发效率和代码质量。MyBatis-Plus作为MyBatis的增强工具与tkMapper这一代码生成神器结合能够将单表CRUD操作的开发时间缩短80%以上。我经历过从纯手写SQL到全自动生成的完整演进过程这种技术组合彻底改变了传统MyBatis的开发模式。2. 核心组件定位与技术选型2.1 MyBatis-Plus的核心价值作为MyBatis的增强工具包MPMyBatis-Plus简称在保留所有原生特性的基础上提供了内置通用MapperBaseMapper接口包含18个常用单表操作方法条件构造器QueryWrapper/LambdaQueryWrapper实现动态SQL拼装分页插件自动识别数据库方言的分页实现性能分析内置SQL执行性能分析拦截器全局配置可统一设置逻辑删除、字段填充等策略2.2 tkMapper的独特优势相比MyBatis-Plus自带的代码生成器tkMapper具有更简洁的配置单个注解即可实现复杂映射更智能的生成自动识别字段类型与关系更丰富的模板支持自定义Freemarker模板历史版本兼容完美支持MyBatis 3.x全系列技术选型建议新项目建议直接使用MyBatis-Plus 3.5.7版本老项目迁移可考虑tkMapper作为过渡方案3. 完整环境搭建与配置3.1 基础环境准备!-- pom.xml关键依赖 -- dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-boot-starter/artifactId version3.5.7/version /dependency dependency groupIdtk.mybatis/groupId artifactIdmapper-spring-boot-starter/artifactId version4.2.4/version /dependency3.2 生成器配置详解创建generator.properties配置文件# 数据库配置 jdbc.urljdbc:mysql://localhost:3306/your_db?useSSLfalse jdbc.drivercom.mysql.cj.jdbc.Driver jdbc.usernameroot jdbc.password123456 # 生成策略 package.namecom.example.demo module.namesystem table.prefixtb_3.3 代码生成核心类public class CodeGenerator { public static void main(String[] args) { AutoGenerator generator new AutoGenerator(); // 数据源配置 DataSourceConfig dataSourceConfig new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL); dataSourceConfig.setUrl(jdbc:mysql://localhost:3306/your_db); // 其他数据源配置... // 全局配置 GlobalConfig globalConfig new GlobalConfig(); globalConfig.setOutputDir(System.getProperty(user.dir) /src/main/java); globalConfig.setAuthor(YourName); // 其他全局配置... generator.setDataSource(dataSourceConfig); generator.setGlobalConfig(globalConfig); generator.execute(); } }4. 高级功能实现技巧4.1 突破分页限制方案MyBatis-Plus默认的500条分页限制可通过自定义分页插件解决Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor new MybatisPlusInterceptor(); PaginationInnerInterceptor paginationInnerInterceptor new PaginationInnerInterceptor(); paginationInnerInterceptor.setMaxLimit(1000L); // 修改为1000条 interceptor.addInnerInterceptor(paginationInnerInterceptor); return interceptor; }4.2 全SQL日志打印配置在application.yml中添加mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl4.3 防止全表删除通过配置拦截器强制要求必须有WHERE条件Bean public BlockAttackInnerInterceptor blockAttackInnerInterceptor() { return new BlockAttackInnerInterceptor(); }5. 典型问题排查指南5.1 常见错误对照表错误现象可能原因解决方案Table xxx doesnt exist表前缀配置错误检查generator.properties中的table.prefixInvalid bound statement扫描路径不正确确认MapperScan注解包路径分页参数失效未注册分页插件添加PaginationInnerInterceptorLombok注解未生效IDE未安装插件安装Lombok插件并启用注解处理5.2 性能优化建议批量操作使用saveBatch()方法复杂查询优先使用Lambda表达式字段注解使用TableField(condition SqlCondition.LIKE)优化模糊查询定期清理生成的Example类非必须不建议使用6. 企业级实践方案6.1 多数据源配置Configuration MapperScan(basePackages com.example.mapper.db1, sqlSessionTemplateRef db1SqlSessionTemplate) public class Db1DataSourceConfig { Bean ConfigurationProperties(prefix spring.datasource.db1) public DataSource db1DataSource() { return DataSourceBuilder.create().build(); } // 其他Bean配置... }6.2 自定义模板开发在resources目录下创建template/entity.java.ftlpackage ${package.Entity}; #list table.importPackages as pkg import ${pkg}; /#list Data TableName(${table.name}) public class ${entity} { #list table.fields as field TableField(${field.name}) private ${field.propertyType} ${field.propertyName}; /#list }6.3 单元测试规范SpringBootTest public class UserMapperTest { Autowired private UserMapper userMapper; Test void testSelect() { // 使用QueryWrapper构建条件 QueryWrapperUser wrapper new QueryWrapper(); wrapper.lambda().eq(User::getUsername, admin); User user userMapper.selectOne(wrapper); Assert.notNull(user, 查询结果不应为null); } }7. 版本兼容性解决方案7.1 MyBatis-Plus与JPA共存在pom.xml中指定jsqlparser版本properties jsqlparser.version4.6/jsqlparser.version /properties7.2 历史版本迁移指南从tkMapper迁移到MyBatis-Plus替换BaseMapper接口修改Wrapper条件构造方式更新分页查询逻辑逆向迁移方案保留实体类注解恢复tkMapper的通用Mapper接口调整Example查询条件在大型项目中我推荐采用渐进式迁移策略先在新模块使用MyBatis-Plus逐步替换旧代码。对于字段填充、逻辑删除等通用功能务必保持新旧实现的行为一致性。