栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot框架搭建代码快速生成,建MP代码生成器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot框架搭建代码快速生成,建MP代码生成器

介绍
当前spring boot/mybatis/mybatis-plus/JPA等框架快速提升了开发效率,但是整体框架目前还存在一些问题,比如:
1.部分框架繁琐代码;
2.繁重的配置;
3.脚手架搭建完成之后重复性工作;
针对以上问题,节约建立目录和文件的时间,减少简单的增删改查、分页重复性工作时间(增删改查、分页),特地生成了简单的代码生成工具

效果展示

spring boot项目初步生成

利用代码工具生成

解决方案



  com.baomidou
  mybatis-plus-generator
  3.2.0


 

  org.apache.velocity
  velocity-engine-core
  2.1


 

  org.freemarker
  freemarker
  2.3.29



  com.ibeetl
  beetl
  3.0.11.RELEASE

在resources目录下创建文件 application.yml

#### application.yml
spring:  
  application:    
    name: service-edu
profiles:    
  active: dev
####  application-dev.yml 
server:
  port: 8001
mybatis-plus:
   configuration:
       log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 
mapper-locations: classpath:com/MPG/service
public class CodeGenerator {

  public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mybatisPlusCodeGenerator = new AutoGenerator();

    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    
    // 先得到当前工程目录
    String projectPath = System.getProperty("user.dir");
    // 是maven项目的结构,就是工程目录 + /src/main/java
    gc.setOutputDir(projectPath + "/src/main/java");
    
    // 设置生成文件的作者信息
    gc.setAuthor("Yed");

    //当代码生成完成之后是否打开代码所在的文件夹
    gc.setOpen(true);
    gc.setFileOverride(false); //重新生成时文件是否覆盖
    gc.setSwagger2(true); 实体属性 Swagger2 注解
    gc.setServiceName("%sService");//去掉Service接口的首字母I
    
    // 将上述的全局配置注入
    mybatisPlusCodeGenerator.setGlobalConfig(gc);

    // 数据源配置
    DataSourceConfig dataSourceConfiguration = new DataSourceConfig();

    dataSourceConfiguration.setUrl("jdbc:mysql://localhost:3306/spring?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");

    // dataSourceConfiguration.setSchemaName("public");
    dataSourceConfiguration.setDriverName("com.mysql.cj.jdbc.Driver");
    dataSourceConfiguration.setUsername("root");
    dataSourceConfiguration.setPassword("123456");

    mybatisPlusCodeGenerator.setDataSource(dataSourceConfiguration);

    // 包配置
    PackageConfig pc = new PackageConfig();

    // 设置父级包名
    pc.setParent("cn.echo42");//controller entity service service.impl

    pc.setModuleName(scanner("模块名"));
    //pc.setModuleName("sys");

    // 实体类名称
    pc.setEntity("domain");

    // mapper包名称
    pc.setMapper("mapper");

    // mapper对应的映射器xml
    pc.setXml("mapper.xml");

    // 业务包层名称
    pc.setService("service");

    // 业务接口的实现类包
    pc.setServiceImpl("service.impl");

    // 控制器包名称
    pc.setController("controller");

    // 装填包信息对象
    mybatisPlusCodeGenerator.setPackageInfo(pc);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();

    //设置字段和表名的是否把下划线完成驼峰命名规则
    strategy.setNaming(NamingStrategy.underline_to_camel);

    strategy.setColumnNaming(NamingStrategy.underline_to_camel);

    //设置生成的实体类继承的父类
    //strategy.setSuperEntityClass("com.sxt.BaseEntity");

    //是否启动lombok
    strategy.setEntityLombokModel(true);

    //是否生成resetController
    strategy.setRestControllerStyle(true);

    // 公共父类
    //strategy.setSuperControllerClass("com.sxt.BaseController");

    // 写于父类中的公共字段
    //strategy.setSuperEntityColumns("person_id","person_name");

    //要设置生成哪些表 如果不设置就是生成所有的表
    //strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));

    strategy.setControllerMappingHyphenStyle(true);

    //strategy.setTablePrefix(pc.getModuleName() + "_");
    strategy.setTablePrefix("sys_");

    mybatisPlusCodeGenerator.setStrategy(strategy);

    mybatisPlusCodeGenerator.execute();
  }
}

运行上述代码:

生成结果如下:

生成的目录结构


最终结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/969667.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号