Jelajahi Sumber

图斑检查工具BS版

liyatao 7 bulan lalu
induk
melakukan
8d6d0ccea9

+ 5 - 0
onemap-modules/onemap-overlap/pom.xml

@@ -165,6 +165,11 @@
             <artifactId>jts-core</artifactId>
             <version>1.19.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.xerial</groupId>
+            <artifactId>sqlite-jdbc</artifactId>
+            <version>3.47.1.0</version>
+        </dependency>
     </dependencies>
 
     <repositories>

+ 24 - 0
onemap-modules/onemap-overlap/src/main/java/com/onemap/overlap/controller/OverlapController.java

@@ -0,0 +1,24 @@
+package com.onemap.overlap.controller;
+
+import com.onemap.common.core.web.controller.BaseController;
+import com.onemap.common.core.web.domain.RequestResult;
+import com.onemap.overlap.service.SQLiteService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.IOException;
+
+@RestController
+@RequestMapping("/overlap")
+public class OverlapController extends BaseController {
+    @Autowired
+    private SQLiteService sqliteService;
+    @PostMapping("/overlapAnalysis")
+    public RequestResult overlapAnalysis() throws IOException {
+        sqliteService.overlapAnalysis();
+        return null;
+    }
+}
+

+ 8 - 0
onemap-modules/onemap-overlap/src/main/java/com/onemap/overlap/service/SQLiteService.java

@@ -0,0 +1,8 @@
+package com.onemap.overlap.service;
+
+import java.io.IOException;
+
+public interface SQLiteService {
+
+    public void overlapAnalysis() throws IOException;
+}

+ 9 - 0
onemap-modules/onemap-overlap/src/main/java/com/onemap/overlap/service/impl/ResultSetExtractor.java

@@ -0,0 +1,9 @@
+package com.onemap.overlap.service.impl;
+
+import java.sql.ResultSet;
+
+public interface ResultSetExtractor<T> {
+
+    public abstract T extractData(ResultSet rs);
+
+}

+ 8 - 0
onemap-modules/onemap-overlap/src/main/java/com/onemap/overlap/service/impl/RowMapper.java

@@ -0,0 +1,8 @@
+package com.onemap.overlap.service.impl;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+public interface RowMapper<T> {
+    public abstract T mapRow(ResultSet rs, int index) throws SQLException;
+}

+ 170 - 0
onemap-modules/onemap-overlap/src/main/java/com/onemap/overlap/service/impl/SQLiteHelper.java

@@ -0,0 +1,170 @@
+package com.onemap.overlap.service.impl;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.jdbc.core.ResultSetExtractor;
+import org.springframework.jdbc.core.RowMapper;
+
+public class SQLiteHelper {
+    final static Logger logger = LoggerFactory.getLogger(SQLiteHelper.class);
+
+    private Connection connection;
+    private Statement statement;
+    private ResultSet resultSet;
+    private String dbFilePath;
+
+    /**
+     * 构造函数
+     * @param dbFilePath sqlite db 文件路径
+     * @throws ClassNotFoundException
+     * @throws SQLException
+     */
+    public SQLiteHelper(String dbFilePath) throws ClassNotFoundException, SQLException {
+        this.dbFilePath = dbFilePath;
+        connection = getConnection(dbFilePath);
+    }
+
+    /**
+     * 获取数据库连接
+     * @param dbFilePath db文件路径
+     * @return 数据库连接
+     * @throws ClassNotFoundException
+     * @throws SQLException
+     */
+    public Connection getConnection(String dbFilePath) throws ClassNotFoundException, SQLException {
+        Connection conn = null;
+        Class.forName("org.sqlite.JDBC");
+        conn = DriverManager.getConnection("jdbc:sqlite:" + dbFilePath);
+        return conn;
+    }
+
+    /**
+     * 执行sql查询
+     * @param sql sql select 语句
+     * @param rse 结果集处理类对象
+     * @return 查询结果
+     * @throws SQLException
+     * @throws ClassNotFoundException
+     */
+    public <T> T executeQuery(String sql, ResultSetExtractor<T> rse) throws SQLException, ClassNotFoundException {
+        try {
+            resultSet = getStatement().executeQuery(sql);
+            T rs = rse.extractData(resultSet);
+            return rs;
+        } finally {
+            destroyed();
+        }
+    }
+
+    /**
+     * 执行select查询,返回结果列表
+     *
+     * @param sql sql select 语句
+     * @param rm 结果集的行数据处理类对象
+     * @return
+     * @throws SQLException
+     * @throws ClassNotFoundException
+     */
+    public <T> List<T> executeQuery(String sql, RowMapper<T> rm) throws SQLException, ClassNotFoundException {
+        List<T> rsList = new ArrayList<T>();
+        try {
+            resultSet = getStatement().executeQuery(sql);
+            while (resultSet.next()) {
+                rsList.add(rm.mapRow(resultSet, resultSet.getRow()));
+            }
+        } finally {
+            destroyed();
+        }
+        return rsList;
+    }
+
+    /**
+     * 执行数据库更新sql语句
+     * @param sql
+     * @return 更新行数
+     * @throws SQLException
+     * @throws ClassNotFoundException
+     */
+    public int executeUpdate(String sql) throws SQLException, ClassNotFoundException {
+        try {
+            int c = getStatement().executeUpdate(sql);
+            return c;
+        } finally {
+            destroyed();
+        }
+
+    }
+
+    /**
+     * 执行多个sql更新语句
+     * @param sqls
+     * @throws SQLException
+     * @throws ClassNotFoundException
+     */
+    public void executeUpdate(String...sqls) throws SQLException, ClassNotFoundException {
+        try {
+            for (String sql : sqls) {
+                getStatement().executeUpdate(sql);
+            }
+        } finally {
+            destroyed();
+        }
+    }
+
+    /**
+     * 执行数据库更新 sql List
+     * @param sqls sql列表
+     * @throws SQLException
+     * @throws ClassNotFoundException
+     */
+    public void executeUpdate(List<String> sqls) throws SQLException, ClassNotFoundException {
+        try {
+            for (String sql : sqls) {
+                getStatement().executeUpdate(sql);
+            }
+        } finally {
+            destroyed();
+        }
+    }
+
+    private Connection getConnection() throws ClassNotFoundException, SQLException {
+        if (null == connection) connection = getConnection(dbFilePath);
+        return connection;
+    }
+
+    private Statement getStatement() throws SQLException, ClassNotFoundException {
+        if (null == statement) statement = getConnection().createStatement();
+        return statement;
+    }
+
+    /**
+     * 数据库资源关闭和释放
+     */
+    public void destroyed() {
+        try {
+            if (null != connection) {
+                connection.close();
+                connection = null;
+            }
+
+            if (null != statement) {
+                statement.close();
+                statement = null;
+            }
+
+            if (null != resultSet) {
+                resultSet.close();
+                resultSet = null;
+            }
+        } catch (SQLException e) {
+            logger.error("Sqlite数据库关闭时异常", e);
+        }
+    }
+}

+ 59 - 0
onemap-modules/onemap-overlap/src/main/java/com/onemap/overlap/service/impl/SQLiteImp.java

@@ -0,0 +1,59 @@
+package com.onemap.overlap.service.impl;
+
+import com.onemap.overlap.service.SQLiteService;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.jdbc.core.RowMapper;
+import org.springframework.stereotype.Service;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.sql.*;
+import com.onemap.overlap.service.impl.SQLiteHelper;
+
+@Service
+public class SQLiteImp implements SQLiteService{
+    @Value("${spatialite.filepath}")
+    String filepath;
+    @Override
+    public void overlapAnalysis(){
+        Connection connection = null;
+        Statement statement = null;
+        try {
+            // 加载SQLite驱动程序
+            Class.forName("org.sqlite.JDBC");
+            // 创建数据库连接
+            //connection = DriverManager.getConnection(String.format("jdbc:sqlite:%s",filepath));
+            SQLiteHelper sqliteHelper = new SQLiteHelper(filepath);
+            connection = sqliteHelper.getConnection(filepath);
+            // 创建Statement对象
+            statement = connection.createStatement();
+
+            //查询
+            String querySQL = "select 图层名,合法方式,有效性 from 管控信息表";
+            //ResultSet resultSet = sqliteHelper.executeQuery(querySQL,new RowMapper<String>(){});
+            ResultSet resultSet = statement.executeQuery(querySQL);
+
+            System.out.println("数据查询成功!");
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        } finally {
+            // 关闭Statement和Connection
+            if (statement != null) {
+                try {
+                    statement.close();
+                } catch (SQLException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (connection != null) {
+                try {
+                    connection.close();
+                } catch (SQLException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+   }
+}

+ 4 - 1
onemap-modules/onemap-overlap/src/main/resources/application.yml

@@ -10,4 +10,7 @@ server:
 swagger:
   title: 系统模块接口文档
   license: Powered By ruoyi
-  licenseUrl: https://ruoyi.vip
+  licenseUrl: https://ruoyi.vip
+
+spatialite:
+  filePath: D:\mapcheck.sqlite