MyBatis Plus代码生成器
2021-06-25 21:59
831 查看
一、简介
MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在。
官网文档地址:
https://mp.baomidou.com/guide/
MyBatis-Plus 特性:
https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7
二、快速代码生成
最近团队在推荐使用MyBatis-Plus,虽然一直知道他的存在,但由于自己手动挡的玩习惯了,不太喜欢用自动档的东西所以一直也没认真去看过。但为了迎合生产,在最近的项目中也听取建议上了MyBatis-Plus,在看过官方文档后感觉这玩意也挺有意思的。所以在这次的电商平台中也准备全程用MyBatis-Plus来写,关于快速代码生成在网上也看到过很多文章,但个人还是喜欢看权威的,所以在这里贴上网址,有兴趣的可以自己看下:https://mp.baomidou.com/guide/generator.html#使用教程
首先引用依赖
<!--代码生成器--> <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>2.3</version> </dependency>
//代码生成器 public class CodeGenerator { 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("ljx"); gc.setOpen(false);//是否打开 gc.setServiceImplName("%sServoce");//Server名称规苑%sAction,想看命名规范的可以点进源码看他setServiceImplName的说明 // gc.setSwagger2(true); 实体属性 Swagger2 注解 mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/ghy_goods?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("ghy"); pc.setParent("com.ghy.springcloudgoodsservice"); mpg.setPackageInfo(pc); // 自定义配置 // InjectionConfig cfg = new InjectionConfig() { // @Override // public void initMap() { // // to do nothing // } // }; // 如果模板引擎是 freemarker // String templ 564 atePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity // String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置 // List<FileOutConfig> focList = new ArrayList<>(); // // 自定义配置会被优先输出 // focList.add(new FileOutConfig(templatePath) { // @Override // public String outputFile(TableInfo tableInfo) { // // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!! // return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() // + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; // } // }); /* cfg.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { // 判断自定义文件夹是否需要创建 checkDir("调用默认方法创建的目录,自定义目录用"); if (fileType == FileType.MAPPER) { // 已经生成 mapper 文件判断存在,不想重新生成返回 false return !new File(filePath).exists(); } // ad8 允许生成模板文件 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.setEntityLomb 56c okModel(true); strategy.setRestControllerStyle(true); // 公共父类 // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!"); // 写于父类中的公共字段 strategy.setSuperEntityColumns("id"); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
这样改完成后执行就可以自动生成代码了
相关文章推荐
- SpringCloud-OAuth2(三):进阶篇
- 【Python从入门到精通】(六)Python内置的数据类型-列表(list)和元组(tuple),九浅一深,十个章节,不信你用不到
- python实战技巧之两个字典,如何实现键同则值相加【不等长或等长】
- 最好的Kubernetes客户端Java库fabric8io,快来自定义你的操作
- 【Azure Developer】使用 Azure VM 上的用户分配托管标识访问 Azure Key Vault 中国区代码示例
- 办公利器!用Python批量识别发票并录入到Excel表格
- spring @component的作用详细介绍
- SpringBoot 调用 K8s metrics-server
- Python爬虫基础讲解(二十五):scrapy 框架结构
- Qt实现基于多线程的文件传输(服务端,客户端)
- Spring Cloud Data Flow整合UAA之使用LDAP进行账号管理
- 使用 flux2+kustomize+helm+github 进行多集群 GitOps 云原生渐进式交付
- spring的拓展点之-InitializingBean
- 【Java】equals 和 == 的区别
- 【源码分析】- 在SpringBoot中你会使用REST风格处理请求吗?
- Python语言规范之Pylint的使用
- c#压缩文件
- 【解决问题】Springboot 启动报错:FactoryBean threw exception on object creation; nested exception is java.lang.UnsupportedOperationException
- 遇到禁止复制该怎么办?幸好我会Python...
- 5000张高清壁纸大图(手机用),用Python在法律的边缘又试探了一把