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
6d16bcda
Commit
6d16bcda
authored
Dec 09, 2019
by
zhanghouying
Browse files
新增 执行SQL脚本 功能
parent
cf211a67
Changes
2
Hide whitespace changes
Inline
Side-by-side
eladmin-system/src/main/java/me/zhengjie/modules/mnt/rest/DatabaseController.java
View file @
6d16bcda
...
...
@@ -3,15 +3,23 @@ package me.zhengjie.modules.mnt.rest;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
me.zhengjie.aop.log.Log
;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.modules.mnt.domain.Database
;
import
me.zhengjie.modules.mnt.service.DatabaseService
;
import
me.zhengjie.modules.mnt.service.dto.DatabaseDto
;
import
me.zhengjie.modules.mnt.service.dto.DatabaseQueryCriteria
;
import
me.zhengjie.modules.mnt.util.SqlUtils
;
import
me.zhengjie.utils.FileUtil
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.security.access.prepost.PreAuthorize
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.File
;
/**
* @author zhanghouying
...
...
@@ -22,6 +30,8 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping
(
"/api/database"
)
public
class
DatabaseController
{
private
String
fileSavePath
=
System
.
getProperty
(
"java.io.tmpdir"
);
private
final
DatabaseService
databaseService
;
public
DatabaseController
(
DatabaseService
databaseService
)
{
...
...
@@ -70,4 +80,23 @@ public class DatabaseController {
return
new
ResponseEntity
<>(
databaseService
.
testConnection
(
resources
),
HttpStatus
.
CREATED
);
}
@Log
(
"执行SQL脚本"
)
@ApiOperation
(
value
=
"执行SQL脚本"
)
@PostMapping
(
value
=
"/upload"
)
@PreAuthorize
(
"@el.check('database:add')"
)
public
ResponseEntity
upload
(
@RequestBody
MultipartFile
file
,
HttpServletRequest
request
)
throws
Exception
{
String
id
=
request
.
getParameter
(
"id"
);
DatabaseDto
database
=
databaseService
.
findById
(
id
);
String
fileName
=
""
;
if
(
database
!=
null
){
fileName
=
file
.
getOriginalFilename
();
File
executeFile
=
new
File
(
fileSavePath
+
fileName
);
FileUtil
.
del
(
executeFile
);
file
.
transferTo
(
executeFile
);
String
result
=
SqlUtils
.
executeFile
(
database
.
getJdbcUrl
(),
database
.
getUserName
(),
database
.
getPwd
(),
executeFile
);
return
new
ResponseEntity
<>(
result
,
HttpStatus
.
OK
);
}
else
{
throw
new
BadRequestException
(
"Database not exist"
);
}
}
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java
View file @
6d16bcda
...
...
@@ -3,15 +3,14 @@ package me.zhengjie.modules.mnt.util;
import
cn.hutool.crypto.SecureUtil
;
import
com.alibaba.druid.pool.DruidDataSource
;
import
com.alibaba.druid.util.StringUtils
;
import
com.google.common.collect.Lists
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
javax.sql.DataSource
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.io.*
;
import
java.sql.*
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
@Slf4j
...
...
@@ -55,7 +54,7 @@ public class SqlUtils {
}
if
(
StringUtils
.
isEmpty
(
className
))
{
DataTypeEnum
dataTypeEnum
=
DataTypeEnum
.
urlOf
(
jdbcUrl
);
if
(
null
==
dataTypeEnum
)
{
if
(
null
==
dataTypeEnum
)
{
throw
new
RuntimeException
(
"Not supported data type: jdbcUrl="
+
jdbcUrl
);
}
druidDataSource
.
setDriverClassName
(
dataTypeEnum
.
getDriver
());
...
...
@@ -141,23 +140,84 @@ public class SqlUtils {
public
static
boolean
testConnection
(
String
jdbcUrl
,
String
userName
,
String
password
)
{
Connection
connection
=
null
;
try
{
connection
=
getConnection
(
jdbcUrl
,
userName
,
password
);
connection
=
getConnection
(
jdbcUrl
,
userName
,
password
);
if
(
null
!=
connection
)
{
return
true
;
}
}
catch
(
Exception
e
){
log
.
info
(
"Get connection failed:"
,
e
.
getMessage
());
}
finally
{
}
catch
(
Exception
e
)
{
log
.
info
(
"Get connection failed:"
,
e
.
getMessage
());
}
finally
{
releaseConnection
(
connection
);
}
return
false
;
}
public
JdbcTemplate
jdbcTemplate
(
String
jdbcUrl
,
String
userName
,
String
password
)
{
DataSource
dataSource
=
getDataSource
(
jdbcUrl
,
userName
,
password
);
JdbcTemplate
jdbcTemplate
=
new
JdbcTemplate
(
dataSource
);
jdbcTemplate
.
setFetchSize
(
1000
);
return
jdbcTemplate
;
public
static
String
executeFile
(
String
jdbcUrl
,
String
userName
,
String
password
,
File
sqlFile
)
{
Connection
connection
=
getConnection
(
jdbcUrl
,
userName
,
password
);
try
{
batchExecute
(
connection
,
readSqlList
(
sqlFile
));
}
catch
(
Exception
e
)
{
log
.
error
(
"sql脚本执行发生异常:{}"
,
e
.
getMessage
());
return
e
.
getMessage
();
}
finally
{
releaseConnection
(
connection
);
}
return
"success"
;
}
/**
* 批量执行sql
* @param connection
* @param sqlList
* @return
*/
public
static
void
batchExecute
(
Connection
connection
,
List
<
String
>
sqlList
)
throws
SQLException
{
Statement
st
=
connection
.
createStatement
();
for
(
String
sql
:
sqlList
)
{
if
(
sql
.
endsWith
(
";"
))
{
sql
=
sql
.
substring
(
0
,
sql
.
length
()
-
1
);
}
st
.
addBatch
(
sql
);
}
st
.
executeBatch
();
}
/**
* 将文件中的sql语句以;为单位读取到列表中
* @param sqlFile
* @return
* @throws Exception
*/
private
static
List
<
String
>
readSqlList
(
File
sqlFile
)
throws
Exception
{
List
<
String
>
sqlList
=
Lists
.
newArrayList
();
StringBuilder
sb
=
new
StringBuilder
();
BufferedReader
reader
=
null
;
try
{
reader
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
sqlFile
),
"UTF-8"
));
String
tmp
=
null
;
while
((
tmp
=
reader
.
readLine
())
!=
null
)
{
log
.
info
(
"line:{}"
,
tmp
);
if
(
tmp
.
endsWith
(
";"
))
{
sb
.
append
(
tmp
);
sqlList
.
add
(
sb
.
toString
());
sb
.
delete
(
0
,
sb
.
length
());
}
else
{
sb
.
append
(
tmp
);
}
}
if
(!
""
.
endsWith
(
sb
.
toString
().
trim
()))
{
sqlList
.
add
(
sb
.
toString
());
}
}
finally
{
try
{
reader
.
close
();
}
catch
(
IOException
e1
)
{
}
}
return
sqlList
;
}
}
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