MyBatis-flex自动生成代码类

2023-08-08
8375
0
1

温馨提示:本文最后更新于 2023-08-08,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

MyBatis-flex自动生成代码类,读取配置文件里的数据库信息

1. 引入maven包

<dependency>
    <groupId>com.mybatis-flex</groupId>
    <artifactId>mybatis-flex-codegen</artifactId>
    <version>1.5.5</version>
</dependency>

2. 使用main方法生成

package cn.ximiyun.huameng;

import cn.hutool.setting.dialect.Props;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.zaxxer.hikari.HikariDataSource;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * MyBatis-flex自动生成代码类
 */
public class CodeGenerator {

    public static void main(String[] args) {

        String projectPath = System.getProperty("user.dir");
        Props application = new Props(projectPath + "/src/main/resources/application.properties");

        String os = "base";
        String packageName = "cn.ximiyun.huameng." + os;
        String dataSourceName = application.getStr("spring.datasource.username");
        String dataSourcePassword = application.getStr("spring.datasource.password");
        String dataSourceUrl = application.getStr("spring.datasource.url");
        //需要生成的表
        String[] tables = "base_admin_user_info".split(",");
        //是否生成model
        boolean isModel = true;
        //是否生成dao
        boolean isDao = true;
        //是否生成Mapper
        boolean isMapper = true;

        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(dataSourceUrl+"&useInformationSchema=true");
        dataSource.setUsername(dataSourceName);
        dataSource.setPassword(dataSourcePassword);

        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.setBasePackage(packageName);
        globalConfig.setEntityPackage(packageName+".model");
        globalConfig.setMapperPackage(packageName+".dao");
        globalConfig.setMapperXmlPath(projectPath.concat("/src/main/resources/mapper/"+os));

        globalConfig.getJavadocConfig().setAuthor("GMQ");

        //设置表前缀和只生成哪些表
        globalConfig.setGenerateTable(tables);

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(isModel);
        globalConfig.setEntityWithLombok(true);
        globalConfig.setEntityOverwriteEnable(true);
//        globalConfig.setTableDefGenerateEnable(isModel);
//        globalConfig.setTableDefOverwriteEnable(true);
//        globalConfig.setTableDefPropertiesNameStyle(TableDefConfig.NameStyle.UPPER_CAMEL_CASE);

        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(isDao);
        globalConfig.setMapperClassSuffix("Dao");

        globalConfig.setMapperXmlGenerateEnable(isMapper);

        //表设置
        TableConfig tableConfig = new TableConfig();
        tableConfig.setMapperGenerateEnable(false);
        globalConfig.setTableConfig(tableConfig);

        JdbcTypeMapping.registerMapping(LocalDateTime.class, Date.class);

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

}

生成效果如图


评论

Polar.
最重要的,是拥有跟随内心与直觉的勇气

文章目录