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
9ac8eefd
Commit
9ac8eefd
authored
Aug 18, 2023
by
ljb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化通用查询
parent
9d183d98
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
40 additions
and
64 deletions
+40
-64
dataoperation-server/src/main/java/com/zq/dataoperation/controller/CommonQueryController.java
+3
-2
dataoperation-server/src/main/java/com/zq/dataoperation/entity/CommonQuerySetting.java
+2
-2
dataoperation-server/src/main/java/com/zq/dataoperation/entity/QueryDb.java
+2
-2
dataoperation-server/src/main/java/com/zq/dataoperation/holder/ConnectionHolder.java
+0
-20
dataoperation-server/src/main/java/com/zq/dataoperation/runner/DatabaseRunner.java
+4
-12
dataoperation-server/src/main/java/com/zq/dataoperation/service/CommonQueryService.java
+9
-26
dataoperation-server/src/main/java/com/zq/dataoperation/utils/ConnectionUtil.java
+20
-0
No files found.
dataoperation-server/src/main/java/com/zq/dataoperation/controller/CommonQueryController.java
View file @
9ac8eefd
...
...
@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.*;
import
javax.annotation.Resource
;
import
java.sql.SQLException
;
import
java.util.Map
;
@Api
(
tags
=
"通用查询"
)
@RestController
...
...
@@ -48,8 +49,7 @@ public class CommonQueryController {
@ApiOperation
(
"运行查询"
)
@PostMapping
(
"/run/{queryId}"
)
public
ResultVo
run
(
@PathVariable
(
"queryId"
)
Long
queryId
,
@RequestBody
String
body
)
throws
Exception
{
System
.
out
.
println
(
body
);
public
ResultVo
run
(
@PathVariable
(
"queryId"
)
Long
queryId
,
@RequestBody
Map
<
String
,
String
>
body
)
throws
SQLException
{
return
ResultVo
.
success
(
commonQueryService
.
run
(
queryId
,
body
));
}
}
\ No newline at end of file
dataoperation-server/src/main/java/com/zq/dataoperation/entity/CommonQuerySetting.java
View file @
9ac8eefd
...
...
@@ -3,12 +3,12 @@ package com.zq.dataoperation.entity;
import
com.baomidou.mybatisplus.annotation.*
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
org.springframework.data.repository.NoRepositoryBean
;
import
lombok.NoArgsConstructor
;
import
java.util.Date
;
@Data
@No
RepositoryBean
@No
ArgsConstructor
@AllArgsConstructor
@TableName
(
value
=
"common_query_setting"
)
public
class
CommonQuerySetting
{
...
...
dataoperation-server/src/main/java/com/zq/dataoperation/entity/QueryDb.java
View file @
9ac8eefd
...
...
@@ -3,12 +3,12 @@ package com.zq.dataoperation.entity;
import
com.baomidou.mybatisplus.annotation.*
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
org.springframework.data.repository.NoRepositoryBean
;
import
lombok.NoArgsConstructor
;
import
java.util.Date
;
@Data
@No
RepositoryBean
@No
ArgsConstructor
@AllArgsConstructor
@TableName
(
value
=
"query_db"
)
public
class
QueryDb
{
...
...
dataoperation-server/src/main/java/com/zq/dataoperation/holder/ConnectionHolder.java
deleted
100644 → 0
View file @
9d183d98
package
com
.
zq
.
dataoperation
.
holder
;
import
com.alibaba.druid.pool.DruidPooledConnection
;
import
java.util.HashMap
;
import
java.util.Map
;
public
class
ConnectionHolder
{
private
Map
<
Long
,
DruidPooledConnection
>
connectionMap
=
new
HashMap
<>();
public
ConnectionHolder
(
Map
<
Long
,
DruidPooledConnection
>
connectionMap
)
{
this
.
connectionMap
=
connectionMap
;
}
public
DruidPooledConnection
get
(
Long
id
){
return
connectionMap
.
get
(
id
);
}
}
\ No newline at end of file
dataoperation-server/src/main/java/com/zq/dataoperation/runner/DatabaseRunner.java
View file @
9ac8eefd
...
...
@@ -5,19 +5,16 @@ 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
;
import
com.zq.dataoperation.
holder.ConnectionHolder
;
import
com.zq.dataoperation.
utils.ConnectionUtil
;
import
com.zq.dataoperation.service.CommonQueryService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PreDestroy
;
import
javax.annotation.Resource
;
import
java.sql.Connection
;
import
javax.annotation.Resource
;;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -59,17 +56,12 @@ public class DatabaseRunner implements ApplicationRunner {
druidDataSource
.
setUsername
(
ds
.
getUsername
());
druidDataSource
.
setPassword
(
ds
.
getPassword
());
druidDataSource
.
init
();
DruidPooledConnection
connection
=
druidDataSource
.
getConnection
();
connectionMap
.
put
(
ds
.
getId
(),
connection
);
connectionMap
.
put
(
ds
.
getId
(),
druidDataSource
.
getConnection
());
}
ConnectionUtil
.
set
(
connectionMap
);
log
.
info
(
"成功加载数据库:"
+
connectionMap
.
size
());
}
@Bean
public
ConnectionHolder
getConnectionHolder
(){
return
new
ConnectionHolder
(
connectionMap
);
}
@PreDestroy
public
void
onClose
(){
for
(
DruidPooledConnection
connection
:
connectionMap
.
values
())
{
...
...
dataoperation-server/src/main/java/com/zq/dataoperation/service/CommonQueryService.java
View file @
9ac8eefd
package
com
.
zq
.
dataoperation
.
service
;
import
cn.hutool.json.JSONObject
;
import
cn.hutool.json.JSONUtil
;
import
com.alibaba.druid.pool.DruidPooledConnection
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.zq.common.exception.BusinessException
;
...
...
@@ -10,11 +7,11 @@ import com.zq.dataoperation.dao.CommonQuerySettingDao;
import
com.zq.dataoperation.dao.QueryDbDao
;
import
com.zq.dataoperation.entity.CommonQuerySetting
;
import
com.zq.dataoperation.entity.QueryDb
;
import
com.zq.dataoperation.
holder.ConnectionHolder
;
import
com.zq.dataoperation.
utils.ConnectionUtil
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.sql.
PreparedStatement
;
import
java.sql.
Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
...
...
@@ -30,9 +27,6 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
@Resource
private
QueryDbDao
queryDbDao
;
@Resource
private
ConnectionHolder
connectionHolder
;
public
List
<
QueryDb
>
getDatasource
(){
return
queryDbDao
.
selectList
(
new
QueryWrapper
<>());
}
...
...
@@ -53,28 +47,25 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
}
}
public
List
<
Map
>
run
(
Long
queryId
,
String
body
)
throws
Exception
{
public
List
<
Map
>
run
(
Long
queryId
,
Map
<
String
,
String
>
body
)
throws
SQL
Exception
{
CommonQuerySetting
querySetting
=
getById
(
queryId
);
QueryDb
queryDb
=
queryDbDao
.
selectById
(
querySetting
.
getQueryDbId
());
String
sql
=
querySetting
.
getQuerySql
();
JSONObject
jsonObject
=
JSONUtil
.
parseObj
(
body
);
Pattern
pattern
=
Pattern
.
compile
(
"\\{([^}]+)}"
);
Matcher
matcher
=
pattern
.
matcher
(
sql
);
while
(
matcher
.
find
())
{
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
));
}
}
ResultSet
resultSet
=
null
;
try
{
DruidPooledConnection
connection
=
connectionHolder
.
get
(
queryDb
.
getId
());
resultSet
=
connection
.
prepareStatement
(
sql
).
executeQuery
();
Connection
connection
=
ConnectionUtil
.
get
(
queryDb
.
getId
());
ResultSet
resultSet
=
connection
.
prepareStatement
(
sql
).
executeQuery
();
int
columnCount
=
resultSet
.
getMetaData
().
getColumnCount
();
List
<
Map
>
results
=
new
ArrayList
<>();
while
(
resultSet
.
next
())
{
while
(
resultSet
.
next
())
{
Map
<
String
,
String
>
map
=
new
HashMap
<>(
columnCount
);
for
(
int
i
=
1
;
i
<=
columnCount
;
i
++)
{
map
.
put
(
resultSet
.
getMetaData
().
getColumnName
(
i
),
resultSet
.
getString
(
i
));
...
...
@@ -82,12 +73,5 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
results
.
add
(
map
);
}
return
results
;
}
catch
(
Exception
e
)
{
throw
new
Exception
(
e
);
}
finally
{
if
(
resultSet
!=
null
)
{
resultSet
.
close
();
}
}
}
}
\ No newline at end of file
dataoperation-server/src/main/java/com/zq/dataoperation/utils/ConnectionUtil.java
0 → 100644
View file @
9ac8eefd
package
com
.
zq
.
dataoperation
.
utils
;
import
com.alibaba.druid.pool.DruidPooledConnection
;
import
java.sql.Connection
;
import
java.util.Map
;
public
class
ConnectionUtil
{
private
static
Map
<
Long
,
DruidPooledConnection
>
connectionMap
;
public
static
void
set
(
Map
<
Long
,
DruidPooledConnection
>
map
)
{
connectionMap
=
map
;
}
public
static
Connection
get
(
Long
id
){
return
connectionMap
.
get
(
id
).
getConnection
();
}
}
\ 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