AutoCodeGenerator

package com.wpg.meter.api.utils;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.HashMap;
import java.util.Map;

/**

  • @ Author :zx.

  • @ Date :2019/8/23 17:15

  • @ Description:代码自动生成
    */
    public class AutoCodeGenerator {

    public static void main(String[] args) throws InterruptedException {

     InjectionConfig injectionConfig = new InjectionConfig() {
         //自定义属性注入:abc
         //在.ftl(或者是.vm)模板中,通过${cfg.abc}获取属性
         @Override
         public void initMap() {
             Map<String, Object> map = new HashMap<>();
             map.put("model", "com.wpg.meter.common.utils");
             this.setMap(map);
         }
     };
    
     AutoGenerator mpg = new AutoGenerator();
     mpg.setCfg(injectionConfig);
    
     // 全局配置
     GlobalConfig gc = new GlobalConfig();
     gc.setOutputDir("E://mybatis/");
     gc.setFileOverride(true);//是否覆盖原文件
     gc.setActiveRecord(true);//开启 activeRecord 模式
     gc.setEnableCache(false);// XML 二级缓存
     gc.setBaseResultMap(true);// XML ResultMap
     gc.setBaseColumnList(true);// XML columList
     gc.setAuthor("auto");
    
     // 自定义文件命名,注意 %s 会自动填充表实体属性!
     gc.setMapperName("%sMapper");
     gc.setXmlName("%sMapper");
     gc.setServiceName("%sService");
    

// gc.setServiceImplName("%sServiceImpl");
// gc.setServiceImplName("%sService");
gc.setControllerName("%sController");
// gc.setSwagger2(true);
mpg.setGlobalConfig(gc);

    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDbType(DbType.MYSQL);
    /*dsc.setTypeConvert(new MySqlTypeConvert(){
        // 自定义数据库表字段类型转换【可选】
        @Override
        public DbColumnType processTypeConvert(String fieldType) {
            System.out.println("转换类型:" + fieldType);
            return super.processTypeConvert(fieldType);
        }
    });*/
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUrl("jdbc:mysql://122.112.224.61:3306/wpg_meter?serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8");
    dsc.setUsername("wpg");
    dsc.setPassword("Wpg$2019");
    mpg.setDataSource(dsc);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
    // strategy.setTablePrefix(new String[] { "tlog_", "tsys_" });// 此处可以修改为您的表前缀
    strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
    //strategy.setInclude("t_pipelinepoints"); // 需要生成的表
    // strategy.setExclude(new String[]{"test"}); // 排除生成的表
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);//设置control层注解为RestControl
    mpg.setStrategy(strategy);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.wpg.meter.api");
    pc.setController("controller");
    pc.setService("service");
    pc.setEntity("entity");
    pc.setMapper("mapper");
    mpg.setPackageInfo(pc);

    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();

    // 配置自定义输出模板
    //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
    // templateConfig.setEntity("templates/entity2.java");
    // templateConfig.setService();
    // templateConfig.setController();
    mpg.setTemplate(templateConfig);

    // 执行生成
    mpg.execute();
}

}

package

${package.Controller};

import p a c k a g e . S e r v i c e . {package.Service}. package.Service.{table.serviceName};
import p a c k a g e . E n t i t y . {package.Entity}. package.Entity.{entity};
import com.baomidou.mybatisplus.plugins.Page;
import KaTeX parse error: Expected 'EOF', got '#' at position 269: …otation.*; #̲if({superControllerClassPackage})
import ${superControllerClassPackage};
#end

import static ${cfg.model}.RestResult.*;

/**

  • $!{table.comment} 前端控制器
  • @author ${author}
  • @since KaTeX parse error: Expected 'EOF', got '#' at position 11: {date} */ #̲if({restControllerStyle})
    @RestController
    #else
    @Controller
    #end
    @RequestMapping("/admin#if( p a c k a g e . M o d u l e N a m e ) / {package.ModuleName})/ package.ModuleName)/{package.ModuleName}#end/#if( c o n t r o l l e r M a p p i n g H y p h e n S t y l e ) {controllerMappingHyphenStyle}) controllerMappingHyphenStyle){controllerMappingHyphen}#elseKaTeX parse error: Expected 'EOF', got '#' at position 19: …ble.entityPath}#̲end") #if({kotlin})
    class KaTeX parse error: Expected 'EOF', got '#' at position 23: …controllerName}#̲if({superControllerClass}) : ${superControllerClass}()#end

#else
#if(${superControllerClass})
public class ${table.controllerName} extends KaTeX parse error: Expected '}', got '#' at position 30: …erClass} { #̲else @Api(d…!{table.comment}")
public class ${table.controllerName} {
#end

@Autowired
private ${table.serviceName} ${table.entityPath}Service;

@PostMapping("/select e n t i t y P a g e " ) @ A p i O p e r a t i o n ( " 分 页 查 询 " ) @ A p i I m p l i c i t P a r a m ( n a m e = " A u t h o r i z a t i o n " , v a l u e = " t o k e n " , r e q u i r e d = t r u e , d a t a T y p e = " s t r i n g " , p a r a m T y p e = " h e a d e r " ) p u b l i c R e s t R e s u l t < P a g e < {entity}Page") @ApiOperation("分页查询") @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "string", paramType = "header") public RestResult<Page< entityPage")@ApiOperation("")@ApiImplicitParam(name="Authorization",value="token",required=true,dataType="string",paramType="header")publicRestResult<Page<{entity}>>select e n t i t y P a g e ( @ R e q u e s t B o d y P a g e < {entity}Page(@RequestBody Page< entityPage(@RequestBodyPage<{entity}> entity){
return t a b l e . e n t i t y P a t h S e r v i c e . s e l e c t {table.entityPath}Service.select table.entityPathService.select{entity}Page(entity);
}

@GetMapping("/select e n t i t y B y I d / i d " ) @ A p i O p e r a t i o n ( " 通 过 i d 查 询 " ) @ A p i I m p l i c i t P a r a m ( n a m e = " A u t h o r i z a t i o n " , v a l u e = " t o k e n " , r e q u i r e d = t r u e , d a t a T y p e = " s t r i n g " , p a r a m T y p e = " h e a d e r " ) p u b l i c R e s t R e s u l t < {entity}ById/{id}") @ApiOperation("通过id查询") @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "string", paramType = "header") public RestResult< entityById/id")@ApiOperation("id")@ApiImplicitParam(name="Authorization",value="token",required=true,dataType="string",paramType="header")publicRestResult<{entity}> selectKaTeX parse error: Expected '}', got 'EOF' at end of input: …return success({table.entityPath}Service.select${entity}ById(id));
}

@PostMapping("/add e n t i t y " ) @ A p i O p e r a t i o n ( " 新 增 " ) @ A p i I m p l i c i t P a r a m ( n a m e = " A u t h o r i z a t i o n " , v a l u e = " t o k e n " , r e q u i r e d = t r u e , d a t a T y p e = " s t r i n g " , p a r a m T y p e = " h e a d e r " ) p u b l i c R e s t R e s u l t < V o i d > a d d {entity}") @ApiOperation("新增") @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "string", paramType = "header") public RestResult<Void> add entity")@ApiOperation("")@ApiImplicitParam(name="Authorization",value="token",required=true,dataType="string",paramType="header")publicRestResult<Void>add{entity}(@RequestBody ${entity} entity){
return ${table.entityPath}Service.add(entity);
}

@PostMapping("/update${entity}")
@ApiOperation(“修改”)
@ApiImplicitParam(name = “Authorization”, value = “token”, required = true, dataType = “string”, paramType = “header”)
public RestResult update(@RequestBody ${entity} entity){
return ${table.entityPath}Service.update(entity);
}

    }

#end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值