package com.fhresource.crb.util;
import com.baomidou.mybatisplus.annotation.DbType;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MybatisPlusCodeGenerator {
// ================= 自定义配置 =================
private static final DbType DB_TYPE = DbType.MYSQL;
private static final String JDBC_URL = "jdbc:mysql://ip:3306/数据库地址?characterEncoding=UTF-8&useSSL=false";
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String JDBC_USERNAME = "root";
private static final String JDBC_PASSWORD = "123456";
/**
* 生成文件根目录及包名
*/
private static final String ROOT_DIR = "项目名称/src/main/java";
private static final String PACKAGE_PARENT_NAME = "com.fhresource.crb";
private static final String PACKAGE_CONTROLLER_NAME = "controller";
private static final String PACKAGE_SERVICE_NAME = "service";
private static final String PACKAGE_SERVICEIMPL_NAME = "service.impl";
private static final String PACKAGE_ENTITY_NAME = "entity.dto";
private static final String PACKAGE_MAPPER_NAME = "dao";
private static final String RESOURCES_DIR = "server/src/main/resources";
private static final String MAPPER_XML_PATH = "mapper";
/**
* 生成代码的@author
*/
private static final String AUTHOR = "LZY";
/**
* 需要生成代码的数据库表名
*/
private static final String[] TABLES = new String[]{"t_ebook", "t_ebook_detail"
, "t_label", "t_resource"};
/**
* 数据库表名前缀
*/
private static final String[] TABLE_PREFIXS = new String[]{"t_"};
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
mpg.setDataSource(getDataSourceConfig());
mpg.setGlobalConfig(getGlobalConfig());
mpg.setStrategy(getStrategyConfig());
mpg.setPackageInfo(getPackageConfig());
mpg.setCfg(getInjectionConfig());
mpg.setTemplate(getTemplateConfig());
// 执行生成
mpg.execute();
}
/**
* 数据源配置
*
* @return
*/
private static DataSourceConfig getDataSourceConfig() {
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DB_TYPE);
dsc.setDriverName(JDBC_DRIVER);
dsc.setUrl(JDBC_URL);
dsc.setUsername(JDBC_USERNAME);
dsc.setPassword(JDBC_PASSWORD);
return dsc;
}
/**
* 全局配置
*
* @return
*/
private static GlobalConfig getGlobalConfig() {
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(ROOT_DIR);
gc.setFileOverride(true);
gc.setActiveRecord(true);
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(true);// XML columList
gc.setAuthor(AUTHOR);
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setControllerName("%sController");
return gc;
}
/**
* 生成策略配置
*
* @return
*/
private static StrategyConfig getStrategyConfig() {
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix(TABLE_PREFIXS);// 此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setInclude(TABLES); // 需要生成的表
strategy.setEntityLombokModel(true);
return strategy;
}
/**
* 生成包名设置
*
* @return
*/
private static PackageConfig getPackageConfig() {
// 4.生成文件所在包配置:
PackageConfig pc = new PackageConfig();
pc.setParent(PACKAGE_PARENT_NAME);
pc.setController(PACKAGE_CONTROLLER_NAME);
pc.setService(PACKAGE_SERVICE_NAME);
pc.setServiceImpl(PACKAGE_SERVICEIMPL_NAME);
pc.setEntity(PACKAGE_ENTITY_NAME);
pc.setMapper(PACKAGE_MAPPER_NAME);
return pc;
}
/**
* xml文件配置
*
* @return
*/
private static InjectionConfig getInjectionConfig() {
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<String, Object>();
//map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb");
this.setMap(map);
}
};
//xml生成路径
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return RESOURCES_DIR + "/" + MAPPER_XML_PATH + "/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
cfg.setFileOutConfigList(focList);
return cfg;
}
/**
* 关闭默认 xml 生成
*
* @return
*/
private static TemplateConfig getTemplateConfig() {
TemplateConfig tc = new TemplateConfig();
tc.setXml(null);
return tc;
}
}
MybatisPlus逆向生成工具类
最新推荐文章于 2024-01-03 10:54:21 发布