地理几何元素的增删改分为两种方式,一种是在后端对数据库进行操作,另一种是在前端对feature要素进行修改(具体形式跟前端的技术有关,比如Leaflet、Openlayer)。
本次主要介绍的是通过后端实现对postgis数据库中地理几何元素的增删改,通过前端修改的方式将在后期的博文中介绍。
采用后端进行postgis中地理几何元素的增删改优点是减轻前端的压力。
1.在pom.xml中加入引用
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
2.连接数据库
jdbcUrl: jdbc:postgresql://localhost:5432/test?useSSL=false
username: postgres
password: 123456
driverClassName: org.postgresql.Driver
3.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ol.openlayer.project.feature.mapper.MapMapper">
<resultMap id="MapElementMap" type="com.ol.openlayer.project.feature.domain.MapElement">
<id property="id" column="id"/>
<result property="name" javaType="String" jdbcType="VARCHAR" column="name"/>
<result property="latitude" javaType="double" jdbcType="REAL" column="latitude"/>
<result property="longitude" javaType="double" jdbcType="REAL" column="longitude"/>
<result property="geoStr" javaType="String" jdbcType="VARCHAR" column="element_location"/>
</resultMap>
<insert id="addMapElement" parameterType="com.ol.openlayer.project.feature.domain.MapElement" useGeneratedKeys="true" keyProperty="id">
<selectKey keyProperty="id" resultType="Long" order="BEFORE">
SELECT nextval('map_elements_id_seq'::regclass)
</selectKey>
insert into map_elements(name, longitude, latitude, element_location)
values (#{name}, #{longitude}, #{latitude}, ST_GeomFromText(#{geoStr}, 4326))
</insert>
<select id="findById" parameterType="Long" resultMap="MapElementMap">
SELECT id, name, longitude, latitude,
ST_AsGeoJson(element_location) as element_location
FROM map_elements
where id = #{id}
</select>
<delete id="deleteMapElement" parameterType="Long">
delete from map_elements where id = #{id}
</delete>
<update id="updateMapElement" parameterType="com.honorzhang.postgresql.model.MapElement" useGeneratedKeys="true" keyProperty="id" >
UPDATE map_elements
<trim prefix="set" suffixOverrides=",">
<if test="name!=null">name=#{name},</if>
<if test="longitude!=null">longitude=#{longitude},</if>
<if test="latitude!=null">latitude=#{latitude},</if>
<if test="geoStr!=null">element_location=ST_GeomFromText(#{geoStr}, 4326),</if>
</trim>
WHERE id=#{id}
</update>
</mapper>
4.Mapper
package com.ol.openlayer.project.feature.mapper;
import com.ol.openlayer.project.feature.domain.MapElement;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* (GiPayApply)表数据库访问层
*
* @author makejava
* @since 2020-04-14 16:16:15
*/
@Mapper
public interface MapMapper {
/** * @param mapElement */
void addMapElement(MapElement mapElement);
/** * @param id id * @return 依据id返回数据 */
MapElement findById(Long id);
void deleteMapElement(Long id);
void updateMapElement(MapElement mapElement);
}
5.Service
package com.ol.openlayer.project.feature.service.impl;
import com.ol.openlayer.project.feature.domain.MapElement;
import com.ol.openlayer.project.feature.mapper.MapMapper;
import com.ol.openlayer.project.feature.service.MapService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* (GiPayApply)表服务实现类
*
* @author makejava
* @since 2020-04-14 16:16:15
*/
@Service("mapService")
public class MapServiceImpl implements MapService {
@Resource
private MapMapper mapMapper;
@Override
public void addMapElement(MapElement mapElement) {
mapMapper.addMapElement(mapElement);
}
@Override
public MapElement findById(Long id) {
return mapMapper.findById(id);
}
@Override
public void deleteMapElement(Long id){
mapMapper.deleteMapElement(id);
}
@Override
public void updateMapElement(MapElement mapElement)
{
mapMapper.updateMapElement(mapElement);
}
}
6.controller
package com.ol.openlayer.project.feature.controller;
import com.ol.openlayer.project.feature.domain.MapElement;
import com.ol.openlayer.project.feature.service.MapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import static com.ol.openlayer.common.utils.map.Geometry.geometryToString;
@Api(tags = "空间数据操作")
@RestController
public class FeatureController {
@Resource
private MapService mapService;
/**
* * 添加地图元素 *
* @param mapElement
* * @return 添加的地理元素信息
* */
@ApiOperation(value = "上传矢量")
@PostMapping("/feature/add")
public MapElement addMapElement(@RequestBody MapElement mapElement){
mapElement.setGeoStr(geometryToString(mapElement.getLongitude(), mapElement.getLatitude()));
mapService.addMapElement(mapElement);
Long id = mapElement.getId();
return mapService.findById(id);
}
/** *
* @param id id *
* @return 是否删除成功 */
@ApiOperation(value = "删除矢量")
@DeleteMapping("/feature/delete/{id}")
public Boolean deleteMapElement(@PathVariable Long id){
Boolean deleteMapElementSuccess = true;
try{
mapService.deleteMapElement(id);
}catch (Exception e){
// log.info("删除失败:" + e);
deleteMapElementSuccess = false;
}
return deleteMapElementSuccess;
}
/** * 数据更改 *
* @param mapElement *
* @return 更改后的数据 */
@ApiOperation(value = "编辑矢量")
@PutMapping("/feature/modify")
public MapElement updateMapElement(@RequestBody MapElement mapElement){
mapElement.setGeoStr(geometryToString(mapElement.getLongitude(), mapElement.getLatitude()));
mapService.updateMapElement(mapElement);
Long id = mapElement.getId();
return mapService.findById(id);
}
}
关注以下公众号,关注各种技术文章,获取本系列后续推送与分享