MyBatis-Plus Generator代码生成器

2024-01-09 12:34:58

1.加入mybatis-plus相关依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.1</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

2.加入mysql数据库连接的驱动

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

3.交互式生成

GlobalConfig config = GeneratorBuilder.globalConfigBuilder()
                .author("******") // 作者
                .outputDir("F:\\java") // 生成路径
                .fileOverride()  // 文件覆盖
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd")
                .enableSwagger()
                .build();//生成基本的SQL片段

//mysql数据源配置
DataSourceConfig dsc =  new DataSourceConfig.Builder("jdbc:mysql://***.***.***.***:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&allowMultiQueries=true", "***", "***")
        .dbQuery(new MySqlQuery()).build();

// 策略配置
StrategyConfig strategy = GeneratorBuilder.strategyConfigBuilder()
        .addInclude("t_user")    //表名
        .addTablePrefix("t_")    //表前缀
        //Entity配置
        .entityBuilder()
        .naming(NamingStrategy.underline_to_camel)
        .columnNaming(NamingStrategy.underline_to_camel)
        .versionColumnName("version")
        .logicDeleteColumnName("isDelete")
        .enableLombok()//开启 lombok 模型,默认值:false
        .enableChainModel()//开启链式模型,默认值:false
        .enableRemoveIsPrefix()//开启 Boolean 类型字段移除 is 前缀,默认值:false
        .enableTableFieldAnnotation()//开启生成实体时生成字段注解,默认值:false
        .enableActiveRecord()//开启 ActiveRecord 模型,默认值:false
        .idType(IdType.AUTO)//全局主键类型
        //.superClass(BaseEntity.class)
        .addSuperEntityColumns("create_by","create_time","update_by","update_time","remark")
        //Controller配置
        .controllerBuilder()
        .enableHyphenStyle()
        .enableRestStyle()
        .formatFileName("%sController")
        //.superClass(BaseController.class)
        //Service配置
        .serviceBuilder()
        .formatServiceFileName("%sService")
        .formatServiceImplFileName("%sServiceImpl")
        //Mapper配置
        .mapperBuilder()//mapper 策略配置
        .enableBaseResultMap()//启用 BaseResultMap 生成,默认值:false
        .enableBaseColumnList()//启用 BaseColumnList,默认值:false
        .formatMapperFileName("%sMapper")//转换 mapper 接口文件名称后缀,mapper目录下的,例如:UserLoginMapper(有@Mapper)
        .formatXmlFileName("%sMapper")
        .build();

String basePackage = "com.sf.uimp.report";   //包名

// 包名策略配置
PackageConfig pkConfig = GeneratorBuilder.packageConfigBuilder().parent(basePackage)
        .mapper("dao")
        .service("service")
        .controller("controller")
        .entity("model")
        .xml("mapper")
        //.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "src/main/resources/mapper"))
        .build();

TemplateConfig templateConfig = GeneratorBuilder.templateConfigBuilder().build();

// 代码生成器
AutoGenerator mpg = new AutoGenerator(dsc).global(config).strategy(strategy).template(templateConfig).packageInfo(pkConfig);
mpg.execute();

4.连接clickhouse自动生成代码

<dependency>
    <groupId>ru.yandex.clickhouse</groupId>
    <artifactId>clickhouse-jdbc</artifactId>
    <version>0.3.2</version>
</dependency>
//clickhouse数据源配置
DataSourceConfig dsc =  new DataSourceConfig.Builder("jdbc:clickhouse://***.***.***.***:8123/database", "***", "***")
        .dbQuery(new ClickHouseQuery())
        .typeConvert(new ClickHouseTypeConvert(){
            @Override
            public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                if(fieldType.toLowerCase().contains("int")){
                    return DbColumnType.LONG;
                } else {
                    return super.processTypeConvert(globalConfig, fieldType);
                }
            }
        })
        .build();

文章来源:https://blog.csdn.net/I_Am_Your_God52/article/details/135476032
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。