本篇文章主要讲解SpringBoot+MyBatis+PostgreSQL+PostGis实现对存储数据的查找,包括对一般普通的全部查找,id查找,条件查找,分页查找,以及对空间信息的圆形区域查找,多边形区域查找。
本篇文章会直接讲解查询,是(SpringBoot+MyBatis+PostgreSQL+PostGis实现地理几何元素的增删改)后续,建议读本文之前,先读一下这篇文章。
在文章讲解中,我只展示controller层以及myBatis的 mapper.xml文件部分,dao层以及service层相对较简单,文章中不做展示,全部工程代码在GitHub上()。
1.全部查询
controller层
/**
* 查询全部信息
* @return
*/
@GetMapping("/list")
public List<MapElement> findAll(){
return mapService.findAll();
}
mapper.xml文件
<select id="findAll" resultMap="MapElementMap">
SELECT id, name, longitude, latitude,
ST_AsGeoJson(element_location) as element_location
FROM map_elements
</select>
测试结果
2.条件查找
controller层
/**
* 条件查找
* @param mapElement
* @return 复合条件的地图元素
*/
@GetMapping("/condition")
public List<MapElement> findMapElementByCondition(MapElement mapElement){
return mapService.findMapElementByCondition(mapElement);
}
mapper.xml
<!-- 条件查询-->
<select id="findMapElementByCondition" parameterType="com.honorzhang.postgresql.model.MapElement" resultMap="MapElementMap">
select id, name, longitude, latitude,
ST_AsGeoJson(element_location) as element_location
FROM map_elements
where 1 = 1
<if test="name != null" >
and name like #{name}
</if>
<if test="longitude > 0">
and longitude = #{longitude}
</if>
<if test="latitude > 0">
and latitude = #{latitude}
</if>
</select>
测试结果
3.分页查找
分页查找,借助了一个pagehelper的插件,官方GitHub地址为https://github.com/pagehelper/Mybatis-PageHelper。
1.添加pom.xml关于pagehelper的插件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
<exclusions>
<exclusion>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
2.application.properties配置
pagehelper.helperDialect=postgresql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
3.controller层
/**
* 分页查找
* @param currentPage 当前在第几页
* @param pageSize 每页数据条数
* @return 带PageInfo信息的返回结果
*/
@GetMapping
public PageInfo<MapElement> findByPageCondition(int currentPage, int pageSize) {
PageHelper.startPage(currentPage, pageSize);
List<MapElement> list = mapService.findAll();
return new PageInfo<>(list);
}
方法包含两个参数,currentPage为当前在第几页,pageSize为每页显示的数据条数。
4.测试结果
测试结果包含的参数意思在GitHub源码有介绍,地址为:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/src/main/java/com/github/pagehelper/PageInfo.java
4.多边形区域查找
controller层
/**
* 多边形区域查找
* @param geometry 多边形参数
* @return 满足条件的结果
*/
@GetMapping("/polygon")
public List<MapElement> findByMapElementPolygon(String geometry){
return mapService.findMapElementByPolygon(geometry);
}
myBatis的mapper.xml配置
<!-- 多边形区域查询-->
<select id="findMapElementByPolygon" resultMap="MapElementMap">
SELECT id, name,longitude, latitude,
ST_AsGeoJson(element_location) as element_location
from map_elements
where ST_Contains( ST_MakePolygon(ST_GeomFromText('LINESTRING' || #{geometry}, 4326)) , element_location) = 't'
</select>
测试结果
此处测试结果我做了对比,第二个多边形区域与第一个多边形区域相比,多了一组参数,添加了一条边,从而查询结果包含了北京大学。
5.圆形区域查询
controller层
/**
* 圆形区域查找
* @param longitude 圆形区域圆心之经度
* @param latitude 圆形区域圆心之纬度
* @param radius 区域半径,单位为km
* @return 以给定点为原型,radis为半径的区域中满足条件的元素的集合
*/
@GetMapping("/circle")
public List<MapElement> findByMapElementCircle(double longitude, double latitude, double radius){
String geometry = "(" + longitude + " " + latitude + ")";
//单位转换
double radiusMeter = radius * 1000;
return mapService.findMapElementByCircle(geometry, radiusMeter);
}
myBatis的mapper.xml层
<!-- 圆形区域查询-->
<select id="findMapElementByCircle" resultMap="MapElementMap">
SELECT id, name, longitude, latitude, ST_AsGeoJson(element_location) as element_location
from map_elements
where ST_DWithin(element_location :: geography, ST_GeomFromText('POINT' || #{geometry}, 4326 ) :: geography, #{radius} ) IS TRUE
</select>
测试结果
如上图所示,圆形区域的圆心点为清华大学,经度为116.327,纬度为40。在地图上,北大与北航的的地理位置如图所示。
6.总结
本文主要介绍了使用myBatis对数据进行各种查找,其中重点应该放在分页查找,圆形区域查找,多边形区域查找。当中涉及很多额外的知识,需要自己先有一个了解。在代码部分,只简单列举了一部分代码,关于整个工程的完整代码,已经放在GitHub上,需要按照自己实际,对配置文件做一定更改。
最后,如有不妥之处,还望指正,希望能一起进步。