mybatis-plus:代码生成器

一、依赖

代码生成器需要添加一下依赖

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

        <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-engine-core -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.31</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.7</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
    </dependencies>

二、代码实现

定义参数体:

@Data
@Accessors(chain = true)
public class XxxParam {
    private String userName;
    private String password;
    private String auth;
    private String host;
    private String port;
    private String dbName;

    /**
     * 多个表用逗号分割
     */
    private String tableNames;

    private String outPutDir = UserPortalConstant.OUT_PUT_DIR;
    private String packagePath = UserPortalConstant.PACKAGE_PATH;
}

代码生成类:

public class BaseGenerator {
    private static final String SERVICE_IMPL_NAME = "ServiceImpl";
    private static final String SERVICE_NAME = "Service";

    private static final String ENTITY_NAME = "Po";

    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";

    private static final String URL_FORMAT = "jdbc:mysql://%s:%s/%s";

    public void execute(XxxParam  generatorParam) {
        // 1、创建代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        // 2、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        autoGenerator.setGlobalConfig(globalConfig);
        globalConfig.setOutputDir(generatorParam.getOutPutDir())
                .setAuthor(generatorParam.getAuth())
                .setOpen(Boolean.FALSE) //生成后是否打开资源管理器
                .setFileOverride(Boolean.TRUE) //重新生成时文件是否覆盖
                .setServiceImplName("%s" + SERVICE_IMPL_NAME)
                .setServiceName("%s" + SERVICE_NAME)	//去掉Service接口的首字母I
                .setEntityName("%s" + ENTITY_NAME)
                .setIdType(IdType.ID_WORKER) //主键策略
                .setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
                //.setSwagger2(true);//开启Swagger2模式


        // 3、数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        autoGenerator.setDataSource(dataSourceConfig);
        dataSourceConfig.setUrl(String.format(URL_FORMAT, generatorParam.getHost(), generatorParam.getPort(), generatorParam.getDbName()))
            .setDriverName(DRIVER_NAME)
            .setUsername(generatorParam.getUserName())
            .setPassword(generatorParam.getPassword())
            .setDbType(DbType.MYSQL);

        // 4、包配置
        PackageConfig packageConfig = new PackageConfig();
        //packageConfig.setModuleName("table"); //模块名
        autoGenerator.setPackageInfo(packageConfig);
        packageConfig.setParent(generatorParam.getPackagePath())
            .setEntity("entity")
            .setService("service")
            .setServiceImpl("service.impl")
            .setMapper("dao")
            .setXml(null);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        autoGenerator.setStrategy(strategy);
        strategy.setInclude(generatorParam.getTableNames()) // 设置需要生成的表名
            .setNaming(NamingStrategy.underline_to_camel) //数据库表映射到实体的命名策略
            //.setTablePrefix(packageConfig.getModuleName() + "_"); //生成实体时去掉表前缀
            .setColumnNaming(NamingStrategy.underline_to_camel) //数据库表字段映射到实体的命名策略
            .setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
        autoGenerator.execute();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉成226

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值