使用上一篇《SpringBoot和PostGIS环境搭建(Hibernate4)》,配置较多,这里给出Hibernate5的SpringBoot和PostGIS环境搭建,仅仅引入一个hibernate-spatial-5.2.12.Final.jar包。同时,model类做相应调整,实现空间增删改查,以供大家参考。
1、创建空间表
创建普通关系表,如:
CREATE TABLE city
(
id integer primary key,
name character varying(32)
)
添加空间字段
SELECT AddGeometryColumn ('city', 'geom', 4326, 'POLYGON', 2);
2、application.properties配置
#服务端配置
#配置服务器端口,默认为8080
server.port=9090
#配置访问路径,默认为/
server.context-path=/
#配置Tomcat编码,默认为UTF-8
server.tomcat.uri-encoding=UTF-8
#postgresql数据库配置(默认是tomcat-jdbc连接池)
spring.jpa.database=postgresql
spring.jpa.show-sql=true
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
#Hibernate ddl auto(create,create-drop,update,validate)
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/gis
spring.datasource.username=postgres
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver
#HikariCP
#一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒以上
spring.datasource.hikari.maxLifetime: 1765000
#连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
spring.datasource.hikari.maximumPoolSize: 10
#html模板
#前缀
spring.thymeleaf.prefix=classpath:/templates/
#后缀
spring.thymeleaf.suffix=.html
#应用于模板的模板模式
spring.thymeleaf.mode = HTML5
#模板编码
spring.thymeleaf.encoding = UTF-8
#Content-Type值
spring.thymeleaf.content-type = text/html
#启用模板缓存(开发时建议关闭)
spring.thymeleaf.cache=false
3、pom.xml配置
xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd";>
4.0.0
com.facr
1.0.0
FacR
Factory Relation
org.springframework.boot
spring-boot-starter-parent
1.5.4.RELEASE
1.8
5.2.12.Final
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
org.springframework.boot
test
org.springframework.boot
spring-boot-starter-data-jpa
org.apache.tomcat
org.hibernate
${hibernate.version}
org.postgresql
42.1.4
com.zaxxer
com.alibaba
1.2.39
org.springframework.boot
true
org.springframework.boot
com.facr.Application
repackage
4、实体表注解
@Entity
@Table(name="city")
@JsonIgnoreProperties({"handler","hibernateLazyInitializer"})
public class City implements Serializable{
private static final long serialVersionUID = -6388874133425262671L;
private long id;
private String name;
private Polygon geom;
@Id
@Column(name="id",unique=true,nullable=false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name="name",length=32)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonIgnore
@Type(type = "com.vividsolutions.jts.geom.Geometry")
//@Column(name="geom",columnDefinition="com.vividsolutions.jts.geom.Geometry(Polygon,4326)")
@Column(name="geom",columnDefinition="Geometry(Polygon,4326)")
public Polygon getGeom() {
return geom;
}
public void setGeom(Polygon geom) {
this.geom = geom;
}
}