Commit 9ac8eefd by ljb

优化通用查询

parent 9d183d98
......@@ -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
......@@ -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
@NoRepositoryBean
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "common_query_setting")
public class CommonQuerySetting {
......
......@@ -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
@NoRepositoryBean
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "query_db")
public class QueryDb {
......
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
......@@ -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()) {
......
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 SQLException {
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
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment