Commit 73c80096 by ljb

数据库连接添加JdbcTemplate封装

parent 7e1db985
......@@ -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));
}
......
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, DruidPooledConnection> connectionMap = new HashMap<>();
private Map<Integer, DruidDataSource> dataSourceMap = 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(connectionMap);
log.info("成功加载数据库:{}个", connectionMap.size());
ConnectionUtil.set(dataSourceMap);
log.info("成功加载数据库:{}个", dataSourceMap.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
......@@ -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 commonQueryByJdbcTemplate(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.getConnection(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;
......
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, DruidPooledConnection> connectionMap;
private static Map<Integer, DruidDataSource> dataSourceMap;
public static void set(Map<Integer, DruidPooledConnection> map) {
connectionMap = map;
public static void set(Map<Integer, DruidDataSource> map) {
dataSourceMap = 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
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