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配置
<?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>
    </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>
        <!– Hibernate Spatial –>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>4.0</version>
            <exclusions>
                <exclusion>
                    <groupId>postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.postgis</groupId>
                    <artifactId>postgis-jdbc</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.0.0.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.13.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.javassist</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!– the postgresql driver –>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.1.4</version>
            <!– <scope>runtime</scope> –>
        </dependency>
        <dependency>
            <groupId>net.postgis</groupId>
            <artifactId>postgis-jdbc</artifactId>
            <version>2.1.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>postgresql</artifactId>
                    <groupId>postgresql</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernatespatial</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>1.1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>
            </exclusions>
        </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>
    <repositories>
        <repository>
            <id>OSGEO GeoTools repo</id>
        </repository>
        <repository>
            <id>Hibernate Spatial repo</id>
        </repository>
        <!– add JBOSS repository for easy access to Hibernate libraries –>
        <repository>
            <id>JBOSS</id>
        </repository>
    </repositories>
    <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 = “org.hibernate.spatial.GeometryType”)
    @Column(name=”geom”,columnDefinition=”org.postgis.Geometry”)
    public Polygon getGeom() {
        return geom;
    }
    public void setGeom(Polygon geom) {
        this.geom = geom;
    }
}

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

You may also like...