All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fanzezhen.generator.GeneratorTool Maven / Gradle / Ivy

There is a newer version: 1.3.4.4
Show newest version
package com.github.fanzezhen.generator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
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 org.apache.commons.lang3.StringUtils;

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

public class GeneratorTool {
    /**
     * 

* 读取控制台内容 *

*/ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); System.out.println("请输入" + tip + ":"); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } /** *

* 读取控制台内容 *

*/ public static void generator(GeneratorBean generatorBean) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/" + generatorBean.getModuleName() + "/src/main/java"); gc.setAuthor(generatorBean.getAuthor()); gc.setOpen(false); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl(generatorBean.getDataSourceConfigUrl()); // dsc.setSchemaName("public"); dsc.setDriverName(generatorBean.getDriverName()); dsc.setUsername(generatorBean.getDbUsername()); dsc.setPassword(generatorBean.getDbPassword()); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(generatorBean.getPackageName()); pc.setParent(generatorBean.getModulePackageName()); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { // 自定义输入文件名称 return projectPath + "/" + generatorBean.getModuleName() + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); if (StringUtils.isNotEmpty(generatorBean.getSuperEntityClassName())) strategy.setSuperEntityClass(generatorBean.getSuperEntityClassName()); strategy.setEntityLombokModel(true); // strategy.setSuperControllerClass("com.baomidou.mybatisplus.samples.generator.common.BaseController"); // strategy.setSuperEntityColumns("id"); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix("t_"); strategy.setLogicDeleteFieldName("archived"); strategy.setInclude(generatorBean.getTables().split(generatorBean.getTableNameSplitter())); mpg.setStrategy(strategy); // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有! mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy