mybatis plus generator配置
mybatis plus generator配置
代码生成器AutoGenerator
AutoGenerator autoGenerator = new AutoGenerator();
GlobalConfig
全局配置,定义文件的输出目录,设置文件的@author信息等公共配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");// 获取用户程序当前路径(项目根的路径)
gc.setOutputDir(projectPath + "/src/main/java");// 生成文件的输出目录(默认值:D 盘根目录)
gc.setAuthor("yourName");// 开发人员(默认值:null)
gc.setOpen(false);// 是否打开输出目录(默认值:null)
gc.setFileOverride(true);// 是否覆盖已有文件(默认值:false)
autoGenerator.setGlobalConfig(gc);//把全局配置添加到代码生成器主类
DataSourceConfig
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://xxx.xxx.x.x:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8");
//dsc.setSchemaName("public");// 数据库方案名
dsc.setDbType(DbType.MYSQL);// 数据库类型
dsc.setDriverName("com.mysql.cj.jdbc.Driver");// 驱动名称
dsc.setUsername("xxx"); // 用户名
dsc.setPassword("xxx"); // 密码
autoGenerator.setDataSource(dsc);//把数据源配置添加到代码生成器主类
PackageConfig
包配置,就是每个类最上面的package,比如:package com.ck.mybatisplus.foundation.service;这里包配置可以分别配置service、entity、controller等等
PackageConfig pc = new PackageConfig();
pc.setModuleName("foundation");// 父包模块名
pc.setParent("com.ck.mybatisplus");// 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setService("service");// Service包名
pc.setEntity("entity");// Entity包名
pc.setServiceImpl("service.impl");// ServiceImpl包名
pc.setMapper("mapper");// Mapper包名
pc.setController("controller");// Contoller包名
// pc.setXml("mapper.xml");// Mapper.xml包名
mpg.setPackageInfo(pc);// 把包配置添加到代码生成器主类
TemplateConfig
方法一
设置自定义模板路径,文件配置信息使用默认的。(如:生成的文件名,生成的文件路径等),一般都是用mybatis plus中自带的模板
//不用带上.ftl/.vm, 会根据使用的模板引擎自动识别
templateConfig.setEntity("templates/entity.java");
templateConfig.setService("templates/service.java");
templateConfig.setController();
方法二
使用自定义输出配置
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/resources/mapper/mysql/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
StrategyConfig
一些策略的配置
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(INCLUDE_TABLES.split(","));
strategy.setControllerMappingHyphenStyle(true);
// strategy.setTablePrefix(pc.getModuleName() + "_");
autoGenerator.setStrategy(strategy);
autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
生成器工具类
自己写的一个生成器工具类,可以参考一下,根据自己的需求进行修改
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.*;
/**
* @author wenyao
*/
public class CodeGenerator {
private final static AutoGenerator autoGenerator = new AutoGenerator();
//表名映射(','号分割符)
private static String INCLUDE_TABLES = "user";
//private static String INCLUDE_TABLES = "user,home";
//包名配置
private static String CONTROLLER = "controller";
private static String SERVICE = "service";
private static String SERVICEIMPL = "service.Impl";
private static String MAPPER = "mapper";
private static String ENTITY = "entity";
private static String projectPath = System.getProperty("user.dir");// 获取用户程序当前路径(项目根的路径)
private static String ParentName = "com.wenyao";
private static String ModuleName = "";
//公共全局
private static String AUTHOR = "wenyao";
//数据源配置
private static String DB_USERNAME = "root";
private static String DB_PASSWORD = "xxx";
private static String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
private static String DB_URL = "jdbc:mysql://localhost:3306/mybatis_plus_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
//xml模板文件路径
private static String TEMPLATEPATH = "/templates/mapper.xml.ftl";
//配置
private static GlobalConfig gc = new GlobalConfig();
private static DataSourceConfig dsc = new DataSourceConfig();
private static PackageConfig pc = new PackageConfig();
private static InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
private static TemplateConfig templateConfig = new TemplateConfig();
private static StrategyConfig strategy = new StrategyConfig();
/**
* 初始化全局配置
*/
private static void initGlobalConfig(){
// 全局配置
gc.setOutputDir(projectPath + "/src/main/java");// 生成文件的输出目录(默认值:D 盘根目录)
gc.setAuthor(AUTHOR);// 开发人员(默认值:null)
gc.setOpen(false);// 是否打开输出目录(默认值:null)
gc.setFileOverride(true);// 是否覆盖已有文件(默认值:false)
gc.setSwagger2(true);// 实体属性 Swagger2 注解
gc.setBaseResultMap(true); //在xml中生成BaseResultMap
gc.setBaseColumnList(true); //
autoGenerator.setGlobalConfig(gc);//把全局配置添加到代码生成器主类
}
/**
* 初始化数据源配置
*/
private static void initDataSourceConfig(){
// 数据源配置
dsc.setUrl(DB_URL);
//dsc.setSchemaName("public");// 数据库方案名
dsc.setDbType(DbType.MYSQL);// 数据库类型
dsc.setDriverName(DB_DRIVER);// 驱动名称
dsc.setUsername(DB_USERNAME); // 用户名
dsc.setPassword(DB_PASSWORD); // 密码
autoGenerator.setDataSource(dsc);//把数据源配置添加到代码生成器主类
}
/**
* 初始化包配置
*/
private static void initPackageConfig(){
// 包配置
pc.setModuleName(ModuleName);// 父包模块名
pc.setParent(ParentName);// 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setService(SERVICE);// Service包名
pc.setEntity(ENTITY);// Entity包名
pc.setServiceImpl(SERVICEIMPL);// ServiceImpl包名
pc.setMapper(MAPPER);// Mapper包名
pc.setController(CONTROLLER);// Contoller包名
// pc.setXml("mapper.xml");// Mapper.xml包名(自定义配置到resource下,所以这里不使用默认路径)
autoGenerator.setPackageInfo(pc);// 把包配置添加到代码生成器主类
}
/**
* 初始化自定义配置
* 这里配置生成的mapper.xml文件的自定义输出路径(默认不是输出到resources文件夹下,所以需要自定义)
*/
private static void initInjectionConfig(){
// 自定义配置
// 自定义输出配置,自定义配置会被优先输出
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig(TEMPLATEPATH) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/mysql/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
autoGenerator.setCfg(cfg);
}
/**
* 初始化模板配置
*/
private static void initTemplateConfig(){
// 配置模板
templateConfig.setXml(null);
autoGenerator.setTemplate(templateConfig);
}
/**
* 生成策略初始化
*/
private static void initStrategyConfig(){
// 策略配置
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(INCLUDE_TABLES.split(","));
strategy.setControllerMappingHyphenStyle(true);
// strategy.setTablePrefix(pc.getModuleName() + "_");
autoGenerator.setStrategy(strategy);
autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
}
public static void init(){
initGlobalConfig();
initDataSourceConfig();
initPackageConfig();
initInjectionConfig();
initTemplateConfig();
initStrategyConfig();
}
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
init();
autoGenerator.execute();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)