|
@@ -0,0 +1,159 @@
|
|
|
+package cn.com.taiji.cql.service.impl;
|
|
|
+import cn.com.taiji.common.domain.PageResult;
|
|
|
+import cn.com.taiji.common.enums.CommonConstant;
|
|
|
+import cn.com.taiji.cql.service.ISqlService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.geotools.data.jdbc.datasource.DataSourceFinder;
|
|
|
+import org.geotools.jdbc.JDBCDataStoreFactory;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.sql.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class SqlServiceImpl implements ISqlService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult sqlQuery4Mysql(Map<String, Object> mapParams, String sql, Object[] bindArgs) {
|
|
|
+
|
|
|
+
|
|
|
+ //查询记录总数
|
|
|
+ Long count =0l;
|
|
|
+
|
|
|
+ //当前页数
|
|
|
+ Long pageNumber= CommonConstant.PAGE_NUMBER;
|
|
|
+
|
|
|
+ //一页显示多少条记录
|
|
|
+ Long pageSize=CommonConstant.PAGE_SIZE;
|
|
|
+
|
|
|
+ //总页数
|
|
|
+ Long pageCount =0l;
|
|
|
+
|
|
|
+ List<Map<String, Object>> datas = new ArrayList<>();
|
|
|
+
|
|
|
+ String host = mapParams.get(JDBCDataStoreFactory.HOST.key).toString();
|
|
|
+
|
|
|
+ String database = mapParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
|
|
|
+
|
|
|
+ DataSource dataSource = null;
|
|
|
+
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ dataSource = DataSourceFinder.getDataSource(mapParams);
|
|
|
+
|
|
|
+ if (dataSource != null) {
|
|
|
+
|
|
|
+ log.info("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
|
|
|
+
|
|
|
+ connection = dataSource.getConnection();
|
|
|
+
|
|
|
+ preparedStatement = connection.prepareStatement(sql);
|
|
|
+ if (bindArgs != null) {
|
|
|
+ /**设置sql占位符中的值**/
|
|
|
+ for (int i = 0; i < bindArgs.length; i++) {
|
|
|
+ preparedStatement.setObject(i + 1, bindArgs[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println(getExecSQL(sql, bindArgs));
|
|
|
+
|
|
|
+ /**执行sql语句,获取结果集**/
|
|
|
+
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+
|
|
|
+ datas = getDatas(resultSet);
|
|
|
+
|
|
|
+ }
|
|
|
+ }catch (Exception exception){
|
|
|
+
|
|
|
+ exception.printStackTrace();
|
|
|
+ log.error("系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
|
|
|
+
|
|
|
+ }finally {
|
|
|
+
|
|
|
+ if (resultSet != null){
|
|
|
+ try{
|
|
|
+ resultSet.close();
|
|
|
+ }catch (Exception ex){
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (preparedStatement != null){
|
|
|
+ try{
|
|
|
+ preparedStatement.close();
|
|
|
+ }catch (Exception ex){
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (connection != null){
|
|
|
+ try{
|
|
|
+ connection.close();
|
|
|
+ }catch (Exception ex){
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int respCode = 0;
|
|
|
+
|
|
|
+ String respMsg = "操作成功";
|
|
|
+
|
|
|
+ if(datas.size() == 0){
|
|
|
+
|
|
|
+ respCode = 1;
|
|
|
+ respMsg = "操作成功,但没有查找到记录!";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return PageResult.<List<Map<String, Object>>>builder().datas(datas).resp_code(respCode).resp_msg(respMsg).pageSize(pageSize).pageNumber(pageNumber).pageCount(pageCount).recordCount(count).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getExecSQL(String sql, Object[] bindArgs) {
|
|
|
+ StringBuilder sb = new StringBuilder(sql);
|
|
|
+ if (bindArgs != null && bindArgs.length > 0) {
|
|
|
+ int index = 0;
|
|
|
+ for (int i = 0; i < bindArgs.length; i++) {
|
|
|
+ index = sb.indexOf("?", index);
|
|
|
+ sb.replace(index, index + 1, String.valueOf(bindArgs[i]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将结果集对象封装成List<Map<String, Object>> 对象
|
|
|
+ *
|
|
|
+ * @param resultSet 结果多想
|
|
|
+ * @return 结果的封装
|
|
|
+ * @throws SQLException
|
|
|
+ */
|
|
|
+ private List<Map<String, Object>> getDatas(ResultSet resultSet) throws SQLException {
|
|
|
+ List<Map<String, Object>> datas = new ArrayList<>();
|
|
|
+ /**获取结果集的数据结构对象**/
|
|
|
+ ResultSetMetaData metaData = resultSet.getMetaData();
|
|
|
+ while (resultSet.next()) {
|
|
|
+ Map<String, Object> rowMap = new HashMap<>();
|
|
|
+ for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
|
|
+ rowMap.put(metaData.getColumnName(i).replaceAll("(\\[|\\]|`)",""), resultSet.getObject(i));
|
|
|
+ }
|
|
|
+ datas.add(rowMap);
|
|
|
+ }
|
|
|
+// System.out.println("成功查询到了" + datas.size() + "行数据");
|
|
|
+ for (int i = 0; i < datas.size(); i++) {
|
|
|
+ Map<String, Object> map = datas.get(i);
|
|
|
+// System.out.println("第" + (i + 1) + "行:" + map);
|
|
|
+ }
|
|
|
+ return datas;
|
|
|
+ }
|
|
|
+}
|