先看效果图
生成前项目目录
生成后项目目录
需要引入依赖
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-generator</ artifactId>
< version> 3.4.1</ version>
</ dependency>
< dependency>
< groupId> org.apache.velocity</ groupId>
< artifactId> velocity-engine-core</ artifactId>
< version> 2.3</ version>
</ dependency>
更换项目路径、数据库连接参数即可
package com. youxue. webflux. util;
import com. baomidou. mybatisplus. annotation. DbType;
import com. baomidou. mybatisplus. annotation. IdType;
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. DateType;
import com. baomidou. mybatisplus. generator. config. rules. NamingStrategy;
import java. util. ArrayList;
import java. util. List;
import java. util. Scanner;
class CodeGeneratorUtil {
public static void main ( String[ ] args) {
String parent = "com.youxue.webflux" ;
String url = "dataBaseUrl" ;
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver" ;
String user = "userName" ;
String pwd = "password" ;
AutoGenerator mpg = new AutoGenerator ( ) ;
String projectPath = System. getProperty ( "user.dir" ) ;
setGlobalConfig ( mpg, projectPath + "\\webflux\\src\\main\\java" ) ;
setDataSourceConfig ( mpg, url, driver, user, pwd) ;
PackageConfig pc = setPackageConfig ( mpg, parent) ;
setInjectionConfig ( mpg, projectPath) ;
setTemplateConfig ( mpg) ;
setStrategyConfig ( mpg, pc) ;
mpg. execute ( ) ;
}
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. isNotBlank ( ipt) ) {
return ipt;
}
}
throw new MybatisPlusException ( "请输入正确的" + tip + "!" ) ;
}
private static void setGlobalConfig ( AutoGenerator mpg, String projectPath) {
GlobalConfig gc = new GlobalConfig ( ) ;
gc. setOutputDir ( projectPath) ;
gc. setAuthor ( "dx" ) ;
gc. setOpen ( false ) ;
gc. setIdType ( IdType. AUTO) ;
gc. setFileOverride ( true ) ;
gc. setDateType ( DateType. TIME_PACK) ;
gc. setEnableCache ( false ) ;
gc. setEntityName ( "%sPo" ) ;
gc. setMapperName ( "%sMapper" ) ;
gc. setXmlName ( "%sMapper" ) ;
gc. setServiceName ( "" ) ;
gc. setServiceImplName ( "" ) ;
gc. setControllerName ( "" ) ;
mpg. setGlobalConfig ( gc) ;
}
private static void setDataSourceConfig ( AutoGenerator mpg, String url, String driver, String user, String pwd) {
DataSourceConfig dsc = new DataSourceConfig ( ) ;
dsc. setUrl ( url) ;
dsc. setDriverName ( driver) ;
dsc. setUsername ( user) ;
dsc. setPassword ( pwd) ;
dsc. setDbType ( DbType. SQL_SERVER) ;
mpg. setDataSource ( dsc) ;
}
private static PackageConfig setPackageConfig ( AutoGenerator mpg, String parent) {
PackageConfig pc = new PackageConfig ( ) ;
String moduleName = scanner ( "模块名" ) ;
pc. setParent ( parent) ;
pc. setController ( "controller." + moduleName) ;
pc. setEntity ( "model." + moduleName) ;
pc. setService ( "service." + moduleName) ;
pc. setServiceImpl ( "service." + moduleName + ".impl" ) ;
pc. setMapper ( "dao." + moduleName) ;
mpg. setPackageInfo ( pc) ;
return pc;
}
private static void setInjectionConfig ( AutoGenerator mpg, String projectPath) {
InjectionConfig cfg = new InjectionConfig ( ) {
@Override
public void initMap ( ) {
}
} ;
String templatePath = "/templates/mapper.xml.vm" ;
List< FileOutConfig> focList = new ArrayList < > ( ) ;
focList. add ( new FileOutConfig ( templatePath) {
@Override
public String outputFile ( TableInfo tableInfo) {
return projectPath + "\\webflux\\src\\main\\resources\\mapper\\" + tableInfo. getName ( ) + "Mapper" + StringPool. DOT_XML;
}
} ) ;
cfg. setFileOutConfigList ( focList) ;
mpg. setCfg ( cfg) ;
}
private static void setTemplateConfig ( AutoGenerator mpg) {
TemplateConfig templateConfig = new TemplateConfig ( ) ;
templateConfig. setXml ( null) ;
mpg. setTemplate ( templateConfig) ;
}
private static void setStrategyConfig ( AutoGenerator mpg, PackageConfig pc) {
StrategyConfig strategy = new StrategyConfig ( ) ;
strategy. setNaming ( NamingStrategy. no_change) ;
strategy. setColumnNaming ( NamingStrategy. no_change) ;
strategy. setEntityLombokModel ( true ) ;
strategy. setRestControllerStyle ( true ) ;
strategy. setSuperEntityColumns ( "id" ) ;
strategy. setInclude ( scanner ( "表名,多个英文逗号分割" ) . split ( "," ) ) ;
strategy. setControllerMappingHyphenStyle ( true ) ;
strategy. setTablePrefix ( pc. getModuleName ( ) + "_" ) ;
mpg. setStrategy ( strategy) ;
}
}