SpringBoot和PostGIS环境搭建(Hibernate5)

使用上一篇《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配置
<?xml version=”1.0″ encoding=”UTF-8″?>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.facr</groupId>
    <artifactId>FacR</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <name>FacR</name>
    <description>Factory Relation</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <hibernate.version>5.2.12.Final</hibernate.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <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>
        <!– Spring data JPA, default tomcat pool, exclude it –>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!– the postgresql driver –>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.1.4</version>
            <!– <scope>runtime</scope> –>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.39</version>
        </dependency>
        <!– springboot 热部署 –>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <!– optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入–>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.facr.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
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;
    }
}

转载自:https://blog.csdn.net/wm6752062/article/details/78404846

You may also like...