Commit 73c80096 by ljb

数据库连接添加JdbcTemplate封装

parent 7e1db985
...@@ -95,8 +95,7 @@ public class CommonQueryController { ...@@ -95,8 +95,7 @@ public class CommonQueryController {
@ApiOperation("运行查询") @ApiOperation("运行查询")
@PostMapping("/run") @PostMapping("/run")
public ResultVo run(@RequestBody Map<String, Object> body) throws Exception { public ResultVo run(@RequestBody Map<String, Object> body) {
System.out.println(body);
return ResultVo.success(commonQueryService.run(body)); return ResultVo.success(commonQueryService.run(body));
} }
......
package com.zq.dataoperation.runner; package com.zq.dataoperation.runner;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.zq.common.exception.BusinessException; import com.zq.common.exception.BusinessException;
import com.zq.dataoperation.entity.QueryDb; import com.zq.dataoperation.entity.QueryDb;
...@@ -26,7 +25,7 @@ public class DatabaseRunner implements ApplicationRunner { ...@@ -26,7 +25,7 @@ public class DatabaseRunner implements ApplicationRunner {
@Resource @Resource
private CommonQueryService service; private CommonQueryService service;
private Map<Integer, DruidPooledConnection> connectionMap = new HashMap<>(); private Map<Integer, DruidDataSource> dataSourceMap = new HashMap<>();
@Override @Override
public void run(ApplicationArguments args) throws SQLException { public void run(ApplicationArguments args) throws SQLException {
...@@ -56,20 +55,16 @@ public class DatabaseRunner implements ApplicationRunner { ...@@ -56,20 +55,16 @@ public class DatabaseRunner implements ApplicationRunner {
druidDataSource.setUsername(ds.getUsername()); druidDataSource.setUsername(ds.getUsername());
druidDataSource.setPassword(ds.getPassword()); druidDataSource.setPassword(ds.getPassword());
druidDataSource.init(); druidDataSource.init();
connectionMap.put(ds.getId(), druidDataSource.getConnection()); dataSourceMap.put(ds.getId(), druidDataSource);
} }
ConnectionUtil.set(connectionMap); ConnectionUtil.set(dataSourceMap);
log.info("成功加载数据库:{}个", connectionMap.size()); log.info("成功加载数据库:{}个", dataSourceMap.size());
} }
@PreDestroy @PreDestroy
public void onClose(){ public void onClose(){
for (DruidPooledConnection connection : connectionMap.values()) { for (DruidDataSource dataSource : dataSourceMap.values()) {
try { dataSource.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }
} }
\ No newline at end of file
...@@ -18,13 +18,13 @@ import com.zq.dataoperation.utils.ConnectionUtil; ...@@ -18,13 +18,13 @@ import com.zq.dataoperation.utils.ConnectionUtil;
import com.zq.dataoperation.utils.IdentityUtils; import com.zq.dataoperation.utils.IdentityUtils;
import com.zq.dataoperation.utils.SqlUtils; import com.zq.dataoperation.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import java.sql.*; import java.sql.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -62,9 +62,8 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo ...@@ -62,9 +62,8 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
} }
} }
public List<Map> run(Map<String, Object> body) throws Exception { public List<Map<String, Object>> run(Map<String, Object> body) {
JSONObject jsonObject = JSONUtil.parseObj(body); CommonQuerySetting querySetting = getById(body.get("queryId").toString());
CommonQuerySetting querySetting = getById(jsonObject.get("queryId").toString());
QueryDb queryDb = queryDbDao.selectById(querySetting.getQueryDbId()); QueryDb queryDb = queryDbDao.selectById(querySetting.getQueryDbId());
String sql = querySetting.getQuerySql(); String sql = querySetting.getQuerySql();
...@@ -72,11 +71,11 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo ...@@ -72,11 +71,11 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
Matcher matcher = pattern.matcher(sql); Matcher matcher = pattern.matcher(sql);
while (matcher.find()) { while (matcher.find()) {
String group = matcher.group(1); String group = matcher.group(1);
if (jsonObject.containsKey(group)) { if (body.containsKey(group)) {
sql = sql.replace("{" + group + "}", jsonObject.getStr(group)); sql = sql.replace("{" + group + "}", body.get(group).toString());
} }
} }
return commonQuery(sql, queryDb.getId()); return commonQueryByJdbcTemplate(sql, queryDb.getId());
} }
public Object checkConnect(QueryDb queryDb) { public Object checkConnect(QueryDb queryDb) {
...@@ -141,7 +140,7 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo ...@@ -141,7 +140,7 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
public List<Map> commonQuery(String sql, Integer queryId) throws Exception { public List<Map> commonQuery(String sql, Integer queryId) throws Exception {
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
Connection connection = ConnectionUtil.get(queryId); Connection connection = ConnectionUtil.getConnection(queryId);
connection.prepareStatement(sql).setQueryTimeout(5000); connection.prepareStatement(sql).setQueryTimeout(5000);
resultSet = connection.prepareStatement(sql).executeQuery(); resultSet = connection.prepareStatement(sql).executeQuery();
int columnCount = resultSet.getMetaData().getColumnCount(); int columnCount = resultSet.getMetaData().getColumnCount();
...@@ -163,6 +162,11 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo ...@@ -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 { public Object getTables(Integer id) throws Exception {
QueryDb queryDb = queryDbDao.selectById(id); QueryDb queryDb = queryDbDao.selectById(id);
List<Map> results = null; List<Map> results = null;
......
package com.zq.dataoperation.utils; 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.Connection;
import java.sql.SQLException;
import java.util.Map; import java.util.Map;
public class ConnectionUtil { public class ConnectionUtil {
private static Map<Integer, DruidPooledConnection> connectionMap; private static Map<Integer, DruidDataSource> dataSourceMap;
public static void set(Map<Integer, DruidPooledConnection> map) { public static void set(Map<Integer, DruidDataSource> map) {
connectionMap = map; dataSourceMap = map;
} }
public static Connection get(Integer id){ public static Connection getConnection(Integer id){
return connectionMap.get(id).getConnection(); 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