Mybatis 增强工具 MyBatis-Plus 集成
[MyBatis-Plus] (https://baomidou.com)是一个 MyBatis (https://www.mybatis.org/mybatis-3)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。我们可以按照如下步骤集成到我们的项目中:
- 添加 mybatis-plus 的启动器依赖
| <dependency> |
| <groupId>com.baomidou</groupId> |
| <artifactId>mybatis-plus-boot-starter</artifactId> |
| <version>3.5.1</version> |
| </dependency> |
- 配置 MapperScan 注解
| @SpringBootApplication |
| @MapperScan("io.github.xxyopen.novel.dao.mapper") |
| public class NovelApplication { |
| |
| public static void main(String[] args) { |
| SpringApplication.run(NovelApplication.class, args); |
| } |
| |
| } |
- 因为我们系统涉及分页数据查询,所以我们还需要在
io.github.xxyopen.novel.core.config
包下配置 mybatis-plus 的分页插件:
| |
| |
| |
| |
| |
| |
| @Configuration |
| public class MybatisPlusConfig { |
| |
| |
| |
| |
| @Bean |
| public MybatisPlusInterceptor mybatisPlusInterceptor() { |
| MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); |
| interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); |
| return interceptor; |
| } |
| |
| } |
- 数据源配置
YAML 是 JSON 的超集,一种用于指定分层配置数据的便捷格式。本项目中我们统一使用 YAML 格式的配置文件,所以先将 resources 目录下的 application.properties 文件重命名为 application.yml,
然后在 application.yml 配置文件中加入以下数据源配置:
| spring: |
| datasource: |
| url: jdbc:mysql://localhost:3306/novel?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai |
| username: root |
| password: test123456 |
注:根据实际的数据库环境来修改相应的IP、端口号、数据库名、用户名和密码
- 为了兼容 Spring Boot 3(Spring 6),在
org.springframework.core
包下创建NestedIOException
异常类
| |
| |
| |
| |
| |
| |
| |
| |
| public class NestedIOException extends IOException { |
| |
| } |
代码生成器 Mybatis-Plus-Generator 集成
- 添加相关依赖
| <dependency> |
| <groupId>com.baomidou</groupId> |
| <artifactId>mybatis-plus-generator</artifactId> |
| <version>${mybatis-plus.version}</version> |
| <scope>test</scope> |
| </dependency> |
| <dependency> |
| <groupId>org.apache.velocity</groupId> |
| <artifactId>velocity-engine-core</artifactId> |
| <version>2.3</version> |
| <scope>test</scope> |
| </dependency> |
- 在 test/resources/templates 下面创建以下模版文件

- 在 test/java 下面创建代码生成器类
| |
| |
| |
| |
| |
| |
| public class Generator { |
| |
| private static final String USERNAME = System.getenv().get("USER"); |
| |
| |
| |
| |
| private static final String PROJECT_PATH = System.getProperty("user.dir"); |
| private static final String JAVA_PATH = "/src/main/java"; |
| private static final String RESOURCE_PATH = "/src/main/resources"; |
| private static final String BASE_PACKAGE = "io.github.xxyopen.novel"; |
| |
| |
| |
| |
| private static final String DATABASE_IP = "127.0.0.1"; |
| private static final String DATABASE_PORT = "3306"; |
| private static final String DATABASE_NAME = "novel"; |
| private static final String DATABASE_USERNAME = "root"; |
| private static final String DATABASE_PASSWORD = "test123456"; |
| |
| |
| public static void main(String[] args) { |
| |
| |
| genCode("sys_user"); |
| |
| } |
| |
| |
| |
| |
| |
| private static void genCode(String tables) { |
| |
| |
| FastAutoGenerator.create(String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai", DATABASE_IP, DATABASE_PORT, DATABASE_NAME), DATABASE_USERNAME, DATABASE_PASSWORD) |
| .globalConfig(builder -> { |
| builder.author(USERNAME) |
| .fileOverride() |
| |
| |
| .fileOverride() |
| .commentDate("yyyy/MM/dd") |
| .outputDir(PROJECT_PATH + JAVA_PATH); |
| }) |
| |
| .packageConfig(builder -> builder.parent(BASE_PACKAGE) |
| .entity("dao.entity") |
| .service("service") |
| .serviceImpl("service.impl") |
| .mapper("dao.mapper") |
| .controller("controller.front") |
| .pathInfo(Collections.singletonMap(OutputFile.mapperXml, PROJECT_PATH + RESOURCE_PATH + "/mapper"))) |
| |
| .templateConfig(builder -> builder.disable(TemplateType.SERVICE) |
| .disable(TemplateType.SERVICEIMPL) |
| .disable(TemplateType.CONTROLLER)) |
| |
| .strategyConfig(builder -> builder.addInclude(getTables(tables)) |
| .controllerBuilder() |
| .enableRestStyle() |
| .serviceBuilder() |
| .formatServiceFileName("%sService") |
| ) |
| |
| .execute(); |
| |
| } |
| |
| |
| |
| |
| protected static List<String> getTables(String tables) { |
| return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(",")); |
| } |
| |
| } |
- 修改 Generator 类中数据库相关配置,选择我们需要创建的表名(all),运行 main 方法生成代码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY