mybatis-plus 代码生成器

1.引入pom

   <!--代码生成器依赖 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.2</version>
        </dependency>


        <!-- velocity模板引擎 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
  1. 有时候,再springboot项目里面会有application , 它会找不到主类 , 这个时候,我们需要一个插件指定一下谁是主类
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.jp.generate</mainClass>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

最后运行,成功了,我这个是springblade项目,有很多的依赖,可能我说的不全,可能对不上。

public class MyBatisPlusGenerator {

    public static void main(String[] args) {
        String module = "ywtg";
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();

        gc.setOutputDir("D:\\work\\respository\\yiwang\\ywtg-service\\blade-ywtg\\ywtg-service\\ywtg-fep\\src\\main\\java");//这里写你自己的java目录

        gc.setFileOverride(true);//是否覆盖
        gc.setActiveRecord(false);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setAuthor("w");

        // 自定义文件命名,注意 %s 会自动填充表实体属性!
        gc.setMapperName("%sDao");
        gc.setXmlName("%sDao");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert() {

        });
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");//com.mysql.jdbc.Driver
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setUrl("jdbc:mysql://10.4.10.190:3306/ywtg_user?characterEncoding=utf-8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=GMT%2B8");
        mpg.setDataSource(dsc);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
          //strategy.setTablePrefix(new String[]{"pf_"});// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 名称生成策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);

        strategy.setInclude(new String[] {"bridge_info","ai_event_waterlogging"
                ,"bright_total","health_code_info","health_code_statistics"
                ,"overrun_vehicle_total","waterlog_postion"}); // 需要生成的表// 需要生成的表
       // strategy.setInclude(new String[] { "t_district","t_correct_population_record"}); // 需要生成的表

        // 排除生成的表
        /*strategy.setExclude(new String[]{"qrtz_blob_triggers","qrtz_calendars","qrtz_cron_triggers",
        		                         "qrtz_fired_triggers","qrtz_job_details","qrtz_locks",
        		                         "qrtz_paused_trigger_grps","qrtz_scheduler_state","qrtz_simple_triggers",
        		                         "qrtz_simprop_triggers","qrtz_triggers"}); */
        // 自定义实体父类
     	// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
     	// 自定义实体,公共字段
     	// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
     	// 自定义 mapper 父类
     	// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
     	// 自定义 service 父类
     	// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
     	// 自定义 service 实现类父类
     	// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
     	// 自定义 controller 父类
     	//strategy.setSuperControllerClass("com.jpzh.zhss.common.base.BaseController");
        // 生成 RestController 风格
     	strategy.setRestControllerStyle(true);
        strategy.setEntityLombokModel(true);

        mpg.setStrategy(strategy);

        // 包配置
        // 注意不同的模块生成时要修改对应模块包名
        PackageConfig pc = new PackageConfig();
        pc.setParent(null);
        pc.setEntity(String.format("com.jp.%s.enums",module));
        pc.setMapper(String.format("com.jp.%s.mapper",module));
        pc.setXml(String.format("com.jp.%s.mapper",module));
        pc.setService(String.format("com.jp.%s.service",module));
        pc.setServiceImpl(String.format("com.jp.%s.service.impl",module));
        pc.setController(String.format("com.jp.%s.controller",module));
        mpg.setPackageInfo(pc);

        // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };
        mpg.setCfg(cfg);

        // 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改,
        // 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也可以自定义模板名称
        TemplateConfig tc = new TemplateConfig();
        tc.setController("/templates/controller.java.vm");
        tc.setEntity("/templates/entity.java.vm");
        tc.setMapper("/templates/mapper.java.vm");
        tc.setXml("/templates/mapper.xml.vm");
        tc.setService("/templates/service.java.vm");
        tc.setServiceImpl("/templates/serviceImpl.java.vm");
        // 如上任何一个模块如果设置 空 OR Null 将不生成该模块。
        mpg.setTemplate(tc);

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

        // 打印注入设置
        System.err.println(mpg.getCfg().getMap().get("abc"));
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值