Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
53583b20
Commit
53583b20
authored
Nov 27, 2019
by
zhanghouying
Browse files
Merge branches '2.4dev' and '2.4opt' of
https://gitee.com/elunez/eladmin
into 2.4opt
parents
1cec669c
2ecb82a5
Changes
7
Hide whitespace changes
Inline
Side-by-side
eladmin-common/pom.xml
View file @
53583b20
...
...
@@ -9,7 +9,7 @@
</parent>
<modelVersion>
4.0.0
</modelVersion>
<properties>
<hutool.version>
[4.1.12,)
</hutool.version>
<hutool.version>
5.0.6
</hutool.version>
</properties>
<artifactId>
eladmin-common
</artifactId>
...
...
eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java
View file @
53583b20
...
...
@@ -6,9 +6,11 @@ import cn.hutool.core.util.IdUtil;
import
cn.hutool.poi.excel.BigExcelWriter
;
import
cn.hutool.poi.excel.ExcelUtil
;
import
me.zhengjie.exception.BadRequestException
;
import
org.apache.poi.util.IOUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.activation.MimetypesFileTypeMap
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
import
java.security.MessageDigest
;
...
...
@@ -273,6 +275,37 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
return
null
;
}
/**
* 下载文件
* @param request /
* @param response /
* @param file /
*/
public
static
void
downloadFile
(
HttpServletRequest
request
,
HttpServletResponse
response
,
File
file
,
boolean
deleteOnExit
){
response
.
setCharacterEncoding
(
request
.
getCharacterEncoding
());
response
.
setContentType
(
"application/octet-stream"
);
FileInputStream
fis
=
null
;
try
{
fis
=
new
FileInputStream
(
file
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename="
+
file
.
getName
());
IOUtils
.
copy
(
fis
,
response
.
getOutputStream
());
response
.
flushBuffer
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
fis
!=
null
)
{
try
{
fis
.
close
();
if
(
deleteOnExit
){
file
.
deleteOnExit
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
static
String
getMd5
(
File
file
)
{
return
getMd5
(
getByte
(
file
));
}
...
...
eladmin-generator/src/main/java/me/zhengjie/rest/GeneratorController.java
View file @
53583b20
...
...
@@ -11,6 +11,8 @@ import org.springframework.beans.factory.annotation.Value;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.List
;
/**
...
...
@@ -67,15 +69,20 @@ public class GeneratorController {
@ApiOperation
(
"生成代码"
)
@PostMapping
(
value
=
"/{tableName}/{type}"
)
public
ResponseEntity
generator
(
@PathVariable
String
tableName
,
@PathVariable
Integer
type
){
if
(!
generatorEnabled
){
throw
new
BadRequestException
(
"此环境不允许生成代码!"
);
public
ResponseEntity
generator
(
@PathVariable
String
tableName
,
@PathVariable
Integer
type
,
HttpServletRequest
request
,
HttpServletResponse
response
){
if
(!
generatorEnabled
&&
type
==
0
){
throw
new
BadRequestException
(
"此环境不允许生成代码
,请选择预览或者下载查看
!"
);
}
switch
(
type
){
// 生成代码
case
0
:
generatorService
.
generator
(
genConfigService
.
find
(
tableName
),
generatorService
.
getColumns
(
tableName
));
break
;
default
:
break
;
// 预览
case
1
:
return
generatorService
.
preview
(
genConfigService
.
find
(
tableName
),
generatorService
.
getColumns
(
tableName
));
// 打包
case
2
:
generatorService
.
download
(
genConfigService
.
find
(
tableName
),
generatorService
.
getColumns
(
tableName
),
request
,
response
);
break
;
default
:
throw
new
BadRequestException
(
"没有这个选项"
);
}
return
new
ResponseEntity
(
HttpStatus
.
OK
);
}
...
...
eladmin-generator/src/main/java/me/zhengjie/service/GeneratorService.java
View file @
53583b20
...
...
@@ -2,8 +2,10 @@ package me.zhengjie.service;
import
me.zhengjie.domain.GenConfig
;
import
me.zhengjie.domain.ColumnInfo
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.scheduling.annotation.Async
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.List
;
/**
...
...
@@ -50,7 +52,23 @@ public interface GeneratorService {
* 代码生成
* @param genConfig 配置信息
* @param columns 字段信息
*/
void
generator
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
);
/**
* 预览
* @param genConfig 配置信息
* @param columns 字段信息
* @return /
*/
Object
generator
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
);
ResponseEntity
preview
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
);
/**
* 打包下载
* @param genConfig 配置信息
* @param columns 字段信息
* @param request
* @param response
*/
void
download
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
,
HttpServletRequest
request
,
HttpServletResponse
response
);
}
eladmin-generator/src/main/java/me/zhengjie/service/impl/GeneratorServiceImpl.java
View file @
53583b20
...
...
@@ -2,22 +2,30 @@ package me.zhengjie.service.impl;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.core.util.ZipUtil
;
import
me.zhengjie.domain.GenConfig
;
import
me.zhengjie.domain.ColumnInfo
;
import
me.zhengjie.domain.vo.TableInfo
;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.repository.ColumnInfoRepository
;
import
me.zhengjie.service.GeneratorService
;
import
me.zhengjie.utils.FileUtil
;
import
me.zhengjie.utils.GenUtil
;
import
me.zhengjie.utils.PageUtil
;
import
me.zhengjie.utils.StringUtils
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
javax.persistence.EntityManager
;
import
javax.persistence.PersistenceContext
;
import
javax.persistence.Query
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author Zheng Jie
...
...
@@ -113,17 +121,39 @@ public class GeneratorServiceImpl implements GeneratorService {
}
@Override
public
Object
generator
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
)
{
public
void
generator
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
)
{
if
(
genConfig
.
getId
()
==
null
){
throw
new
BadRequestException
(
"请先配置生成器"
);
}
try
{
// 查询是否存在关联实体字段信息
GenUtil
.
generatorCode
(
columns
,
genConfig
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
throw
new
BadRequestException
(
"生成失败,请手动处理已生成的文件"
);
}
return
null
;
}
@Override
public
ResponseEntity
preview
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
)
{
if
(
genConfig
.
getId
()
==
null
){
throw
new
BadRequestException
(
"请先配置生成器"
);
}
List
<
Map
<
String
,
Object
>>
genList
=
GenUtil
.
preview
(
columns
,
genConfig
);
return
new
ResponseEntity
<>(
genList
,
HttpStatus
.
OK
);
}
@Override
public
void
download
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
,
HttpServletRequest
request
,
HttpServletResponse
response
)
{
if
(
genConfig
.
getId
()
==
null
){
throw
new
BadRequestException
(
"请先配置生成器"
);
}
try
{
File
file
=
new
File
(
GenUtil
.
download
(
columns
,
genConfig
));
String
zipPath
=
file
.
getPath
()
+
".zip"
;
ZipUtil
.
zip
(
file
.
getPath
(),
zipPath
);
FileUtil
.
downloadFile
(
request
,
response
,
new
File
(
zipPath
),
true
);
}
catch
(
IOException
e
)
{
throw
new
BadRequestException
(
"打包失败"
);
}
}
}
eladmin-generator/src/main/java/me/zhengjie/utils/GenUtil.java
View file @
53583b20
...
...
@@ -11,10 +11,7 @@ import java.io.FileWriter;
import
java.io.IOException
;
import
java.io.Writer
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.*
;
/**
* 代码生成
...
...
@@ -42,11 +39,11 @@ public class GenUtil {
templateNames
.
add
(
"Entity"
);
templateNames
.
add
(
"Dto"
);
templateNames
.
add
(
"Mapper"
);
templateNames
.
add
(
"Repository"
);
templateNames
.
add
(
"Controller"
);
templateNames
.
add
(
"QueryCriteria"
);
templateNames
.
add
(
"Service"
);
templateNames
.
add
(
"ServiceImpl"
);
templateNames
.
add
(
"QueryCriteria"
);
templateNames
.
add
(
"Controller"
);
templateNames
.
add
(
"Repository"
);
return
templateNames
;
}
...
...
@@ -56,12 +53,115 @@ public class GenUtil {
*/
private
static
List
<
String
>
getFrontTemplateNames
()
{
List
<
String
>
templateNames
=
new
ArrayList
<>();
templateNames
.
add
(
"api"
);
templateNames
.
add
(
"index"
);
templateNames
.
add
(
"api"
);
return
templateNames
;
}
public
static
List
<
Map
<
String
,
Object
>>
preview
(
List
<
ColumnInfo
>
columns
,
GenConfig
genConfig
)
{
Map
<
String
,
Object
>
genMap
=
getGenMap
(
columns
,
genConfig
);
List
<
Map
<
String
,
Object
>>
genList
=
new
ArrayList
<>();
// 获取后端模版
List
<
String
>
templates
=
getAdminTemplateNames
();
TemplateEngine
engine
=
TemplateUtil
.
createEngine
(
new
TemplateConfig
(
"template"
,
TemplateConfig
.
ResourceMode
.
CLASSPATH
));
for
(
String
templateName
:
templates
)
{
Map
<
String
,
Object
>
map
=
new
HashMap
<>(
1
);
Template
template
=
engine
.
getTemplate
(
"generator/admin/"
+
templateName
+
".ftl"
);
map
.
put
(
"content"
,
template
.
render
(
genMap
));
map
.
put
(
"name"
,
templateName
);
genList
.
add
(
map
);
}
// 获取前端模版
templates
=
getFrontTemplateNames
();
for
(
String
templateName
:
templates
)
{
Map
<
String
,
Object
>
map
=
new
HashMap
<>(
1
);
Template
template
=
engine
.
getTemplate
(
"generator/front/"
+
templateName
+
".ftl"
);
map
.
put
(
templateName
,
template
.
render
(
genMap
));
map
.
put
(
"content"
,
template
.
render
(
genMap
));
map
.
put
(
"name"
,
templateName
);
genList
.
add
(
map
);
}
return
genList
;
}
public
static
String
download
(
List
<
ColumnInfo
>
columns
,
GenConfig
genConfig
)
throws
IOException
{
String
tempPath
=
System
.
getProperty
(
"java.io.tmpdir"
)
+
"eladmin-gen-temp"
+
File
.
separator
+
genConfig
.
getTableName
()
+
File
.
separator
;
Map
<
String
,
Object
>
genMap
=
getGenMap
(
columns
,
genConfig
);
TemplateEngine
engine
=
TemplateUtil
.
createEngine
(
new
TemplateConfig
(
"template"
,
TemplateConfig
.
ResourceMode
.
CLASSPATH
));
// 生成后端代码
List
<
String
>
templates
=
getAdminTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/admin/"
+
templateName
+
".ftl"
);
String
filePath
=
getAdminFilePath
(
templateName
,
genConfig
,
genMap
.
get
(
"className"
).
toString
(),
tempPath
+
"eladmin"
+
File
.
separator
);
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
// 如果非覆盖生成
if
(!
genConfig
.
getCover
()
&&
FileUtil
.
exist
(
file
)){
continue
;
}
// 生成代码
genFile
(
file
,
template
,
genMap
);
}
// 生成前端代码
templates
=
getFrontTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/front/"
+
templateName
+
".ftl"
);
String
path
=
tempPath
+
"eladmin-web"
+
File
.
separator
;
String
apiPath
=
path
+
"src"
+
File
.
separator
+
"api"
+
File
.
separator
;
String
srcPath
=
path
+
"src"
+
File
.
separator
+
"views"
+
File
.
separator
+
genMap
.
get
(
"changeClassName"
).
toString
()
+
File
.
separator
;
String
filePath
=
getFrontFilePath
(
templateName
,
apiPath
,
srcPath
,
genMap
.
get
(
"changeClassName"
).
toString
());
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
// 如果非覆盖生成
if
(!
genConfig
.
getCover
()
&&
FileUtil
.
exist
(
file
)){
continue
;
}
// 生成代码
genFile
(
file
,
template
,
genMap
);
}
return
tempPath
;
}
public
static
void
generatorCode
(
List
<
ColumnInfo
>
columnInfos
,
GenConfig
genConfig
)
throws
IOException
{
Map
<
String
,
Object
>
genMap
=
getGenMap
(
columnInfos
,
genConfig
);
TemplateEngine
engine
=
TemplateUtil
.
createEngine
(
new
TemplateConfig
(
"template"
,
TemplateConfig
.
ResourceMode
.
CLASSPATH
));
// 生成后端代码
List
<
String
>
templates
=
getAdminTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/admin/"
+
templateName
+
".ftl"
);
String
filePath
=
getAdminFilePath
(
templateName
,
genConfig
,
genMap
.
get
(
"className"
).
toString
(),
System
.
getProperty
(
"user.dir"
));
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
// 如果非覆盖生成
if
(!
genConfig
.
getCover
()
&&
FileUtil
.
exist
(
file
)){
continue
;
}
// 生成代码
genFile
(
file
,
template
,
genMap
);
}
// 生成前端代码
templates
=
getFrontTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/front/"
+
templateName
+
".ftl"
);
String
filePath
=
getFrontFilePath
(
templateName
,
genConfig
.
getApiPath
(),
genConfig
.
getPath
(),
genMap
.
get
(
"changeClassName"
).
toString
());
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
// 如果非覆盖生成
if
(!
genConfig
.
getCover
()
&&
FileUtil
.
exist
(
file
)){
continue
;
}
// 生成代码
genFile
(
file
,
template
,
genMap
);
}
}
// 获取模版数据
private
static
Map
<
String
,
Object
>
getGenMap
(
List
<
ColumnInfo
>
columnInfos
,
GenConfig
genConfig
)
{
// 存储模版字段数据
Map
<
String
,
Object
>
genMap
=
new
HashMap
<>(
16
);
// 接口别名
...
...
@@ -165,7 +265,7 @@ public class GenUtil {
// 表单显示
listMap
.
put
(
"formShow"
,
column
.
getFormShow
());
// 表单组件类型
listMap
.
put
(
"formType"
,
column
.
getFormType
()
);
listMap
.
put
(
"formType"
,
StringUtils
.
isNotBlank
(
column
.
getFormType
())
?
column
.
getFormType
()
:
"Input"
);
// 小写开头的字段名称
listMap
.
put
(
"changeColumnName"
,
changeColumnName
);
//大写开头的字段名称
...
...
@@ -215,47 +315,14 @@ public class GenUtil {
genMap
.
put
(
"betweens"
,
betweens
);
// 保存非空字段信息
genMap
.
put
(
"isNotNullColumns"
,
isNotNullColumns
);
TemplateEngine
engine
=
TemplateUtil
.
createEngine
(
new
TemplateConfig
(
"template"
,
TemplateConfig
.
ResourceMode
.
CLASSPATH
));
// 生成后端代码
List
<
String
>
templates
=
getAdminTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/admin/"
+
templateName
+
".ftl"
);
String
filePath
=
getAdminFilePath
(
templateName
,
genConfig
,
className
);
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
// 如果非覆盖生成
if
(!
genConfig
.
getCover
()
&&
FileUtil
.
exist
(
file
)){
continue
;
}
// 生成代码
genFile
(
file
,
template
,
genMap
);
}
// 生成前端代码
templates
=
getFrontTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/front/"
+
templateName
+
".ftl"
);
String
filePath
=
getFrontFilePath
(
templateName
,
genConfig
,
genMap
.
get
(
"changeClassName"
).
toString
());
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
// 如果非覆盖生成
if
(!
genConfig
.
getCover
()
&&
FileUtil
.
exist
(
file
)){
continue
;
}
// 生成代码
genFile
(
file
,
template
,
genMap
);
}
return
genMap
;
}
/**
* 定义后端文件路径以及名称
*/
private
static
String
getAdminFilePath
(
String
templateName
,
GenConfig
genConfig
,
String
className
)
{
String
projectPath
=
System
.
getProperty
(
"user.dir"
)
+
File
.
separator
+
genConfig
.
getModuleName
();
private
static
String
getAdminFilePath
(
String
templateName
,
GenConfig
genConfig
,
String
className
,
String
rootPath
)
{
String
projectPath
=
rootPath
+
File
.
separator
+
genConfig
.
getModuleName
();
String
packagePath
=
projectPath
+
File
.
separator
+
"src"
+
File
.
separator
+
"main"
+
File
.
separator
+
"java"
+
File
.
separator
;
if
(!
ObjectUtils
.
isEmpty
(
genConfig
.
getPack
()))
{
packagePath
+=
genConfig
.
getPack
().
replace
(
"."
,
File
.
separator
)
+
File
.
separator
;
...
...
@@ -299,11 +366,10 @@ public class GenUtil {
/**
* 定义前端文件路径以及名称
*/
private
static
String
getFrontFilePath
(
String
templateName
,
GenConfig
genConfig
,
String
apiName
)
{
String
path
=
genConfig
.
getPath
();
private
static
String
getFrontFilePath
(
String
templateName
,
String
apiPath
,
String
path
,
String
apiName
)
{
if
(
"api"
.
equals
(
templateName
))
{
return
genConfig
.
getA
piPath
()
+
File
.
separator
+
apiName
+
".js"
;
return
a
piPath
+
File
.
separator
+
apiName
+
".js"
;
}
if
(
"index"
.
equals
(
templateName
))
{
...
...
sql/eladmin-gen.sql
View file @
53583b20
...
...
@@ -3,6 +3,7 @@
-- ----------------------------
INSERT
INTO
`menu`
VALUES
(
82
,
b
'0'
,
'生成配置'
,
'generator/config'
,
36
,
33
,
'dev'
,
'generator/config/:tableName'
,
b
'1'
,
b
'1'
,
'GeneratorConfig'
,
'2019-11-17 20:08:56'
,
''
,
1
);
INSERT
INTO
`menu`
VALUES
(
116
,
b
'0'
,
'生成预览'
,
'generator/preview'
,
36
,
999
,
'java'
,
'generator/preview/:tableName'
,
b
'1'
,
b
'1'
,
'Preview'
,
'2019-11-26 14:54:36'
,
NULL
,
1
);
-- ----------------------------
-- Table structure for column_config
...
...
Write
Preview
Supports
Markdown
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