mybatis-plus使用generator实现逆向工程
作者:菜鸟菜 发布时间:2022-06-05 20:16:49
标签:mybatis-plus,generator,逆向工程
1.背景
可以使用mybatis-plus-generator
逆向生成dao层、service层、controller层等代码
2.引入jar包
mybatis-plus-generator
在3.5.0以及以后的版本使用新的方式逆向生成代码。
这里介绍使用旧版本的方式生成代码。
<!-- mybatis-plus begin -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!-- mybatis-plus 默认是vm引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- mybatis-plus end -->
3.自动生成代码
public static void main(String[] args) {
// 构建一个代码生成器对象
AutoGenerator mpg = new AutoGenerator();
// 配置执行策略
// 1.全局配置
GlobalConfig gc = new GlobalConfig();
// 当前项目路径
String proPath = System.getProperty("user.dir");
// 设置代码生成路径
gc.setOutputDir(proPath + "/src/main/java");
// 生成的类的注释中作者信息
gc.setAuthor("curry");
// 生成后是否打开文件夹
gc.setOpen(false);
// 是否覆盖
gc.setFileOverride(true);
// 生成service类的后缀
gc.setServiceName("%sService");
// 主键生成策略 和数据库id生成策略一致
gc.setIdType(IdType.AUTO);
// 设置日期类型
gc.setDateType(DateType.ONLY_DATE);
// 是否生成Swagger
gc.setSwagger2(false);
// 生成entity类的后缀
gc.setEntityName("%sEntity");
mpg.setGlobalConfig(gc);
// 2.设置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test_db");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 3.配置生成包的路径
PackageConfig pc = new PackageConfig();
// 设置模块存放位置
pc.setParent("com.");
// 设置该模块包的路径
pc.setModuleName("dao");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);
// 4.策略配置
StrategyConfig strategy=new StrategyConfig();
// 设置要映射的表名 不配置默认处理全部表
// strategy.setInclude("user");
// 表名中下划线转驼峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
// 表中字段如果有下划线,转驼峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setEntityLombokModel(true);//自动生成Lombok
// strategy.setRestControllerStyle(true);//开启 RestFul 风格
// strategy.setControllerMappingHyphenStyle(true);
// 对表中的字段 设置逻辑删除 生成的dao层代码会添加@TableLogic
strategy.setLogicDeleteFieldName("delete_flag");
// 5.自动填充 (表中如果有创建时间、修改时间话,可以使用自动填充)
TableFill createTime = new TableFill("created_date", FieldFill.INSERT);
TableFill updateTime = new TableFill("modified_date", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(createTime);
tableFills.add(updateTime);
strategy.setTableFillList(tableFills);
// 乐观锁配置
// strategy.setVersionFieldName("version");
mpg.setStrategy(strategy);
// 6.配置实体类模板
TemplateConfig templateConfig = new TemplateConfig();
// 如果setXxxxx(null) 不会生成Xxxx实体类相关代码
// 因此如果只生成dao层代码
// 可以在这里控制
templateConfig.setController(null);
templateConfig.setMapper(null);
templateConfig.setService(null);
templateConfig.setServiceImpl(null);
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 7.执行代码生成操作
mpg.execute();
}
4.修改*Mapper.xml文件的生成位置
4.1 默认*Mapper.xml文件生成位置
// 3.配置生成包的路径
PackageConfig pc = new PackageConfig();
// 设置模块存放位置
pc.setParent("com.");
// 设置该模块包的路径
pc.setModuleName("dao");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);
以上面代码为例,*Mapper.xml
文件位置是mapper/xml/*Mapper.xml
4.2 修改*Mapper.xml文件生成位置
step1:在模板中控制不生成xml文件 防止重复生成
templateConfig.setXml(null);
step2:在第三步中mpg.execute();
执行前增加以下代码
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
// 这里自定义配置的是*Mapper.xml文件
// 所以templatePath = "/templates/mapper.xml.vm";
// 如果你想自定义配置其它 修改templatePath即可
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 如果你 Entity 设置了前后缀
String entityName = tableInfo.getEntityName();
int length = entityName.length();
entityName = entityName.substring(0, length - 6);
return proPath + "/src/main/resources/mapper/" +
entityName + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
来源:https://juejin.cn/post/7095284828781674509


猜你喜欢
- SpringMVC文件下载说明: 在 SpringMVC 中,通过返回 ResponseEntity的类型,可以实现文件下载的功能案例演示1
- Java super关键字super 关键字与 this 类似,this 用来表示当前类的实例,super 用来表示父类。super 可以用
- 一般来说,Android自身就包含了常用于嵌入式系统的SQLite,这样就免去了开发者自己移植安装的功夫。SQLite 支持多数SQL92标
- 本文实例讲述了C#操作注册表的方法。分享给大家供大家参考,具体如下:下面我们就来用.NET下托管语言C#注册表操作,主要内容包括:注册表项的
- 什么是显式转换Explicit Conversion就是在将一种类型转换成另外一种类型时,需要额外的代码来完成这种转换。int n = 1;
- 一、同步容器 1、Vector——>ArrayList vector 是线程(Thread)同步(Synchron
- 下面介绍的这个版本搭配是我研究好久好久才跑通的,这在我的电脑上是一组可行的配置,如果你使用了同样的配置跑不通,那可能是环境中某一部分还是有不
- Pattern类定义 public final class Pattern
- 一、前言1、热更新代码的场景(1)当线上服务器出现问题时,有些时候现有的手段不足以发现问题所在,可能需要追加打印日志或者增加一些调试代码,如
- 使用线程池的好处1、降低资源消耗可以重复利用已创建的线程降低线程创建和销毁造成的消耗。2、提高响应速度当任务到达时,任务可以不需要等到线程创
- 文件资源操作Spring 定义了一个 org.springframework.core.io.Resource 接口,Resource 接口
- 本文实例为大家分享了Android设置默认锁屏壁纸接口的具体代码,供大家参考,具体内容如下完成自定义service后,接下来就是具体实现接口
- 本文实例介绍了清除aspx页面缓存的程序实现方法,具体步骤如下:所有用到页面缓存的aspx页面修改以下cs,让它继承一个自定义基类(例如:P
- 一、Lombok从上一篇博客可看出,DAO接口类的编写变得简单,反过来看模型,编写还需要(私有属性、setter...getter...方法
- 前言 实际业务开发中,集合的判断和操作也是经常
- 最近做一个项目,因为涉及到注册,因此需要发送短信,一般发送短信都有一个倒计时的小按钮,因此,就做了一个,在此做个记录。一、发送消息没有调用公
- Android 是一款基于 Linux 内核,面向移动终端的操作系统。为适应其作为移动平台操作系统的特殊需要,谷歌对其做了特别的设计与优化,
- springcloud-gateway集成knife4j环境信息环境信息spring-boot:2.6.3spring-cloud-alib
- c#里面封装了几乎所有我们可以想到的和我们没有想到的类,流是读取文件的一般手段,那么你真的会用它读取文件中的数据了么?真的能读完全么?通常我
- 这篇文章主要介绍了Mybatis一对多关联关系映射实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,