import java.util.ArrayList;
import
java
.
util
.
List
;
import
java
.
util
.
Scanner
;
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
.
DataSourceConfig
;
import
com
.
baomidou
.
mybatisplus
.
generator
.
config
.
FileOutConfig
;
import
com
.
baomidou
.
mybatisplus
.
generator
.
config
.
GlobalConfig
;
import
com
.
baomidou
.
mybatisplus
.
generator
.
config
.
PackageConfig
;
import
com
.
baomidou
.
mybatisplus
.
generator
.
config
.
StrategyConfig
;
import
com
.
baomidou
.
mybatisplus
.
generator
.
config
.
TemplateConfig
;
import
com
.
baomidou
.
mybatisplus
.
generator
.
config
.
po
.
TableInfo
;
import
com
.
baomidou
.
mybatisplus
.
generator
.
config
.
rules
.
NamingStrategy
;
import
com
.
baomidou
.
mybatisplus
.
generator
.
engine
.
FreemarkerTemplateEngine
;
/**
@作者:zhangrongshan
* mysql
代码⽣成器演示例⼦
*/
public class
MysqlGenerator
{
/**
* <p>
*
读取控制台内容
* </p>
*/
public 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
+
"
!
"
);
}
/**
* RUN THIS
*/
public static
void
main
(
String
[]
args
) {
//
代码⽣成器
AutoGenerator mpg
=
new
AutoGenerator
();
//
全局配置
GlobalConfig gc
=
new
GlobalConfig
();
String
projectPath
=
System
.
getProperty
(
"user.dir"
);
gc
.
setOutputDir
(
projectPath
+
"/src/main/java"
);
gc
.
setAuthor
(
"lagou"
);
gc
.
setOpen
(
false
);
mpg
.
setGlobalConfig
(
gc
);
//
数据源配置
DataSourceConfig dsc
=
new
DataSourceConfig
();
dsc
.
setUrl
(
"jdbc:mysql://127.0.0.1:3306/mp?
useUnicode=true&useSSL=false&characterEncoding=utf8"
);
// dsc.setSchemaName("public");
dsc
.
setDriverName
(
"com.mysql.jdbc.Driver"
);
dsc
.
setUsername
(
"root"
);
dsc
.
setPassword
(
"root"
);
mpg
.
setDataSource
(
dsc
);
//
包配置
PackageConfig pc
=
new
PackageConfig
();
pc
.
setModuleName
(
scanner
(
"
模块名
"
));
pc
.
setParent
(
"com.lagou.mp.generator"
);
mpg
.
setPackageInfo
(
pc
);
//
⾃定义配置
InjectionConfig cfg
=
new
InjectionConfig
() {
@Override
public
void
initMap
() {
// to do nothing
}
};
List
<
FileOutConfig
>
focList
=
new
ArrayList
<>
();
focList
.
add
(
new
FileOutConfig
(
"/templates/mapper.xml.ftl"
) {
@Override
public
String
outputFile
(
TableInfo tableInfo
) {
//
⾃定义输⼊⽂件名称
return
projectPath
+
"/lagou-mp
generator/src/main/resources/mapper/"
+
pc
.
getModuleName
()
+
"/"
+
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
);
//
strategy.setSuperEntityClass("com.baomidou.mybatisplus.samples.generator.commo
n.BaseEntity");
strategy
.
setEntityLombokModel
(
true
);
//
strategy.setSuperControllerClass("com.baomidou.mybatisplus.samples.generator.c
ommon.BaseController");
strategy
.
setInclude
(
scanner
(
"
表名
"
));
strategy
.
setSuperEntityColumns
(
"id"
);
strategy
.
setControllerMappingHyphenStyle
(
true
);
strategy
.
setTablePrefix
(
pc
.
getModuleName
()
+
"_"
);
mpg
.
setStrategy
(
strategy
);
//
选择
freemarker
引擎需要指定如下加,注意
pom
依赖必须有!
mpg
.
setTemplateEngine
(
new
FreemarkerTemplateEngine
());
mpg
.
execute
();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/0d18f0430e61f978fe6e37c0f7f8ba8b.png)