PostgreSQL、JAP、Springboot保存Point空间数据

使用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文件配置

>
		>
			>org.springframework.boot>
			>spring-boot-starter-web>
		>
		
		>
			>org.springframework.boot>
			>spring-boot-starter-test>
			>test>
		>
		
		>
		    >org.springframework.boot>
		    >spring-boot-starter-data-jpa>
		>
		
		>
		    >org.postgresql>
		    >postgresql>
		>
		
		>
			>com.alibaba>
			>fastjson>
			>1.2.36>
		>
		
		>
			>net.sourceforge.javacsv>
			>javacsv>
			>2.0>
		>
		
		>
	        >com.vividsolutions>
	        >jts>
	        >1.13>
	    >
	    
	     >
            >org.hibernate>
            >hibernate-spatial>
            >5.3.0.Beta1>
        >
        
        >
            >org.hibernate>
            >hibernate-java8>
            >5.3.0.Beta1>
        >
        
        >
            >com.bedatadriven>
            >jackson-datatype-jts>
            >2.3>
        >
	>

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);
	}
}

你可能感兴趣的:(Springboot相关文章)