官网 http://mybatis.org/generator/running/running.html
文章目录
MyBatis Generator (MBG)
是MyBatis 官方开发的插件。
作用是 只要配置好数据库连接与表参数,就可以自动生成表对应的 dao层接口、pojo以及mapper xml
MyBatis Generator 生成的文件包含三类:
- Model实体文件,一个数据库表对应生成一个 Model 实体;
- Mapper接口文件,数据数操作方法都在此接口中定义;
- Mapper XML配置文件
- 配置文件说明,官网还没翻译看大佬们总结的凑合用
https://blog.csdn.net/zhangshengqiang168/article/details/90511316
通过 Maven Plugin使用
https://blog.csdn.net/weixin_42250302/article/details/106986707
通过 Maven dependency 使用
使用案例
https://gitee.com/xu_yuchun/construct-framework/tree/master/.history/v1.0
- pom.xml依赖
- resource配置
generator.properties
generatorConfig.xml - Generator启动类编写
- 运行Generator启动类生成
踩坑
1:生成的 Mapper.xml 注入Mapper接口 失败
报错 Consider defining a bean of type '... ... Mapper' in your configuration.
- 办法
- Mapper接口 上使用@Mapper
- 配置类 使用@MapperScan注解
@Configuration @MapperScan("mapper接口路径")
https://blog.csdn.net/luanchenghuakai/article/details/88416697?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
2.生成的 Mapper.xml 出现了相同的命名id
报错 java.lang.IllegalArgumentException: ... ... Mapper.BaseResultMap
-
原因
生成的mapper.xml文件信息是直接追加在原来的文件上,运行了2次生成,造成id重复2次 -
办法:添加覆盖参数
<!--生成mapper.xml时覆盖原文件-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />