其他分享
首页 > 其他分享> > MyBatisPlus代码生成示例

MyBatisPlus代码生成示例

作者:互联网

一、依赖

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.3</version>
</dependency>
<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>compile</scope>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.4.1</version>
</dependency>

二、基础类

import com.baomidou.mybatisplus.generator.config.*;

/**
 * 基础测试类
 *
 * @author lanjerry
 * @since 3.5.3
 */
public class BaseGeneratorTest {

    /**
     * 执行数据库脚本
     */

    /**
     * 策略配置
     */
    protected static StrategyConfig.Builder strategyConfig() {
        return new StrategyConfig.Builder();
    }

    /**
     * 全局配置
     */
    protected static GlobalConfig.Builder globalConfig() {
        return new GlobalConfig.Builder();
    }

    /**
     * 包配置
     */
    protected static PackageConfig.Builder packageConfig() {
        return new PackageConfig.Builder();
    }

    /**
     * 模板配置
     */
    protected static TemplateConfig.Builder templateConfig() {
        return new TemplateConfig.Builder();
    }

    /**
     * 注入配置
     */
    protected static InjectionConfig.Builder injectionConfig() {
        // 测试自定义输出文件之前注入操作,该操作再执行生成代码前 debug 查看
        return new InjectionConfig.Builder().beforeOutputFile((tableInfo, objectMap) -> {
            System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
        });
    }
}

三、测试类


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.type.JdbcType;
import org.junit.jupiter.api.Test;

/**
 * Oracle 代码生成
 *
 * @author lanjerry
 * @since 3.5.3
 */
public class OracleGeneratorTest extends BaseGeneratorTest {

    /**
     * 数据源配置
     */
    private static final DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:oracle:thin:@172.18.2.10:1521:stdb", "ecology", "QJ9svVFK18mPH6rw")
            .schema("ECOLOGY").typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                if (metaInfo.getJdbcType().equals(JdbcType.NUMERIC) && metaInfo.getScale() == 0) {
                    return DbColumnType.LONG;
                } else {
                    return typeRegistry.getColumnType(metaInfo);
                }
            })
            .build();

    @Test
    public void testSimple() {
        /*  文档 https://github.com/baomidou/generator */
        AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
        generator.global(globalConfig().disableOpenDir()
                .author("oa").outputDir("D:\\JkWorkSpace\\oa\\src\\main\\java\\")
                .build());
        generator.strategy(strategyConfig()
                .addInclude("HRMRESOURCE,HRMDEPARTMENT") // 设置需要生成的表名
                .addTablePrefix("t_", "c_")// 设置过滤表前缀
                .entityBuilder().enableFileOverride().enableRemoveIsPrefix()
                .enableChainModel().enableLombok().enableTableFieldAnnotation()
                .mapperBuilder().enableFileOverride().mapperAnnotation(Mapper.class)
                .build());
        generator.packageInfo(packageConfig()
                .parent("com.jinko.mybatis") // 设置父包名
                .mapper("mapper.oa")
                .xml("mapper.oa")
                .entity("model")
                .build());
        generator.injection(injectionConfig()
                .beforeOutputFile((b, r) -> {
                    System.out.println(b);
                }).build());
        generator.template(templateConfig()
                .disable(TemplateType.CONTROLLER)
                .disable(TemplateType.SERVICE)
                .disable(TemplateType.SERVICE_IMPL)
                .build())
                .execute(new FreemarkerTemplateEngine());// 使用Freemarker引擎模板,默认的是Velocity引擎模板
    }


}

标签:代码生成,MyBatisPlus,generator,示例,Builder,baomidou,new,import,com
来源: https://www.cnblogs.com/xysgo/p/16497719.html