一、什么是代码生成器?

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

二、使用教程

1、创建一个项目,以SpringBoot项目为例

  • 在该项目中可以新建一个模块,用于代码生成器工具的使用(我没有新建模块)

2、添加依赖

<!--        添加代码生成器依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.4.1</version>
</dependency>
<!--        添加模板引擎依赖--><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>latest-velocity-version</version>
</dependency>

3、编写配置(该文章只使用了默认模板)

在test文件下新建CodeGenerator类,然后进行复制以下代码进行个人配置:

// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {/*** <p>* 读取控制台内容* </p>*/public static String scanner(String tip) {Scanner scanner = new Scanner(System.in);StringBuilder help = new StringBuilder();help.append("请输入" + tip + ":");System.out.println(help.toString());if (scanner.hasNext()) {String ipt = scanner.next();if (StringUtils.isNotBlank(ipt)) {return ipt;}}throw new MybatisPlusException("请输入正确的" + tip + "!");}public static void main(String[] args) {// 代码生成器AutoGenerator mpg = new AutoGenerator();// 全局配置GlobalConfig gc = new GlobalConfig();String projectPath = System.getProperty("user.dir");gc.setOutputDir(projectPath + "/src/main/java");gc.setAuthor("jobob");gc.setOpen(false);// gc.setSwagger2(true); 实体属性 Swagger2 注解mpg.setGlobalConfig(gc);// 数据源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://localhost:3306/ant?useUnicode=true&useSSL=false&characterEncoding=utf8");// dsc.setSchemaName("public");dsc.setDriverName("com.mysql.jdbc.Driver");dsc.setUsername("root");dsc.setPassword("密码");mpg.setDataSource(dsc);// 包配置PackageConfig pc = new PackageConfig();pc.setModuleName(scanner("模块名"));pc.setParent("com.baomidou.ant");mpg.setPackageInfo(pc);// 自定义配置InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}};// 如果模板引擎是 freemarkerString templatePath = "/templates/mapper.xml.ftl";// 如果模板引擎是 velocity// String templatePath = "/templates/mapper.xml.vm";// 自定义输出配置List<FileOutConfig> focList = new ArrayList<>();// 自定义配置会被优先输出focList.add(new FileOutConfig(templatePath) {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}});/*cfg.setFileCreate(new IFileCreate() {@Overridepublic boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {// 判断自定义文件夹是否需要创建checkDir("调用默认方法创建的目录,自定义目录用");if (fileType == FileType.MAPPER) {// 已经生成 mapper 文件判断存在,不想重新生成返回 falsereturn !new File(filePath).exists();}// 允许生成模板文件return true;}});*/cfg.setFileOutConfigList(focList);mpg.setCfg(cfg);// 配置模板TemplateConfig templateConfig = new TemplateConfig();// 配置自定义输出模板//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别// templateConfig.setEntity("templates/entity2.java");// templateConfig.setService();// templateConfig.setController();templateConfig.setXml(null);mpg.setTemplate(templateConfig);// 策略配置StrategyConfig strategy = new StrategyConfig();strategy.setNaming(NamingStrategy.underline_to_camel);strategy.setColumnNaming(NamingStrategy.underline_to_camel);strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");strategy.setEntityLombokModel(true);strategy.setRestControllerStyle(true);// 公共父类strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");// 写于父类中的公共字段strategy.setSuperEntityColumns("id");strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));strategy.setControllerMappingHyphenStyle(true);strategy.setTablePrefix(pc.getModuleName() + "_");mpg.setStrategy(strategy);mpg.setTemplateEngine(new FreemarkerTemplateEngine());mpg.execute();}}
  • 配置 GlobalConfig
 // 全局配置GlobalConfig gc = new GlobalConfig();String projectPath = System.getProperty("user.dir");gc.setOutputDir(projectPath + "/src/main/java");//目录如果有多个模块要加在前面gc.setAuthor("yhw");//作者名字,自己修改gc.setOpen(false);// gc.setSwagger2(true); 实体属性 Swagger2 注解mpg.setGlobalConfig(gc);
  • 配置 DataSourceConfig
// 数据源配置(需要连接自己需要的数据库)DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");// dsc.setSchemaName("public");dsc.setDriverName("com.mysql.jdbc.Driver");dsc.setUsername("root");dsc.setPassword("root");mpg.setDataSource(dsc);
  • 包配置
// 包配置PackageConfig pc = new PackageConfig();
//        pc.setModuleName(scanner("模块名"));//代表子模块,可以不写pc.setParent("com.example");//父模块/*** 需要设置自己需要生成的包名*/pc.setEntity("pojo");pc.setMapper("mapper");pc.setService("service");pc.setController("controller");mpg.setPackageInfo(pc);

