java+mybatis+postgis读取数据库数据
webgis开发的时候选择postgres+postgis最佳,postgis不但提供了支持空间存储,而且postgis提供的空间操作函数非常强大,不亚于geotools和JTS。本篇文章主要讲述,结合mybatis去获取postgis数据。
一、全局配置XML
<?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/testRoute"/>
<property name="username" value="postgres"/>
<property name="password" value="postgres"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hpu/mapper/chinaMapper.xml" />
</mappers>
</configuration>
与mysql配置略有不同
二、Java封装类
package com.hpu.pojo;
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;
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
public double getBou2_4m_() {
return bou2_4m_;
}
public void setBou2_4m_(double bou2_4m_) {
this.bou2_4m_ = bou2_4m_;
}
public double getBou2_4m_id() {
return bou2_4m_id;
}
public void setBou2_4m_id(double bou2_4m_id) {
this.bou2_4m_id = bou2_4m_id;
}
public int getAdcode93() {
return adcode93;
}
public void setAdcode93(int adcode93) {
this.adcode93 = adcode93;
}
public int getAdcode99() {
return adcode99;
}
public void setAdcode99(int adcode99) {
this.adcode99 = adcode99;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGeom() {
return geom;
}
public void setGeom(String geom) {
this.geom = geom;
}
public bou2_4p(int gid, double area, double perimeter, double bou2_4m_, double bou2_4m_id, int adcode93, int adcode99,
String name, String geom) {
super();
this.gid = gid;
this.area = area;
this.perimeter = perimeter;
this.bou2_4m_ = bou2_4m_;
this.bou2_4m_id = bou2_4m_id;
this.adcode93 = adcode93;
this.adcode99 = adcode99;
this.name = name;
this.geom = geom;
}
public bou2_4p() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "bou2_4p [gid=" + gid + ", area=" + area + ", perimeter=" + perimeter + ", bou2_4m_=" + bou2_4m_
+ ", bou2_4m_id=" + bou2_4m_id + ", adcode93=" + adcode93 + ", adcode99=" + adcode99 + ", name=" + name
+ ", geom=" + geom + "]";
}
}
该类封装了bou2_4p表所有字段,geom字段转成WKT使用string来接受
三、mapper.xml
<?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.hpu.China">
<select id="selAll" resultType="com.hpu.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 postgisTest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import com.hpu.pojo.bou2_4p;
public class Test {
public static void main(String[] args) throws IOException {
Logger logger=Logger.getLogger(Test.class);
logger.debug("putong");
InputStream is = Resources.getResourceAsStream("mybatisl.xml");
//使用工厂涉及模式
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
//生产SqlSession
SqlSession session=factory.openSession();
//方法名+包名
List<bou2_4p> selectList = session.selectList("com.hpu.China.selAll");
for(bou2_4p u:selectList){
System.out.println(u);
}
session.close();
}
}
五、结果
bou2_4p [gid=922, area=0.0, perimeter=0.018, bou2_4m_=923.0, bou2_4m_id=3109.0, adcode93=810000, adcode99=810000, name=香港特别行政区, geom=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)))]
bou2_4p [gid=923, area=0.0, perimeter=0.014, bou2_4m_=924.0, bou2_4m_id=3112.0, adcode93=810000, adcode99=810000, name=香港特别行政区, geom=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)))]
bou2_4p [gid=924, area=0.0, perimeter=0.079, bou2_4m_=925.0, bou2_4m_id=3114.0, adcode93=810000, adcode99=810000, name=香港特别行政区, geom=MULTIPOLYGON(((114.256278991699 22.1602687835693,114.254356384277 22.1638584136963,114.251022338867 22.1662311553955,114.247840881348 22.1702213287354,114.250122070313 22.1737785339355,114.263931274414 22.1833896636963,114.268623352051 22.1833782196045,114.271430969238 22.1724720001221,114.27498626709 22.1659698486328,114.275352478027 22.1639175415039,114.26685333252 22.1612224578857,114.256278991699 22.1602687835693)))]
bou2_4p [gid=925, area=0.0, perimeter=0.011, bou2_4m_=926.0, bou2_4m_id=3115.0, adcode93=810000, adcode99=810000, name=香港特别行政区, geom=MULTIPOLYGON(((114.298927307129 22.1781215667725,114.300636291504 22.1789150238037,114.302070617676 22.17897605896,114.302383422852 22.1780014038086,114.301986694336 22.1763286590576,114.300903320313 22.175479888916,114.299537658691 22.1761703491211,114.299049377441 22.1773509979248,114.298927307129 22.1781215667725)))]
转载自:https://blog.csdn.net/weixin_40184249/article/details/85238537