Springboot工程通过JAP的方式保存空间数据到PostgreSQL数据库

使用springboot Jpa保存Point类型坐标到PostgreSQL。

application.properties文件配置

spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/geoserver
spring.datasource.username=test
spring.datasource.password=test
spring.jpa.database=postgresql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.show-sql=true

pom.xml文件配置

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</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-data-jpa</artifactId>
		</dependency>
		
		<dependency>
		    <groupId>org.postgresql</groupId>
		    <artifactId>postgresql</artifactId>
		</dependency>
		
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.36</version>
		</dependency>
		
		<dependency>
			<groupId>net.sourceforge.javacsv</groupId>
			<artifactId>javacsv</artifactId>
			<version>2.0</version>
		</dependency>
		
		<dependency>
	        <groupId>com.vividsolutions</groupId>
	        <artifactId>jts</artifactId>
	        <version>1.13</version>
	    </dependency>
	    
	     <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>5.3.0.Beta1</version>
        </dependency>
        
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-java8</artifactId>
            <version>5.3.0.Beta1</version>
        </dependency>
        
        <dependency>
            <groupId>com.bedatadriven</groupId>
            <artifactId>jackson-datatype-jts</artifactId>
            <version>2.3</version>
        </dependency>
	</dependencies>

DAO层代码

package com.xxx.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.xxx.dto.XxxPointDTO;

public interface XxxPointDAO extends JpaRepository<XxxPointDTO, String>{

}

DTO层代码

package com.xxx.dto;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;

@Entity
@Table(name="db_xxx_xxxpoint",schema="xxx")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class XxxPointDTO implements Serializable {
	
	@Id
	@GeneratedValue(generator = "jpa-uuid")
	@Column(name = "ID")
	private String Id;
	
	@Column(name = "address_loc",columnDefinition = "GEOMETRY")
	private Geometry address_loc;

	public String getId() { 
		return Id;
	}

	public void setId(String id) {
		Id = id;
	}

	public Geometry getAddress_loc() {
		return address_loc;
	}

	public void setAddress_loc(Geometry address_loc) {
		this.address_loc = address_loc;
	}

}

listener层代码

package com.xxx.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.xxx.dao.FirePointDAO;
import com.xxx.dto.FirePointDTO;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequenceFactory;

@Component
@EnableScheduling
public class XxxPointListener {

	private static final Logger log = LoggerFactory.getLogger(XxxPointListener.class);
	
	@Autowired
	private XxxPointDAO xxxPointDAO;
	
	@Scheduled(cron="0/10 * * * * ?")
	public void consumerListener() {
		XxxPointDTO xxxPointDTO = new XxxPointDTO();
		Coordinate coord = new Coordinate(109.013388, 32.715519);
		CoordinateSequence coordinates= CoordinateArraySequenceFactory.instance().create(new Coordinate[] {coord});
		GeometryFactory factory= new GeometryFactory();
		Point point = new Point(coordinates, factory);
		xxxPointDTO.setAddress_loc(point);
		xxxPointDAO.save(xxxPointDTO);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值