前言:
PostgreSQL15+对应PostGIS安装教程及空间数据可视化
空间数据库-常用空间函数
完成PostGIS的安装与配置后,让我们来写一个Java操作postgis数据库的demo吧~
使用工具:
- Navicat
- IDEA
一、PostGIS数据库准备
- 在Navicat中新建一个postgreSQL的连接,并新建一个名为"
postdemo
"的数据库:
→ - 在数据库中创建postgis扩展,并创建
bou2_4p
表(执行以下sql语句):
-- 创建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));
二、构建项目
- 在IDEA中创建一个Maven项目,并在pom.xml文件中引入相关依赖:
<dependencies>
<!-- Mybatis核心 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- pg驱动 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.8</version>
</dependency>
<!-- postgis -->
<dependency>
<groupId>net.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<!--slf4j依赖-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<!-- logback 依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
- mybatis全局配置xml文件(其中数据库的名字,用户名和密码,以及SQL映射文件路径改成自己的):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--声明使用的环境 default是引用环境的id,表示当前所使用的环境 -->
<environments default="default">
<environment id="default">
<!--使用原生JDBC事务 -->
<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;
}
- 创建mapper接口,SQL映射文件:
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();
}
}
-
运行测试类,得到如下结果:
-
下面是我的项目目录结构,作为参考:
参考文档