SpringBoot和PostGIS环境搭建(Hibernate4)

       根据项目需要,基于Hibernate4使用SpringBoot和PostGIS进行空间业务实现,经过多次尝试探索,终于实现空间增删改查,这里给出基本配置过程,以供大家参考。关于Hibernate5使用SpringBoot和PostGIS进行空间业务实现,配置更简单,可参看我的另一篇文章《SpringBoot和PostGIS环境搭建(Hibernate5)》。
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.url=jdbc:postgresql_postGIS://127.0.0.1:5432/gis
spring.datasource.username=postgres
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver
#spring.datasource.driverClassName=org.postgis.DriverWrapper

#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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">;
    4.0.0
    com.facr
    FacR
    1.0.0
    jar
    FacR
    Factory Relation
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.4.RELEASE
    
    
        UTF-8
        UTF-8
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
            
                
                    org.apache.tomcat
                    tomcat-jdbc
                
            
        
        
        
            org.hibernate
            hibernate-spatial
            4.0
            
                
                    postgresql
                    postgresql
                
                
                    org.postgis
                    postgis-jdbc
                
                
                    org.hibernate
                    hibernate-core
                
            
        
        
            org.hibernate
            hibernate-entitymanager
            4.0.0.Final
            
                
                    javassist
                    javassist
                
            
        
        
            org.hibernate
            hibernate-core
            4.2.13.Final
            
                
                    org.javassist
                    javassist
                
            
        
        
        
            org.postgresql
            postgresql
            42.1.4
            
        
        
            net.postgis
            postgis-jdbc
            2.1.3
            
                
                    postgresql
                    postgresql
                
            
        
        
            org.hibernatespatial
            hibernate-spatial
            1.1.1
            
                
                    javassist
                    javassist
                
            
        
        
            com.zaxxer
            HikariCP
        
        
            com.alibaba
            fastjson
            1.2.39
        
        
        
            org.springframework.boot
            spring-boot-devtools
            true
            
        
    
    
        
            OSGEO GeoTools repo
            http://download.osgeo.org/webdav/geotools;
        
        
            Hibernate Spatial repo
            http://www.hibernatespatial.org/repository;
        
        
        
            JBOSS
            https://repository.jboss.org/nexus/content/repositories/releases/;
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    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 = "org.hibernate.spatial.GeometryType")
    @Column(name="geom",columnDefinition="org.postgis.Geometry")
    public Polygon getGeom() {
        return geom;
    }
    public void setGeom(Polygon geom) {
        this.geom = geom;
    }
}

你可能感兴趣的:(Java,GIS,Hibernate,SpringBoot,PostGIS,Hibernate4)