mybatis-plus的代碼生成(逆向工程)


主要的分了五步:

1、數據源配置

DataSourceConfig dsc = new DataSourceConfig();
// 配置數據庫 url 地址
dsc.setUrl("jdbc:mysql://XXXXXX:3306/kanban?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
// 配置數據庫驅動
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
// 數據庫賬號
dsc.setUsername("賬號");
// 數據庫密碼
dsc.setPassword("密碼");

2、全局配置

GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
// 代碼生成的目錄
gc.setOutputDir(projectPath + "/src/main/java");
// 作者信息
gc.setAuthor("liaojie");
// 配置是否打開目錄,false 為不打開
gc.setOpen(false);
// 第二次生成會把第一次生成的覆蓋掉
gc.setFileOverride(true);

// 自定義service 命名
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");

// 生成 baseResultMap 和 baseColumnList
gc.setBaseResultMap(true); // 生成resultMap
gc.setBaseColumnList(true);// 在xml中生成基礎列

3、包配置

PackageConfig pc = new PackageConfig();
// 設置包名的父類名
pc.setParent("com.tianshu");
// 配置模塊名
pc.setModuleName("system");
mpg.setPackageInfo(pc);

// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};

// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
// 自定義輸出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定義配置會被優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 , 如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟着發生變化!!
return projectPath + "/src/main/java/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);

4、模板配置

TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);

5、策略配置

StrategyConfig strategy = new StrategyConfig();
// 配置數據表與實體類名之間映射的策略 (下划線到駝峰的命名方式)
strategy.setNaming(NamingStrategy.underline_to_camel);
// 配置數據表的字段與實體類的屬性名之間映射的策略 (表名字段名使用下划線)
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 配置 lombok 模式
strategy.setEntityLombokModel(true);
// 配置 rest 風格的控制器(@RestController)
strategy.setRestControllerStyle(true);
// 【實體】是否生成字段常量(默認 false)
strategy.setEntityColumnConstant(true);
// 是否為鏈式模型(默認 false)
strategy.setChainModel(true);

// 公共父類
strategy.setSuperEntityClass(BaseEntity.class);
// 自定義的 BaseMapper
strategy.setSuperMapperClass("com.tianshu.base.BaseMapper");
// 自定義的service
strategy.setSuperServiceClass(BaseService.class);
// 自定義的service實現類
strategy.setSuperServiceImplClass(BaseServiceImpl.class);
// 自定義的Controller
strategy.setSuperControllerClass(BaseController.class);

// 寫於父類中的公共字段
strategy.setSuperEntityColumns();

if (StringUtils.hasText(pc.getModuleName())) {
strategy.setTablePrefix(pc.getModuleName() + "_");
}
strategy.setControllerMappingHyphenStyle(true);


主要的代碼
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.tianshu.base.BaseController;
import com.tianshu.base.BaseEntity;
import com.tianshu.base.BaseService;
import com.tianshu.base.BaseServiceImpl;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


public class CodeGenerator {

    public static void main(String[] args) {
// 代碼生成器 AutoGenerator mpg = new AutoGenerator(); // 數據源配置 DataSourceConfig dsc = new DataSourceConfig(); // 配置數據庫 url 地址 dsc.setUrl("jdbc:mysql://數據庫地址:3306/kanban?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai"); // 配置數據庫驅動 dsc.setDriverName("com.mysql.cj.jdbc.Driver"); // 數據庫賬號 dsc.setUsername("賬號"); // 數據庫密碼 dsc.setPassword("密碼"); mpg.setDataSource(dsc); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); // 代碼生成的目錄 gc.setOutputDir(projectPath + "/src/main/java"); // 作者信息 gc.setAuthor("liaojie"); // 配置是否打開目錄,false 為不打開 gc.setOpen(false); // 第二次生成會把第一次生成的覆蓋掉 gc.setFileOverride(true); // 自定義service 命名 gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); // 生成 baseResultMap 和 baseColumnList gc.setBaseResultMap(true); // 生成resultMap gc.setBaseColumnList(true);// 在xml中生成基礎列 mpg.setGlobalConfig(gc); // 包配置 PackageConfig pc = new PackageConfig(); // 設置包名的父類名 pc.setParent("com.tianshu"); // 配置模塊名 pc.setModuleName("system"); mpg.setPackageInfo(pc); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker // String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity String templatePath = "/templates/mapper.xml.vm"; // 自定義輸出配置 List<FileOutConfig> focList = new ArrayList<>(); // 自定義配置會被優先輸出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定義輸出文件名 , 如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟着發生變化!! return projectPath + "/src/main/java/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); // 配置數據表與實體類名之間映射的策略 (下划線到駝峰的命名方式) strategy.setNaming(NamingStrategy.underline_to_camel); // 配置數據表的字段與實體類的屬性名之間映射的策略 (表名字段名使用下划線) strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 配置 lombok 模式 strategy.setEntityLombokModel(true); // 配置 rest 風格的控制器(@RestController) strategy.setRestControllerStyle(true); // 【實體】是否生成字段常量(默認 false) strategy.setEntityColumnConstant(true); // 是否為鏈式模型(默認 false) strategy.setChainModel(true); // 公共父類 strategy.setSuperEntityClass(BaseEntity.class); // 自定義的 BaseMapper strategy.setSuperMapperClass("com.tianshu.base.BaseMapper"); // 自定義的service strategy.setSuperServiceClass(BaseService.class); // 自定義的service實現類 strategy.setSuperServiceImplClass(BaseServiceImpl.class); // 自定義的Controller strategy.setSuperControllerClass(BaseController.class); // 寫於父類中的公共字段 strategy.setSuperEntityColumns(); if (StringUtils.hasText(pc.getModuleName())) { strategy.setTablePrefix(pc.getModuleName() + "_"); } strategy.setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy); mpg.execute(); } }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM