学习笔记——Mybatis逆向工程MBG;MyBatis逆向工程MBG使用步骤
2023-01-12
一、逆向工程
1、逆向工程
数据库中表影响程序中代码(表影响java对象)。
MyBatis Generator:简称MGB,是一个专门为MyBatis框架使用定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。
2、正向工程
应用程序中代码影响数据库表中数据(java对象影响表)
二、MGB简介
(1)MyBatis Generator:即MBG
(2)MBG是一个专门为MyBatis框架使用者定制的代码生成器
(3)MBG可以快速的根据表生成对应的映射文件、接口、以及bean类
(4)只可以生成单表CRUD,但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
2023-01-13
三、逆向工程的使用步骤
1、官方文档
http://mybatis.org/generator/
2、导入逆向工程的jar包
放在模块的“pom.xml”中的<dependecies>中
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency>
3、编写MBG的配置文件mbg.xml(重要几处配置)
相应的配置文件在官方文档中,可以找到
向下滑动
上述配置文件是官方给出的示例配置文件,之后将其中相关部分改为和自己模块匹配的内容
4、运行程序(代码生成器)
四、创建MBG逆向工程项目
1、创建一个maven项目,命名为“day05_mybatis_MBG”
(1)在模块的“pom.xml”中导入jar包
<dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency> </dependencies>
(2)在“day05_mybatis_MBG.src.main.resources”中创建“mybatis-config.xml”,设置“db.properties”和“log4j.xml”
(3)在“day05_mybatis_MBG.src.main.resources”中创建“mbg.xml“
(4)设置测试文件
在“day05_mubatis_MBG.src.test.java”中创建测试类“TestMBG”
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南