Spring Boot (七)MyBatis代码自动生成和辅助插件
ztj100 2025-08-05 22:25 4 浏览 0 评论
一、简介
1.1 MyBatis Generator介绍
MyBatis Generator 是MyBatis 官方出品的一款,用来自动生成MyBatis的 mapper、dao、entity 的框架,让我们省去规律性最强的一部分最基础的代码编写。
1.2 MyBatis Generator使用
MyBatis Generator的使用方式有4种:
- 命令行生成
- Maven方式生成
- 使用Ant任务生成
- 使用Java代码生成
其中推荐使用Maven方式进行代码生成,因为集成和使用比较简单。
1.3 开发环境
MySQL:8.0.12
MyBatis Generator:1.3.7
Maven:4.0
IDEA:2018.2
二、代码自动生成配置
上面介绍了使用MyBatis Generator的几种方式,其中最推荐使用的是Maven方式,所以下面我们来看Maven方式的MyBatis代码生成,分为四步:
Step1:添加依赖
配置pom.xml文件,增加依赖和配置生成文件(“generatorConfig.xml”)路径:
体验AI代码助手
代码解读
复制代码
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>
mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.12</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> </dependencies> <executions> <execution> <id>Generate MyBatis Artifacts</id> <phase>package</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <!--允许移动生成的文件 --> <verbose>true</verbose> <!-- 是否覆盖 --> <overwrite>true</overwrite> <!-- 自动生成的配置 --> <configurationFile>generatorConfig.xml</configurationFile> </configuration> </plugin>
Step2:添加配置文件
根据上面在pom里的配置,我们需要添加generatorConfig.xml在项目的根目录:
体验AI代码助手
代码解读
复制代码
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "
http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--加载配置文件,为下面读取数据库信息准备--> <properties resource="application.properties"/> <!--defaultModelType="flat" 大数据字段,不分表 --> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="autoDelimitKeywords" value="true" /> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> <property name="javaFileEncoding" value="utf-8" /> <plugin type="
org.mybatis.generator.plugins.SerializablePlugin" /> <plugin type="
org.mybatis.generator.plugins.ToStringPlugin" /> <!-- 注释 --> <commentGenerator > <property name="suppressAllComments" value="true"/><!-- 是否取消注释 --> <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳--> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection driverClass="${
spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${
spring.datasource.username}" password="${
spring.datasource.password}"> </jdbcConnection> <!-- 类型转换 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model类存放位置--> <javaModelGenerator targetPackage="
com.hello.springboot.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="mapper" targetProject="
src/main/resources/mybatis" > <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator targetPackage="com.hello.springboot.dao" targetProject="src/main/java" type="XMLMAPPER" > <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table tableName="article" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"> <generatedKey column="id" sqlStatement="Mysql" identity="true" /> </table> <table tableName="user_log" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"> <generatedKey column="id" sqlStatement="Mysql" identity="true" /> </table> </context> </generatorConfiguration>
其中数据库连接的配置,是从application.properties直接读取的。
Step3:配置全局属性文件
全局属性文件application.properties的配置,和Spring Boot增加MyBatis的配置是一样的,如果你的Spring Boot项目里面已经配置了MyBatis支持,请忽略此步骤。
体验AI代码助手
代码解读
复制代码
# MyBatis 配置 spring.datasource.url=
jdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=
com.hello.springboot.mapper mybatis.config-locations=
classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
注意: MySQL 6以后JDBC的配置就不一样了,参照如上MySQL 8的配置。
Step4:点击Maven生成代码
如果你使用的是IDEA,点击最右侧的Maven Projects => 点击mybatis-generator => 右键
mybatis-generator:generate => Run Maven Build,如下图所示:
正常控制台输出“BUILD SUCCESS”说明生成已经成功了,如果出现错误,根据错误提示信息排除处理错误即可。
MyBatis Generator 示例源码:github.com/vipstone/sp…
三、安装IDEA插件
如果你使用的是 IDEA,那么强烈建议你安装一款免费的IDEA插件“Free MyBatis plugin”,可以实现dao到mapper xml对应方法的快速映射,点击任意一个快速调整到相应的方法,提高工作效率,效果如下图所示:
点击绿色的箭头直接跳转到了mapper xml对应的方法了,如下图所示:
可以相互点击,进行对应的跳转。
安装步骤
- 点击菜单栏Flie => Settings
- 点击Browse repostitories..
- 输入“Free MyBatis plugin”查找插件
- 点击安装,重启IDEA
关键步骤的截图如下:
四、总结
使用了MyBatis Generator可以帮我们自动生成实体类,和5个最基础的方法,大大的提高我们的工作效率,用户只需要按需写自己独有的一些业务即可。同时增加“Free MyBatis plugin”插件,可以很方便的帮我们开发和调试代码,真是实实在在的福利。
相关推荐
- 作为后端开发,你知道MyBatis有哪些隐藏的 “宝藏” 扩展点吗?
-
在互联网大厂后端开发领域,MyBatis作为一款主流的持久层框架,凭借其灵活的配置与强大的数据处理能力,广泛应用于各类项目之中。然而,随着业务场景日趋复杂、系统规模不断扩张,开发过程中常面临SQL...
- 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构(附源码)
-
前言zheng项目不仅仅是一个开发架构,而是努力打造一套从前端模板-基础框架-分布式架构-开源项目-持续集成-自动化部署-系统监测-无缝升级的全方位J2EE企业级开发解...
- 基于Java实现,支持在线发布API接口读取数据库,有哪些工具?
-
基于java实现,不需要编辑就能发布api接口的,有哪些工具、平台?还能一键发布、快速授权和开放提供给第三方请求调用接口的解决方案。架构方案设计:以下是一些基于Java实现的无需编辑或只需少量编辑...
- Mybatis Plus框架学习指南-第三节内容
-
自动填充字段基本概念MyBatis-Plus提供了一个便捷的自动填充功能,用于在插入或更新数据时自动填充某些字段,如创建时间、更新时间等。原理...
- 被你误删了的代码,在 IntelliJ IDEA中怎么被恢复
-
在IntelliJIDEA中一不小心将你本地代码给覆盖了,这个时候,你ctrl+z无效的时候,是不是有点小激动?我今天在用插件mybatisgenerator自动生成mapper的时候,...
- 修改 mybatis-generator 中数据库类型和 Java 类型的映射关系
-
使用mybatis-generator发现数据库类型是tinyint(4),生成model时字段类型是Byte,使用的时候有点不便数据库的类型和Model中Java类型的关系...
- 又被问到了, java 面试题:反射的实现原理及用途?
-
一、反射的实现原理反射(Reflection)是Java在运行时动态获取类的元数据(如方法、字段、构造器等)并操作类对象的能力。其核心依赖于...
- Spring Boot 中JPA和MyBatis技术那个更好?
-
你在进行SpringBoot项目开发时,是不是也经常在选择JPA和MyBatis这两个持久化技术上犯难?面对众多前辈的经验之谈,却始终拿不准哪种技术才最适合自己的项目?别担心,今天咱们就...
- Spring Boot (七)MyBatis代码自动生成和辅助插件
-
一、简介1.1MyBatisGenerator介绍MyBatisGenerator是MyBatis官方出品的一款,用来自动生成MyBatis的mapper、dao、entity的框架,让...
- 解决MyBatis Generator自动生成.java.1文件
-
MyBatis框架操作数据库,一张表对应着一个实体类、一个Mapper接口文件、一个Mapper映射文件。一个工程项目通常最少也要几十张表,那工作量可想而知非常巨大的,MyBatis框架替我们想好了解...
- Linux yq 命令使用详解
-
简介yq是一个轻量级、可移植的命令行...
- Python学不会来打我(62) json数据操作汇总
-
很多小伙伴学了很久的python一直还是没有把数据类型之间的转换搞明白,上一篇文章我们详细分享了python的列表、元组、字典、集合之间的相互转换,这一篇文章我们来分享json数据相关的操作,虽然严格...
- 之前3W买的Python全系列教程完整版(懂中文就能学会)
-
今天给大家带来了干货,Python入门教程完整版,完整版啊!完整版!言归正传,小编该给大家介绍一下这套教程了,希望每个小伙伴都沉迷学习,无法自拔...
- x-cmd pkg | grex - 正则表达式生成利器,解决手动编写的烦恼
-
简介grex是一个旨在简化创作正则表达式的复杂且繁琐任务的库和命令行程序。这个项目最初是DevonGovett编写的JavaScript工具regexgen的Rust移植。但re...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- 作为后端开发,你知道MyBatis有哪些隐藏的 “宝藏” 扩展点吗?
- 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构(附源码)
- 基于Java实现,支持在线发布API接口读取数据库,有哪些工具?
- Mybatis Plus框架学习指南-第三节内容
- 被你误删了的代码,在 IntelliJ IDEA中怎么被恢复
- 修改 mybatis-generator 中数据库类型和 Java 类型的映射关系
- 又被问到了, java 面试题:反射的实现原理及用途?
- Spring Boot 中JPA和MyBatis技术那个更好?
- Spring Boot (七)MyBatis代码自动生成和辅助插件
- 解决MyBatis Generator自动生成.java.1文件
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)