MyBatis-Flex代码生成

引入依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>

<dependency>
    <groupId>com.mybatis-flex</groupId>
    <artifactId>mybatis-flex-spring-boot-starter</artifactId>
    <version>1.11.0</version>
</dependency>

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

代码实现

java 复制代码
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.qiangesoft.mybatisflex.base.BaseEntity;
import com.zaxxer.hikari.HikariDataSource;

/**
 * 代码生成工具类
 */
public class CodegenUtil {

    public static void main(String[] args) {
        // 配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/db_mybatisflex?useInformationSchema=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        // 创建配置内容,两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle2();

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

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

    /**
     * 常规风格
     */
    public static GlobalConfig createGlobalConfigUseStyle1() {
        // 创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 设置路径及根包
        globalConfig.setSourceDir("D:/work/code/boot-business/boot-business-mybatisflex/src/main/java");
        globalConfig.setBasePackage("com.qiangesoft.mybatisflex");
        globalConfig.setMapperXmlPath("D:/work/code/boot-business/boot-business-mybatisflex/src/main/resources/mapper");

        // 设置生成策略
//        globalConfig.setTablePrefix("tb_");
        globalConfig.setGenerateTable("sys_user", "sys_dept");
        globalConfig.setUnGenerateTable("tb_test");
        globalConfig.setLogicDeleteColumn("deleted");
        globalConfig.setVersionColumn("version");

        // 设置 javadoc
        globalConfig.setAuthor("qiangesoft");
        globalConfig.setSince("1.0.0");

        // 设置生成 entity
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
//        globalConfig.setEntitySuperClass(BaseEntity.class);
        globalConfig.setEntityJdkVersion(11);

        // 设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.setMapperAnnotation(true);

        // 设置生成 mapper.xml
        globalConfig.setMapperXmlGenerateEnable(true);

        // 设置生成 service
        globalConfig.setServiceGenerateEnable(true);

        // 设置生成 serviceImpl
        globalConfig.setServiceImplGenerateEnable(true);

        // 设置生成 controller
        globalConfig.setControllerGenerateEnable(true);
        globalConfig.setControllerRestStyle(true);
//        globalConfig.setControllerRequestMappingPrefix("sys");

        // 单独配置某个列
//        ColumnConfig columnConfig1 = new ColumnConfig();
//        columnConfig1.setColumnName("create_time");
//        columnConfig1.setOnInsertValue("now()");
//
//        ColumnConfig columnConfig2 = new ColumnConfig();
//        columnConfig2.setColumnName("update_time");
//        columnConfig2.setOnUpdateValue("now()");
//
//        globalConfig.setColumnConfig(columnConfig1);
//        globalConfig.setColumnConfig(columnConfig2);

        // 指定表
        // globalConfig.setColumnConfig("tb_account", columnConfig);

        return globalConfig;
    }

    /**
     * 链式风格
     */
    public static GlobalConfig createGlobalConfigUseStyle2() {
        // 创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 设置路径及根包
        globalConfig.getPackageConfig()
                .setSourceDir("D:/work/code/boot-business/boot-business-mybatisflex/src/main/java")
                .setBasePackage("com.qiangesoft.mybatisflex")
                .setMapperXmlPath("D:/work/code/boot-business/boot-business-mybatisflex/src/main/resources/mapper");

        // 设置生成策略
        globalConfig.getStrategyConfig()
//                .setTablePrefix("sys_")
                .setGenerateTable("sys_user", "sys_dept")
                .setUnGenerateTable("tb_test")
                .setIgnoreColumns("test")
                .setLogicDeleteColumn("deleted")
                .setVersionColumn("version");

        // 设置 javadoc
        globalConfig.getJavadocConfig()
                .setAuthor("admin")
                .setSince("1.0.0");

        // 设置生成 entity
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setAlwaysGenColumnAnnotation(true)
		//        .setSuperClass(BaseEntity.class)
                .setJdkVersion(11);

        // 设置生成 mapper
        globalConfig.enableMapper()
                .setMapperAnnotation(true);

        // 配置生成 mapper.xml
        globalConfig.enableMapperXml();

        // 设置生成 service
        globalConfig.enableService();

        // 设置生成 serviceImpl
        globalConfig.enableServiceImpl();

        // 设置生成 controller
        globalConfig.enableController()
                .setRestStyle(true)
                .setRequestMappingPrefix("sys");

        // 单独配置某个列
//        ColumnConfig columnConfig1 = new ColumnConfig();
//        columnConfig1.setColumnName("create_time");
//        columnConfig1.setOnInsertValue("now()");
//
//        ColumnConfig columnConfig2 = new ColumnConfig();
//        columnConfig2.setColumnName("update_time");
//        columnConfig2.setOnUpdateValue("now()");
//
//        globalConfig.getStrategyConfig()
//                .setColumnConfig(columnConfig1)
//                .setColumnConfig(columnConfig2);

        return globalConfig;
    }

}

生成结果


相关推荐
考虑考虑3 小时前
Mybatis实现批量插入
java·后端·mybatis
敲个大西瓜14 天前
mybatis拦截器插件实现数据库字段加解密
mybatis
武子康14 天前
Java-28 深入浅出 Spring 实现简易Ioc-04 在上节的业务下手动实现AOP
java·后端·mybatis
一条泥憨鱼14 天前
苍穹外卖【day6|微信登录与商品浏览功能】
后端·mybatis·苍穹外卖
vx-Biye_Design14 天前
springboot安阳地区研学旅游服务小程序-计算机毕业设计源码12785
java·vue.js·windows·spring boot·tomcat·maven·mybatis
摇滚侠14 天前
MyBatis+Spring+SpringMVC SSM 整合 179-185
java·spring·mybatis
摇滚侠14 天前
MyBatis+Spring+SpringMVC SSM ContextLoaderListener 177-178
java·spring·mybatis
Spring小子14 天前
【Spring Boot + Vue + DeepSeek】从零打造一个AI驱动的智能健康分析系统
java·spring boot·mybatis
武子康15 天前
Java-27 深入浅出 Spring - 实现简易Ioc-03 在上节的业务下手动实现IoC 从 XML 配置到 BeanFactory 反射注入
java·后端·mybatis