MyBatis代码生成器

一、代码生成器
作用:根据提供的数据库的表名让生成器自动帮助我们完成各种代码的创建。

配置:

1、数据库配置:

new DataSourceConfig.Builder("jdbc:mysql://127.0.0.1:3306/mybatis-plus","root","123456")

    .build();

2、全局配置

new GlobalConfig.Builder()
    .fileOverride()//覆盖已生成文件
    .outputDir("/opt/baomidou")//指定输出目录
    .author("baomidou")//作者名
    .enableKotlin()//	开启 kotlin 模式,默认值为false
    .enableSwagger()//开启 swagger 模式,默认值是false
    .dateType(DateType.TIME_PACK)//注释日期
    .commentDate("yyyy-MM-dd")//注释日期
    .build();

3、包配置

new PackageConfig.Builder()
    .parent("com.baomidou.mybatisplus.samples.generator")//父包名
    .moduleName("sys")//父包模块名
    .entity("po")//Entity 包名
    .service("service")//Service 包名
    .serviceImpl("service.impl")//Service Impl 包名
    .mapper("mapper")//Mapper 包名
    .xml("mapper.xml")//Mapper XML 包名
    .controller("controller")//Controller 包名
    .other("other")//自定义文件包名
    .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://"))//路径配置信息
    .build();

3、模板配置

new TemplateConfig.Builder()
    .disable(TemplateType.ENTITY)//	禁用模板
    .entity("/templates/entity.java")//设置实体模板路径(JAVA)
    .service("/templates/service.java")//设置 service 模板路径
    .serviceImpl("/templates/serviceImpl.java")//设置 serviceImpl 模板路径
    .mapper("/templates/mapper.java")//设置 mapper 模板路径
    .mapperXml("/templates/mapper.xml")//设置 mapperXml 模板路径
    .controller("/templates/controller.java")//设置 controller 模板路径
    .build();

4、注入配置

new InjectionConfig.Builder()
    .beforeOutputFile((tableInfo, objectMap) -> {
        System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
    })//输出文件之前消费者
    .customMap(Collections.singletonMap("test", "baomidou"))//自定义配置 Map 对象
    .customFile(Collections.singletonMap("test.txt", "/templates/test.vm"))//自定义配置模板文件
    .build();

5、策略配置

new StrategyConfig.Builder()
    .enableCapitalMode()//开启大写命名
    .enableSkipView()//开启跳过视图
    .disableSqlFilter()//禁用 sql 过滤
    .likeTable(new LikeTable("USER"))//	模糊表匹配(sql 过滤)
    .addInclude("t_simple")//增加表匹配(内存过滤)
    .addTablePrefix("t_", "c_")//增加过滤表前缀
    .addFieldSuffix("_flag")//增加过滤字段后缀
    .build();
package com.example.demo;

import java.util.Collections;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

public class Demo {
	
	public static void main(String[] args) {
		
		
		FastAutoGenerator.create("jdbc:mysql://localhost:3306/test?useSSL=true", "root", "123456")
	    .globalConfig(builder -> {
	        builder.author("wenyay") // 设置作者
	            .enableSwagger() // 开启 swagger 模式
	            .fileOverride() // 覆盖已生成文件
	            .outputDir("E:\\test\\sts_workspace\\work04" + "/src/main/java"); // 指定输出目录
	    })
	    .packageConfig(builder -> {
	        builder.parent("com.wenyay.demo") // 设置父包名
	            .moduleName(null) // 设置父包模块名
	            .entity("entity")
	            .controller("controller")
	            .service("service")
	            .mapper("mapper");
//	            .pathInfo(Collections.singletonMap(OutputFile.xml, "E://")); // 设置mapperXml生成路径
	    })
	    .strategyConfig(builder -> {
	        builder.addInclude("tb_user") // 设置需要生成的表名
	            .addTablePrefix("tb_"); // 设置过滤表前缀
	    })
	    .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
	    .execute();


	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值