mybatis-code-generator

mybatis 代码生成器

参考官网

pom.xml 引入依赖

  • 注意 spring boot 项目spring-boot-starter-freemarker 会管理版本
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.3.2</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

    FreemarkerCodeGenerator 代码生成器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    package com.xinou.web.config.code_generator;

    import com.baomidou.mybatisplus.annotation.IdType;
    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.NamingStrategy;
    import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    import com.xinou.web.base.EntityBase;

    import java.util.ArrayList;
    import java.util.List;

    /**
    * 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
    *
    * @author lizhongyuan
    */
    public class FreemarkerCodeGenerator {

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

    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    // 输出的位置
    gc.setOutputDir(projectPath + "/web/src/main/java");
    gc.setAuthor("lizhongyuan");
    gc.setIdType(IdType.AUTO);
    gc.setOpen(false);
    // 是否生成swagger2
    gc.setSwagger2(true);
    gc.setFileOverride(true);
    mpg.setGlobalConfig(gc);

    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    // 数据连接
    dsc.setUrl("jdbc:mysql://578d7e0e5ca35.bj.cdb.myqcloud.com:3449/rongye_crm?useUnicode=true&characterEncoding=utf8&useSSL=true");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    // 数据库用户名
    dsc.setUsername("***");
    // 数据库密码
    dsc.setPassword("***");
    //自定义类型转化 不自定义 数据库的tinyint到java 会变成 boolean
    dsc.setTypeConvert(new MySqlTypeConvert());
    mpg.setDataSource(dsc);

    // 包配置
    PackageConfig pc = new PackageConfig();
    // 包的路径
    pc.setParent("com.xinou.web");
    mpg.setPackageInfo(pc);

    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {
    @Override
    public void initMap() {
    // to do nothing
    }
    };

    String templatePath = "/templates/mapper.xml.ftl";

    // 自定义输出配置
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定义配置会被优先输出
    focList.add(new FileOutConfig(templatePath) {
    @Override
    public String outputFile(TableInfo tableInfo) {
    // .xml位置
    // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
    return projectPath + "/web/src/main/resources/mappers/" +
    tableInfo.getEntityName() + "Mapping" + StringPool.DOT_XML;
    }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);

    // 配置模板
    mpg.setTemplate(new TemplateConfig().setXml(null));

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    // 实体的公共父类
    strategy.setSuperEntityClass(EntityBase.class);
    strategy.setEntityLombokModel(true);
    strategy.setChainModel(true);
    //是否生成实体时,生成字段注解
    strategy.setEntityTableFieldAnnotationEnable(true);
    // 是否生成controller
    strategy.setRestControllerStyle(true);
    // 实体基类
    strategy.setSuperEntityColumns("id", "gmt_create", "gmt_modified", "is_delete");
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    // 要忽略的表
    strategy.setExclude(
    "rel_object_content",
    "sys_app",
    "sys_carousel",
    "sys_msg_log",
    "sys_permission",
    "sys_re_role_permission",
    "sys_re_user_app",
    "sys_re_user_role",
    "sys_resources",
    "sys_role",
    "sys_user"
    );
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
    }
    }
  • 自定义转换的类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    package com.xinou.web.config.code_generator;

    import com.baomidou.mybatisplus.generator.config.GlobalConfig;
    import com.baomidou.mybatisplus.generator.config.ITypeConvert;
    import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
    import com.baomidou.mybatisplus.generator.config.rules.IColumnType;

    /**
    * @author lizhongyuan
    */
    public class MySqlTypeConvert implements ITypeConvert {

    @Override
    public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
    String t = fieldType.toLowerCase();
    if (t.contains("char")) {
    return DbColumnType.STRING;
    } else if (t.contains("bigint")) {
    return DbColumnType.LONG;
    } else if (t.contains("tinyint(1)")) {
    return DbColumnType.INTEGER;
    } else if (t.contains("int")) {
    return DbColumnType.INTEGER;
    } else if (t.contains("text")) {
    return DbColumnType.STRING;
    } else if (t.contains("bit")) {
    return DbColumnType.BOOLEAN;
    } else if (t.contains("decimal")) {
    return DbColumnType.BIG_DECIMAL;
    } else if (t.contains("clob")) {
    return DbColumnType.CLOB;
    } else if (t.contains("blob")) {
    return DbColumnType.BLOB;
    } else if (t.contains("binary")) {
    return DbColumnType.BYTE_ARRAY;
    } else if (t.contains("float")) {
    return DbColumnType.FLOAT;
    } else if (t.contains("double")) {
    return DbColumnType.DOUBLE;
    } else if (t.contains("date") || t.contains("time") || t.contains("year")){
    return DbColumnType.DATE;
    } else {
    return DbColumnType.STRING;
    }
    }
    }
作者

lizhongyuan3

发布于

2020-09-27

更新于

2022-09-05

许可协议

评论