mybatis-plus代码生成,实体类不生成父类属性

一、参考文档:

官方文档其实说的很清楚了,可能有个别地方有点不太清楚。

  mybatis-plus官方: https://mp.baomidou.com/guide/generator.html  

模版引擎用的beetl,之前没怎么接触过这块,不过感觉beetl有点像是写jsp一样,上手快。

  beetl官方: http://ibeetl.com/guide/#/beetl/

二、具体实现,说明可以看注释,更详细的配置可以看官方文档。

依赖:

       <!--代码生成-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!--代码生成使用的模版引擎-->
        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl</artifactId>
            <version>2.9.3</version>
        </dependency>

 

 

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;

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

public class Main {
    //核心类,所有操作配置都围绕该类展开。
    private static final AutoGenerator mpg = new AutoGenerator();
    //获取项目目录
    private static final String projectPath = System.getProperty("user.dir");
    //项目基础包
    private static final String Package = "com.mz.mzservice";
    //子模块包名,最终生成的是类似  com.mz.mzservice.dev 这样的
    private static final String ModuleName = "dev";

    public static void main(String[] args) {
        //数据库连接配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");
        mpg.setDataSource(dataSourceConfig);
        //公用的一些配置,一看就懂的就不加注释了。
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(projectPath + "/src/main/java");
        globalConfig.setAuthor("sun");
        //日期类型的字段使用哪个类型,默认是 java8的 日期类型,此处改为 java.util.date
        globalConfig.setDateType(DateType.ONLY_DATE);
        //是否覆盖 已存在文件,默认 false 不覆盖
        globalConfig.setFileOverride(false);
        //mapper.xml 是否生成 ResultMap,默认 false 不生成
        globalConfig.setBaseResultMap(true);
        //mapper.xml 是否生成 ColumnList,默认 false 不生成
        globalConfig.setBaseColumnList(true);
        //生成的实体类名字,增加前后缀的 ,下面的mapper xml 同理,另外还有controller和service的名称配置
//        globalConfig.setEntityName("%sEntity");
        globalConfig.setMapperName("%sMapper");
        globalConfig.setXmlName("%sMapper");
        //是否生成完成后打开资源管理器
        globalConfig.setOpen(false);
        mpg.setGlobalConfig(globalConfig);

        PackageConfig pc = new PackageConfig();
        pc.setParent(Package);
        pc.setModuleName(ModuleName);
        mpg.setPackageInfo(pc);

        StrategyConfig strategy = new StrategyConfig();
        //支持正则表达式,字符串数组。 和 Exclude 二选一
        // TODO 此处使用正则生成时,提示有些bug。
        //  被正则过滤的表会提示 “^dev_.*$” 表不存在,其实需要生成代码的表已经正常生成完毕了。
        strategy.setInclude("^dev_.*$");
        //  不需要生成的表
        //  strategy.setExclude("sequence");
        //此处配置为 下划线转驼峰命名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //生成的字段 是否添加注解,默认false
        strategy.setEntityTableFieldAnnotationEnable(true);
        //表前缀,配置后 生成的的代码都会把前缀去掉
//        strategy.setTablePrefix("dev_");
        //实体类的基础父类。没有可以不配置。
        strategy.setSuperEntityClass("com.mz.mzservice.entity.BaseEntity");
        //这里本来以为配置上 生成的实体类就没有父类的属性了,但其实不是。
        //如何去掉父类属性,下面有说明。
        strategy.setSuperEntityColumns("creater","createTime","editor","editTime","remark");
        //controller,基础父类
        strategy.setSuperControllerClass("com.mz.mzservice.controller.BaseController");
        //是否启用 Lombok
        strategy.setEntityLombokModel(true);
        //是否启用 builder 模式 例:new DevDevice().setDealerId("").setDeviceCode("");
        strategy.setEntityBuilderModel(true);
        mpg.setStrategy(strategy);

        InjectionConfig in = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<String, Object>();
                //自定义配置,在模版中cfg.superColums 获取
                // TODO 这里解决子类会生成父类属性的问题,在模版里会用到该配置
                map.put("superColums", this.getConfig().getStrategyConfig().getSuperEntityColumns());
                this.setMap(map);
            }
        };
        in.setFileOutConfigList(CollectionUtil.newArrayList(new FileOutConfig("/templates/mapper.xml.btl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义mapper xml输出目录
                return projectPath + "/src/main/resources/mapper/" + ModuleName
                        + "/" + tableInfo.getMapperName()  + StringPool.DOT_XML;
            }
        }));
        mpg.setCfg(in);
        TemplateConfig templateConfig = new TemplateConfig();
        //自定义模版
        templateConfig.setController("/templates/btl/controller.java");
        templateConfig.setEntity("/templates/btl/entity.java");
        //关闭默认的mapper xml生成
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        //使用beetl模版引擎
        mpg.setTemplateEngine(new BeetlTemplateEngine());
        mpg.execute();
    }

}

三、自定义模版,这里我只粘贴出来,子类不生成父类属性的地方。修改是基于官方模版的。

说明:大概逻辑是判断字段名是否包含在之前在代码里配置的父类字段。如果包含则直接continue。

entity.java.btl

 

 

 

 

四、官方的默认模版位置

 

posted on 2019-10-19 16:39  SunEn  阅读(8751)  评论(0编辑  收藏  举报

导航