三、运行结果

如何使用MyBatis-Plus中的代码生成器?相关推荐

  1. MyBatis之优化MyBatis配置文件中的配置

    MyBatis之优化MyBatis配置文件中的配置 2017/9/30 MyBatis配置文件很重要,首先我们来看看MyBatis配置文件中的内容和顺序: 文件目录结构如下: 1.<proper ...

  2. mybatis 配置文件中,collection 和 association 的对应关系

    mybatis 配置文件中,collection 和 association 的对应关系  如下图所示:

  3. ❤️Mybatis开发中什么是多对一处理、一对多处理?

    ❤️Mybatis开发中什么是多对一处理.一对多处理? 什么是多对一: 对于学生而言,关联-多个学生关联一个老师(多对一) 对于老师而言,集合-一个老师有很多学生(一对多) SQL: CREATE T ...

  4. mybatis XML 中<if>、<choose>、<when>、<otherwise>等标签的使用?多条件查询该怎么处理?

    mybatis XML 中if.choose.when.otherwise等标签的使用 一般使用在多条查询,虽然也可以通过注解写,我比较菜,我不会. 一般多条查询怎么解决? 1.如果是单表间的多条件查 ...

  5. Mybatis xml中配置一对一关系association一对多关系collection

    Mybatis xml中配置一对一关系association&一对多关系collection 今天在配置一对一关系映射以及一对多关系映射的时候,把collection中应该使用的ofType配 ...

  6. Mybatis.xml中sql语句的转译

    Mybatis.xml中sql语句的转译

  7. mybatis接口中的方法重载_MyBatis的Mapper接口以及Example的实例函数及详解

    一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 int countByExample(UserExample example) thorws SQLException ...

  8. MyBatis Generator中的新功能

    版本1.3.5 请参阅GitHub页面的里程碑1.3.5,了解本版本中发生了什么变化.里程碑1.3.5 版本1.3.4 在这个版本中,我们已经弃用了eclipse插件中的弹出菜单项,用于运行MyBat ...

  9. mybatis学习四(代码生成器MGB)

    目录 1.MyBatis Generator(MGB)简介 二.MGB核心API 三.MGB使用方式 3.1 Maven插件方式 3.2 java程序运行 1.MyBatis Generator(MG ...

  10. mybatis plus 中 EntityWrapper源码解读

    mybatis plus内置了好多CRUD,其中 EntityWrapper这个类就是. 这个类是mybatis plus帮我们写好的好多接口,就如同我们在dao层写好方法在xml中实现一样. 那么这 ...

最新文章

  1. [*开同*看] 星际情书
  2. 滴滴重磅开源跨平台统一 MVVM 框架 Chameleon
  3. 移动端h5开发总结不断更新中....
  4. 多线程:当你提交任务时,线程队列已经满了,这时会发生什么?
  5. Matlab:精度控制
  6. Codeforces Round #378 (Div. 2) D - Kostya the Sculptor
  7. 谷歌 | 多任务学习,如何挑选有效的辅助任务?只需一个公式!
  8. taz文件_我们将赠送LulzBot Taz 6 3D打印机
  9. java的实现内部类实现链表
  10. mysql自动填充_Mysql自动填充测试数据
  11. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.解决办法
  12. 虎牙tv是用php写的吗,huya虎牙php_麦麦同学
  13. VSCode安装教程详细简单版
  14. 计算机内存延迟,内存延迟有多重要?游戏性能测试说明真相:酷睿i9-9900K依然无敌...
  15. 蓝兔子现在有一个字符串,如果一个字符串从左向右看和从右向左看是一样的,则称为回文串。请编写程序,帮助蓝兔子判断输入的字符串是否是回文串。
  16. ERD Commander 2005 使用教程
  17. 汽车维修企业管理【11】
  18. win easypanel安装php,windows下kangle虚拟主机-kangleeasypanel安装图文教程以及心得
  19. Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
  20. 基础数论讲解(详细)

热门文章

  1. php 公众号多图文消息,微信公众号怎样群发多图文消息?
  2. python 三次样条_python实现三次样条插值
  3. 计算几何(基础部分)
  4. 2018 python视频教程-自学python,怎能少得了教程
  5. 邮件服务解决方案--iRedMail
  6. 卧槽!VSCode 上竟然也能约会,谈对象了???
  7. 免费使用短信服务接口 ----用Java实现
  8. 揭阳市计算机考证报名点在哪里
  9. HTML深海骑兵制作,深海迷航代码独眼巨人号护盾发生器 | 手游网游页游攻略大全...
  10. 百度色情图片识别API