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