Created
August 20, 2019 08:32
-
-
Save TouiSoraHe/f316e08248adb80042c982eb03d3fba3 to your computer and use it in GitHub Desktop.
mybatis-plus自动生成代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; | |
import com.baomidou.mybatisplus.core.toolkit.StringPool; | |
import com.baomidou.mybatisplus.core.toolkit.StringUtils; | |
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.VelocityTemplateEngine; | |
import org.springframework.stereotype.Component; | |
import org.yaml.snakeyaml.Yaml; | |
import java.io.IOException; | |
import java.util.*; | |
@Component | |
public class CodeGenerator { | |
private static String url = Config.get("spring.datasource.url"); | |
private static String driverName = Config.get("spring.datasource.driver-class-name"); | |
private static String username = Config.get("spring.datasource.username"); | |
private static String password = Config.get("spring.datasource.password"); | |
private static String projectPath = System.getProperty("user.dir"); | |
private static String moduleName = ""; | |
public static void generator(){ | |
// 代码生成器 | |
AutoGenerator mpg = new AutoGenerator(); | |
mpg.setGlobalConfig(getGlobalConfig()); | |
mpg.setDataSource(getDataSourceConfig()); | |
mpg.setPackageInfo(getPackageConfig()); | |
mpg.setCfg(getInjectionConfig()); | |
mpg.setTemplate(getTemplateConfig()); | |
mpg.setStrategy(getStrategyConfig()); | |
mpg.setTemplateEngine(new VelocityTemplateEngine()); | |
mpg.execute(); | |
} | |
/** | |
* 获取全局配置 | |
* @return | |
*/ | |
private static GlobalConfig getGlobalConfig(){ | |
// 全局配置 | |
GlobalConfig gc = new GlobalConfig(); | |
gc.setOutputDir(projectPath + "/src/main/java"); | |
gc.setAuthor(scanner("作者名")); | |
gc.setOpen(false); | |
gc.setBaseResultMap(true); | |
gc.setBaseColumnList(true); | |
return gc; | |
} | |
/** | |
* 数据源配置 | |
* @return | |
*/ | |
private static DataSourceConfig getDataSourceConfig(){ | |
// 数据源配置 | |
DataSourceConfig dsc = new DataSourceConfig(); | |
dsc.setUrl(url); | |
dsc.setDriverName(driverName); | |
dsc.setUsername(username); | |
dsc.setPassword(password); | |
return dsc; | |
} | |
/** | |
* 包配置 | |
* @return | |
*/ | |
private static PackageConfig getPackageConfig(){ | |
PackageConfig pc = new PackageConfig(); | |
pc.setParent(scanner("parent package(example:com.xxx.xxx)")); | |
pc.setModuleName(moduleName); | |
return pc; | |
} | |
/** | |
* 自定义配置 | |
* @return | |
*/ | |
private static InjectionConfig getInjectionConfig(){ | |
// 自定义配置 | |
InjectionConfig cfg = new InjectionConfig() { | |
@Override | |
public void initMap() { | |
// to do nothing | |
} | |
}; | |
// 自定义输出配置 | |
List<FileOutConfig> focList = new ArrayList<>(); | |
// 自定义配置会被优先输出 | |
focList.add(new FileOutConfig("/templates/mapper.xml.vm") { | |
@Override | |
public String outputFile(TableInfo tableInfo) { | |
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!! | |
return projectPath + "/src/main/resources/mapper/" + moduleName | |
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; | |
} | |
}); | |
cfg.setFileOutConfigList(focList); | |
return cfg; | |
} | |
/** | |
* 配置模板 | |
* @return | |
*/ | |
private static TemplateConfig getTemplateConfig(){ | |
// 配置模板 | |
TemplateConfig templateConfig = new TemplateConfig(); | |
// 配置自定义输出模板 | |
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 | |
templateConfig.setXml(null); | |
return templateConfig; | |
} | |
/** | |
* 策略配置 | |
* @return | |
*/ | |
private static StrategyConfig getStrategyConfig(){ | |
// 策略配置 | |
StrategyConfig strategy = new StrategyConfig(); | |
strategy.setNaming(NamingStrategy.underline_to_camel); | |
strategy.setColumnNaming(NamingStrategy.underline_to_camel); | |
strategy.setEntityLombokModel(true); | |
strategy.setRestControllerStyle(true); | |
strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); | |
strategy.setControllerMappingHyphenStyle(true); | |
return strategy; | |
} | |
/** | |
* <p> | |
* 读取控制台内容 | |
* </p> | |
*/ | |
private static String scanner(String tip) { | |
Scanner scanner = new Scanner(System.in); | |
StringBuilder help = new StringBuilder(); | |
help.append("请输入" + tip + ":"); | |
System.out.println(help.toString()); | |
if (scanner.hasNext()) { | |
String ipt = scanner.next(); | |
if (StringUtils.isNotEmpty(ipt)) { | |
return ipt; | |
} | |
} | |
throw new MybatisPlusException("请输入正确的" + tip + "!"); | |
} | |
public static void main(String[] args) throws IOException { | |
generator(); | |
} | |
} | |
class Config { | |
private static final Map config = new Yaml().load(CodeGenerator.class.getClassLoader().getResourceAsStream("application.yml")); | |
public static String get(String key){ | |
if (StringUtils.isNotEmpty(key)){ | |
Queue<String> queue = new LinkedList(Arrays.asList(key.split("\\."))); | |
return get(queue,config); | |
} | |
return null; | |
} | |
private static String get(Queue<String> strs, Map map){ | |
if(strs.size() > 1){ | |
Object o = map.get( strs.poll()); | |
if(o instanceof Map){ | |
return get(strs,(Map)o); | |
} | |
else{ | |
System.out.println("参数错误,该值不存在"); | |
return null; | |
} | |
} | |
Object ret = map.get(strs.poll()); | |
if(ret instanceof String) return (String) ret; | |
System.out.println("无法读取一个Object"); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment