SpringBoot操作前端传的Geojson进行空间查询
项目说明:
项目技术栈:SpringBoot+MybatisPlus+postgresql
先上查询SQL
SELECT *
FROM demo
WHERE ST_Intersects(geom, ST_GeomFromGeoJSON('放geojson类型数据'));
表结构
Controller层
package com.itcy.postgresql.controller;
import com.itcy.postgresql.model.Demo;
import com.itcy.postgresql.service.DemoService;
import org.locationtech.jts.geom.Geometry;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* @BelongsProject: cyspring
* @BelongsPackage: com.itcy.postgresql.controller
* @Author: cuiYong
* @CreateTime: 2023-08-07 09:25
* @Description: TODO
* @Version: 1.0
*/
@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private DemoService demoService;
/**
* 传入geojson数据类型查询相交情况*/
@PostMapping("/getByGeoJson")
public List<Demo> getByGeoJson(@RequestBody String geoJson){
return demoService.getByGeoJson(geoJson);
}
Service层
package com.itcy.postgresql.service;
import com.itcy.postgresql.model.Demo;
import org.locationtech.jts.geom.Geometry;
import java.util.List;
/**
* @author 176
*/
public interface DemoService {
/**
* 传入geojson数据类型查询相交情况*/
List<Demo> getByGeoJson(String geoJson);
}
ServiceImpl层
package com.itcy.postgresql.service.impl;
import com.itcy.postgresql.mapper.DemoMapper;
import com.itcy.postgresql.model.Demo;
import com.itcy.postgresql.service.DemoService;
import org.locationtech.jts.geom.Geometry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author 176
*/
@Service
public class DemoServiceImpl implements DemoService {
@Override
public List<Demo> getByGeoJson(String geoJson) {
return demoMapper.getByGeoJson(geoJson);
}
}
Mapper层
package com.itcy.postgresql.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itcy.postgresql.model.Demo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.locationtech.jts.geom.Geometry;
import java.util.List;
@Mapper
public interface DemoMapper extends BaseMapper<China> {
List<Demo> getByGeoJson(String geoJson);
}
DemoMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.itcy.postgresql.mapper.DemoMapper">
<!--传入geojson查询数据库-->
<select id="getByGeoJson" resultType="com.itcy.postgresql.model.Demo">
SELECT *
FROM demo
WHERE ST_Intersects(geom, ST_GeomFromGeoJSON('${geoJson}'));
</select>
</mapper>
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- springboot 整合junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- json数据的转换的工具 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- PostGis -->
<dependency>
<groupId>net.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
<version>2.5.0</version>
</dependency>
<!-- 引入 Geometry 相关类 -->
<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>1.16.1</version>
</dependency>
<dependency>
<groupId>org.locationtech.spatial4j</groupId>
<artifactId>spatial4j</artifactId>
<version>0.7</version>
</dependency>
<!-- Geometry 转 JSON 需要 -->
<dependency>
<groupId>com.graphhopper.external</groupId>
<artifactId>jackson-datatype-jts</artifactId>
<version>1.0-2.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.5</version>
</plugin>
</plugins>
</build>
标签:postgresql,SpringBoot,itcy,boot,Geojson,查询,org,import,com
From: https://blog.51cto.com/u_16103484/7126134