Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
datax-cloud
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄营
datax-cloud
Commits
40bd3b38
Commit
40bd3b38
authored
Mar 16, 2020
by
yuwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
项目初始化
parent
53c72c3b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
160 deletions
+18
-160
Generate.java
...n/src/main/java/cn/datax/tool/codegen/utils/Generate.java
+18
-160
No files found.
datax-tools/datax-tool-codegen/src/main/java/cn/datax/tool/codegen/utils/Generate.java
View file @
40bd3b38
...
...
@@ -16,149 +16,7 @@ public class Generate {
public
static
void
main
(
String
[]
args
)
{
// 代码生成器
AutoGenerator
mpg
=
new
AutoGenerator
();
// 全局配置
GlobalConfig
gc
=
new
GlobalConfig
();
String
projectPath
=
"F://code"
;
gc
.
setOutputDir
(
projectPath
+
"/src/main/java"
);
gc
.
setAuthor
(
"yuwei"
);
// 是否打开输出目录
gc
.
setOpen
(
true
);
// 是否覆盖已有文件
gc
.
setFileOverride
(
true
);
// 开启 ActiveRecord 模式
gc
.
setActiveRecord
(
false
);
// 开启 BaseResultMap
gc
.
setBaseResultMap
(
true
);
// 开启 baseColumnList
gc
.
setBaseColumnList
(
true
);
gc
.
setEntityName
(
"%sEntity"
);
gc
.
setMapperName
(
"%sDao"
);
gc
.
setXmlName
(
"%sMapper"
);
gc
.
setServiceName
(
"%sService"
);
gc
.
setServiceImplName
(
"%sServiceImpl"
);
gc
.
setControllerName
(
"%sController"
);
// 实体属性 Swagger2 注解
gc
.
setSwagger2
(
false
);
// 指定生成的主键的ID类型
gc
.
setIdType
(
IdType
.
ASSIGN_ID
);
mpg
.
setGlobalConfig
(
gc
);
// 数据源配置
DataSourceConfig
dsc
=
new
DataSourceConfig
();
dsc
.
setUrl
(
"jdbc:mysql://localhost:3306/data_cloud?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"
);
dsc
.
setDriverName
(
"com.mysql.cj.jdbc.Driver"
);
dsc
.
setUsername
(
"root"
);
dsc
.
setPassword
(
"1234@abcd"
);
mpg
.
setDataSource
(
dsc
);
// 包配置
PackageConfig
pc
=
new
PackageConfig
();
pc
.
setParent
(
"cn.datax.service.data"
);
// 父包模块名
pc
.
setModuleName
(
"factory"
);
pc
.
setMapper
(
"dao"
);
mpg
.
setPackageInfo
(
pc
);
// 自定义配置
InjectionConfig
cfg
=
new
InjectionConfig
()
{
@Override
public
void
initMap
()
{
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"PackageParent"
,
"cn.datax.service.data"
);
this
.
setMap
(
map
);
}
};
// 自定义输出配置
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/"
+
pc
.
getModuleName
()
+
"/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Mapper"
)
+
StringPool
.
DOT_XML
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/entity.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
projectPath
+
"/src/main/java/"
+
"cn.datax.service.data"
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
pc
.
getModuleName
()
+
"/api/entity/"
+
tableInfo
.
getEntityName
()
+
StringPool
.
DOT_JAVA
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/mapstruct.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
projectPath
+
"/src/main/java/"
+
"cn.datax.service.data"
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
pc
.
getModuleName
()
+
"/mapstruct/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Mapper"
)
+
StringPool
.
DOT_JAVA
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/dto.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
projectPath
+
"/src/main/java/"
+
"cn.datax.service.data"
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
pc
.
getModuleName
()
+
"/api/dto/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Dto"
)
+
StringPool
.
DOT_JAVA
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/vo.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
projectPath
+
"/src/main/java/"
+
"cn.datax.service.data"
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
pc
.
getModuleName
()
+
"/api/vo/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Vo"
)
+
StringPool
.
DOT_JAVA
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/query.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
projectPath
+
"/src/main/java/"
+
"cn.datax.service.data"
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
pc
.
getModuleName
()
+
"/api/query/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Query"
)
+
StringPool
.
DOT_JAVA
;
}
});
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录");
return false;
}
});
*/
cfg
.
setFileOutConfigList
(
focList
);
mpg
.
setCfg
(
cfg
);
// 配置模板
TemplateConfig
templateConfig
=
new
TemplateConfig
();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
templateConfig
.
setEntity
(
null
);
templateConfig
.
setController
(
"templates/controller.java"
);
templateConfig
.
setService
(
"templates/service.java"
);
templateConfig
.
setServiceImpl
(
"templates/serviceImpl.java"
);
templateConfig
.
setMapper
(
"templates/mapper.java"
);
templateConfig
.
setXml
(
null
);
mpg
.
setTemplate
(
templateConfig
);
// 策略配置
StrategyConfig
strategy
=
new
StrategyConfig
();
strategy
.
setNaming
(
NamingStrategy
.
underline_to_camel
);
strategy
.
setColumnNaming
(
NamingStrategy
.
underline_to_camel
);
strategy
.
setSuperEntityClass
(
"cn.datax.common.base.BaseEntity"
);
// 写于父类中的公共字段
strategy
.
setSuperEntityColumns
(
new
String
[]{
"id"
,
"create_time"
,
"create_by"
,
"update_time"
,
"update_by"
,
"status"
});
strategy
.
setEntityLombokModel
(
true
);
strategy
.
setRestControllerStyle
(
true
);
// 公共父类
strategy
.
setSuperControllerClass
(
"cn.datax.common.base.BaseController"
);
strategy
.
setSuperServiceClass
(
"cn.datax.common.base.BaseService"
);
strategy
.
setSuperServiceImplClass
(
"cn.datax.common.base.BaseServiceImpl"
);
strategy
.
setSuperMapperClass
(
"cn.datax.common.base.BaseDao"
);
strategy
.
setControllerMappingHyphenStyle
(
true
);
// strategy.setTablePrefix("data_");
// strategy.setInclude(new String[]{"sys_user", "sys_role", "sys_dept", "sys_menu", "sys_post", "sys_user_role", "sys_user_dept", "sys_user_post", "sys_role_menu"});
strategy
.
setInclude
(
new
String
[]{
"data_source"
});
mpg
.
setStrategy
(
strategy
);
mpg
.
setTemplateEngine
(
new
VelocityTemplateEngine
());
mpg
.
execute
();
generateByTables
(
"F://code"
,
"factory"
,
"cn.datax.service.data"
,
new
String
[]{
"data_source"
});
}
/**
...
...
@@ -168,7 +26,7 @@ public class Generate {
* @param parentName 包名
* @param tableNames 表名
*/
private
void
generateByTables
(
String
projectPath
,
String
moduleName
,
String
parentName
,
List
<
String
>
tableNames
)
{
private
static
void
generateByTables
(
String
projectPath
,
String
moduleName
,
String
parentName
,
String
[]
tableNames
)
{
//配置数据源
DataSourceConfig
dataSourceConfig
=
getDataSourceConfig
();
//全局变量配置
...
...
@@ -178,7 +36,7 @@ public class Generate {
// 策略配置
StrategyConfig
strategyConfig
=
getStrategyConfig
(
tableNames
);
//自定义配置
InjectionConfig
injectionConfig
=
getInjectionConfig
(
moduleName
,
parentName
);
InjectionConfig
injectionConfig
=
getInjectionConfig
(
projectPath
,
moduleName
,
parentName
);
//配置模板
TemplateConfig
templateConfig
=
getTemplateConfig
();
//自动生成
...
...
@@ -195,7 +53,7 @@ public class Generate {
* @param injectionConfig 自定义配置
* @param templateConfig 模板配置
*/
private
void
atuoGenerator
(
DataSourceConfig
dataSourceConfig
,
StrategyConfig
strategyConfig
,
GlobalConfig
globalConfig
,
PackageConfig
packageConfig
,
private
static
void
atuoGenerator
(
DataSourceConfig
dataSourceConfig
,
StrategyConfig
strategyConfig
,
GlobalConfig
globalConfig
,
PackageConfig
packageConfig
,
InjectionConfig
injectionConfig
,
TemplateConfig
templateConfig
)
{
new
AutoGenerator
()
.
setGlobalConfig
(
globalConfig
)
...
...
@@ -213,7 +71,7 @@ public class Generate {
*
* @return templateConfig
*/
private
InjectionConfig
getInjectionConfig
(
String
moduleName
,
String
parentName
)
{
private
static
InjectionConfig
getInjectionConfig
(
String
projectPath
,
String
moduleName
,
String
parentName
)
{
InjectionConfig
cfg
=
new
InjectionConfig
()
{
@Override
public
void
initMap
()
{
...
...
@@ -226,37 +84,37 @@ public class Generate {
focList
.
add
(
new
FileOutConfig
(
"/templates/mapper.xml.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
System
.
getProperty
(
"user.dir"
)
+
"/src/main/resources/mapper/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Mapper"
)
+
StringPool
.
DOT_XML
;
return
projectPath
+
"/src/main/resources/mapper/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Mapper"
)
+
StringPool
.
DOT_XML
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/entity.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
System
.
getProperty
(
"user.dir"
)
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/api/entity/"
+
tableInfo
.
getEntityName
()
+
StringPool
.
DOT_JAVA
;
return
projectPath
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/api/entity/"
+
tableInfo
.
getEntityName
()
+
StringPool
.
DOT_JAVA
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/mapstruct.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
System
.
getProperty
(
"user.dir"
)
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/mapstruct/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Mapper"
)
+
StringPool
.
DOT_JAVA
;
return
projectPath
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/mapstruct/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Mapper"
)
+
StringPool
.
DOT_JAVA
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/dto.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
System
.
getProperty
(
"user.dir"
)
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/api/dto/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Dto"
)
+
StringPool
.
DOT_JAVA
;
return
projectPath
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/api/dto/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Dto"
)
+
StringPool
.
DOT_JAVA
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/vo.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
System
.
getProperty
(
"user.dir"
)
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/api/vo/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Vo"
)
+
StringPool
.
DOT_JAVA
;
return
projectPath
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/api/vo/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Vo"
)
+
StringPool
.
DOT_JAVA
;
}
});
focList
.
add
(
new
FileOutConfig
(
"/templates/query.java.vm"
)
{
@Override
public
String
outputFile
(
TableInfo
tableInfo
)
{
return
System
.
getProperty
(
"user.dir"
)
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/api/query/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Query"
)
+
StringPool
.
DOT_JAVA
;
return
projectPath
+
"/src/main/java/"
+
parentName
.
replace
(
"."
,
File
.
separator
)
+
File
.
separator
+
moduleName
+
"/api/query/"
+
tableInfo
.
getEntityName
().
replace
(
"Entity"
,
"Query"
)
+
StringPool
.
DOT_JAVA
;
}
});
cfg
.
setFileOutConfigList
(
focList
);
...
...
@@ -268,7 +126,7 @@ public class Generate {
*
* @return templateConfig
*/
private
TemplateConfig
getTemplateConfig
()
{
private
static
TemplateConfig
getTemplateConfig
()
{
return
new
TemplateConfig
()
.
setEntity
(
null
)
.
setController
(
"templates/controller.java"
)
...
...
@@ -285,7 +143,7 @@ public class Generate {
* @param parentName 父路径包名 如:cn.datax.service
* @return PackageConfig 包名配置
*/
private
PackageConfig
getPackageConfig
(
String
moduleName
,
String
parentName
)
{
private
static
PackageConfig
getPackageConfig
(
String
moduleName
,
String
parentName
)
{
return
new
PackageConfig
()
.
setModuleName
(
moduleName
)
.
setParent
(
parentName
)
...
...
@@ -302,10 +160,10 @@ public class Generate {
*
* @return GlobalConfig
*/
private
GlobalConfig
getGlobalConfig
(
String
projectPath
)
{
private
static
GlobalConfig
getGlobalConfig
(
String
projectPath
)
{
return
new
GlobalConfig
()
//是否打开输出目录
.
setOpen
(
fals
e
)
.
setOpen
(
tru
e
)
//开启 baseColumnList
.
setBaseColumnList
(
true
)
//开启 BaseResultMap
...
...
@@ -335,7 +193,7 @@ public class Generate {
* @param tableNames 表名
* @return StrategyConfig
*/
private
StrategyConfig
getStrategyConfig
(
List
<
String
>
tableNames
)
{
private
static
StrategyConfig
getStrategyConfig
(
String
[]
tableNames
)
{
StrategyConfig
strategyConfig
=
new
StrategyConfig
()
//从数据库表到文件的命名策略
.
setNaming
(
NamingStrategy
.
underline_to_camel
)
...
...
@@ -347,7 +205,7 @@ public class Generate {
.
setTablePrefix
(
"sys_"
)
//写于父类中的公共字段
.
setSuperEntityColumns
(
new
String
[]{
"id"
,
"create_time"
,
"create_by"
,
"update_time"
,
"update_by"
,
"status"
})
.
setInclude
(
tableNames
.
toArray
(
new
String
[
tableNames
.
size
()])
)
.
setInclude
(
tableNames
)
//公共父类
.
setSuperEntityClass
(
"cn.datax.common.base.BaseEntity"
)
.
setSuperServiceClass
(
"cn.datax.common.base.BaseService"
)
...
...
@@ -362,7 +220,7 @@ public class Generate {
*
* @return 数据源配置 DataSourceConfig
*/
private
DataSourceConfig
getDataSourceConfig
()
{
private
static
DataSourceConfig
getDataSourceConfig
()
{
return
new
DataSourceConfig
()
.
setUrl
(
"jdbc:mysql://localhost:3306/data_cloud?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"
)
.
setDriverName
(
"com.mysql.cj.jdbc.Driver"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment