Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nnjcy-data-model
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ljb
nnjcy-data-model
Commits
73c80096
Commit
73c80096
authored
Oct 17, 2023
by
ljb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
数据库连接添加JdbcTemplate封装
parent
7e1db985
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
27 deletions
+37
-27
dataoperation-server/src/main/java/com/zq/dataoperation/controller/CommonQueryController.java
+1
-2
dataoperation-server/src/main/java/com/zq/dataoperation/runner/DatabaseRunner.java
+7
-11
dataoperation-server/src/main/java/com/zq/dataoperation/service/CommonQueryService.java
+12
-8
dataoperation-server/src/main/java/com/zq/dataoperation/utils/ConnectionUtil.java
+17
-6
No files found.
dataoperation-server/src/main/java/com/zq/dataoperation/controller/CommonQueryController.java
View file @
73c80096
...
...
@@ -95,8 +95,7 @@ public class CommonQueryController {
@ApiOperation
(
"运行查询"
)
@PostMapping
(
"/run"
)
public
ResultVo
run
(
@RequestBody
Map
<
String
,
Object
>
body
)
throws
Exception
{
System
.
out
.
println
(
body
);
public
ResultVo
run
(
@RequestBody
Map
<
String
,
Object
>
body
)
{
return
ResultVo
.
success
(
commonQueryService
.
run
(
body
));
}
...
...
dataoperation-server/src/main/java/com/zq/dataoperation/runner/DatabaseRunner.java
View file @
73c80096
package
com
.
zq
.
dataoperation
.
runner
;
import
com.alibaba.druid.pool.DruidDataSource
;
import
com.alibaba.druid.pool.DruidPooledConnection
;
import
com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder
;
import
com.zq.common.exception.BusinessException
;
import
com.zq.dataoperation.entity.QueryDb
;
...
...
@@ -26,7 +25,7 @@ public class DatabaseRunner implements ApplicationRunner {
@Resource
private
CommonQueryService
service
;
private
Map
<
Integer
,
Druid
PooledConnection
>
connection
Map
=
new
HashMap
<>();
private
Map
<
Integer
,
Druid
DataSource
>
dataSource
Map
=
new
HashMap
<>();
@Override
public
void
run
(
ApplicationArguments
args
)
throws
SQLException
{
...
...
@@ -56,20 +55,16 @@ public class DatabaseRunner implements ApplicationRunner {
druidDataSource
.
setUsername
(
ds
.
getUsername
());
druidDataSource
.
setPassword
(
ds
.
getPassword
());
druidDataSource
.
init
();
connectionMap
.
put
(
ds
.
getId
(),
druidDataSource
.
getConnection
()
);
dataSourceMap
.
put
(
ds
.
getId
(),
druidDataSource
);
}
ConnectionUtil
.
set
(
connection
Map
);
log
.
info
(
"成功加载数据库:{}个"
,
connection
Map
.
size
());
ConnectionUtil
.
set
(
dataSource
Map
);
log
.
info
(
"成功加载数据库:{}个"
,
dataSource
Map
.
size
());
}
@PreDestroy
public
void
onClose
(){
for
(
DruidPooledConnection
connection
:
connectionMap
.
values
())
{
try
{
connection
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
for
(
DruidDataSource
dataSource
:
dataSourceMap
.
values
())
{
dataSource
.
close
();
}
}
}
\ No newline at end of file
dataoperation-server/src/main/java/com/zq/dataoperation/service/CommonQueryService.java
View file @
73c80096
...
...
@@ -18,13 +18,13 @@ import com.zq.dataoperation.utils.ConnectionUtil;
import
com.zq.dataoperation.utils.IdentityUtils
;
import
com.zq.dataoperation.utils.SqlUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
java.sql.*
;
import
javax.annotation.Resource
;
import
javax.sql.DataSource
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
...
...
@@ -62,9 +62,8 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
}
}
public
List
<
Map
>
run
(
Map
<
String
,
Object
>
body
)
throws
Exception
{
JSONObject
jsonObject
=
JSONUtil
.
parseObj
(
body
);
CommonQuerySetting
querySetting
=
getById
(
jsonObject
.
get
(
"queryId"
).
toString
());
public
List
<
Map
<
String
,
Object
>>
run
(
Map
<
String
,
Object
>
body
)
{
CommonQuerySetting
querySetting
=
getById
(
body
.
get
(
"queryId"
).
toString
());
QueryDb
queryDb
=
queryDbDao
.
selectById
(
querySetting
.
getQueryDbId
());
String
sql
=
querySetting
.
getQuerySql
();
...
...
@@ -72,11 +71,11 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
Matcher
matcher
=
pattern
.
matcher
(
sql
);
while
(
matcher
.
find
())
{
String
group
=
matcher
.
group
(
1
);
if
(
jsonObject
.
containsKey
(
group
))
{
sql
=
sql
.
replace
(
"{"
+
group
+
"}"
,
jsonObject
.
getStr
(
group
));
if
(
body
.
containsKey
(
group
))
{
sql
=
sql
.
replace
(
"{"
+
group
+
"}"
,
body
.
get
(
group
).
toString
(
));
}
}
return
commonQuery
(
sql
,
queryDb
.
getId
());
return
commonQuery
ByJdbcTemplate
(
sql
,
queryDb
.
getId
());
}
public
Object
checkConnect
(
QueryDb
queryDb
)
{
...
...
@@ -141,7 +140,7 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
public
List
<
Map
>
commonQuery
(
String
sql
,
Integer
queryId
)
throws
Exception
{
ResultSet
resultSet
=
null
;
try
{
Connection
connection
=
ConnectionUtil
.
get
(
queryId
);
Connection
connection
=
ConnectionUtil
.
get
Connection
(
queryId
);
connection
.
prepareStatement
(
sql
).
setQueryTimeout
(
5000
);
resultSet
=
connection
.
prepareStatement
(
sql
).
executeQuery
();
int
columnCount
=
resultSet
.
getMetaData
().
getColumnCount
();
...
...
@@ -163,6 +162,11 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
}
}
public
List
<
Map
<
String
,
Object
>>
commonQueryByJdbcTemplate
(
String
sql
,
Integer
queryId
)
{
JdbcTemplate
jdbcTemplate
=
ConnectionUtil
.
getJdbcTemplate
(
queryId
);
return
jdbcTemplate
.
queryForList
(
sql
);
}
public
Object
getTables
(
Integer
id
)
throws
Exception
{
QueryDb
queryDb
=
queryDbDao
.
selectById
(
id
);
List
<
Map
>
results
=
null
;
...
...
dataoperation-server/src/main/java/com/zq/dataoperation/utils/ConnectionUtil.java
View file @
73c80096
package
com
.
zq
.
dataoperation
.
utils
;
import
com.alibaba.druid.pool.DruidPooledConnection
;
import
com.alibaba.druid.pool.DruidDataSource
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.util.Map
;
public
class
ConnectionUtil
{
private
static
Map
<
Integer
,
Druid
PooledConnection
>
connection
Map
;
private
static
Map
<
Integer
,
Druid
DataSource
>
dataSource
Map
;
public
static
void
set
(
Map
<
Integer
,
Druid
PooledConnection
>
map
)
{
connection
Map
=
map
;
public
static
void
set
(
Map
<
Integer
,
Druid
DataSource
>
map
)
{
dataSource
Map
=
map
;
}
public
static
Connection
get
(
Integer
id
){
return
connectionMap
.
get
(
id
).
getConnection
();
public
static
Connection
getConnection
(
Integer
id
){
try
{
return
dataSourceMap
.
get
(
id
).
getConnection
().
getConnection
();
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
public
static
JdbcTemplate
getJdbcTemplate
(
Integer
id
){
return
new
JdbcTemplate
(
dataSourceMap
.
get
(
id
));
}
}
\ No newline at end of file
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