前言:
PostgreSQL15+对应PostGIS安装教程及空间数据可视化
空间数据库-常用空间函数
完成PostGIS的安装与配置后,让我们来写一个Java操作postgis数据库的demo吧~
使用工具:
-- 创建postgis扩展
CREATE EXTENSION postgis;
-- 创建表 bou2_4p
CREATE TABLE bou2_4p (
gid SERIAL PRIMARY KEY,
area float8,
perimeter float8,
bou2_4m_ float8,
bou2_4m_id float8,
adcode93 INT,
adcode99 INT,
name VARCHAR(20),
geom geometry(MultiPolygon, 4326)
);
INSERT INTO bou2_4p (area, perimeter, bou2_4m_, bou2_4m_id, adcode93, adcode99, name, geom) VALUES (0.0, 0.018, 923.0, 3109.0, 810000, 810000, '香港特别行政区', ST_GeomFromText('MULTIPOLYGON(((114.286201477051 22.1847763061523,114.28434753418 22.1850299835205,114.284927368164 22.187816619873,114.287582397461 22.1907157897949,114.290313720703 22.1892375946045,114.287910461426 22.1863689422607,114.286201477051 22.1847763061523)))', 4326));
INSERT INTO bou2_4p (area, perimeter, bou2_4m_, bou2_4m_id, adcode93, adcode99, name, geom) VALUES (0.0, 0.014, 924.0, 3112.0, 810000, 810000, '香港特别行政区', ST_GeomFromText('MULTIPOLYGON(((114.303497314453 22.1849155426025,114.304130554199 22.1863536834717,114.305946350098 22.1868076324463,114.308296203613 22.1860828399658,114.308296203613 22.1850547790527,114.307823181152 22.1833896636963,114.305519104004 22.1828384399414,114.304168701172 22.1835269927979,114.303497314453 22.1849155426025)))', 4326));
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.7version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.postgresqlgroupId>
<artifactId>postgresqlartifactId>
<version>42.3.8version>
dependency>
<dependency>
<groupId>net.postgisgroupId>
<artifactId>postgis-jdbcartifactId>
<version>2.5.0version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.30version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.32version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
dependencies>
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/postdemo"/>
<property name="username" value="postgres"/>
<property name="password" value="123456"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="com/postgis/demo/mapper/demoMapper.xml" />
mappers>
configuration>
bou2_4p
:package com.postgis.demo.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class bou2_4p {
private int gid;
private double area;
private double perimeter;
private double bou2_4m_;
private double bou2_4m_id;
private int adcode93;
private int adcode99;
private String name;
private String geom;
}
package com.postgis.demo.mapper;
import com.postgis.demo.pojo.bou2_4p;
import java.util.List;
public interface demoMapper {
List<bou2_4p> selAll();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.postgis.demo.mapper.demoMapper">
<select id="selAll" resultType="com.postgis.demo.pojo.bou2_4p">
select gid,area,perimeter,bou2_4m_,bou2_4m_id,adcode93,adcode99,name,ST_AsText(geom) as geom from bou2_4p
</select>
</mapper>
package com.postgis.demo.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.postgis.demo.pojo.bou2_4p;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Test {
public static void main(String[] args) throws IOException {
Logger logger = LoggerFactory.getLogger(Test.class);
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//使用工厂设计模式
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
//生产SqlSession
SqlSession session=factory.openSession();
logger.info("查询数据:");
//方法名+包名
List<bou2_4p> selectList = session.selectList("com.postgis.demo.mapper.demoMapper.selAll");
for(bou2_4p u:selectList){
System.out.println(u);
}
session.close();
}
}
参考文档