本篇案列是谷歌切片的查询,但适用其他场景,所谓万变不离其宗,主要看思路。
切片实体:
package com.appleyk.entity;
import org.springframework.data.annotation.Id;
public class Tile {
@Id
private Long id;
/**
* 切片行 -- X
*/
private Integer row;
/**
* 切片列 -- Y
*/
private Integer col;
/**
* 切片级别 -- Z
*/
private Integer level;
/**
* 切片版本 -- V
*/
private Long version;
/**
* 切片来源 -- S -- 比如切片数据来源自Google、百度、天地图等
*/
private String source;
/**
* 切片的二进制数据
*/
private byte[] data;
public Tile(){
}
public Tile(Integer row,Integer col,Integer level){
this.row = row;
this.col = col;
this.level = level;
}
public Tile(Long id,Integer row,Integer col,Integer level,Long version,String source){
this.id = id;
this.row = row;
this.col = col;
this.level = level;
this.version = version;
this.source = source;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getCol() {
return col;
}
public void setCol(Integer col) {
this.col = col;
}
public Integer getRow() {
return row;
}
public void setRow(Integer row) {
this.row = row;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}
切片服务service
package com.appleyk.service;
import java.util.List;
import com.appleyk.entity.Tile;
public interface TileService {
List<Tile> findData(Tile tile);
}
切片服务实现service.Impl
package com.appleyk.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Field;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import com.appleyk.entity.Tile;
import com.appleyk.service.TileService;
@Service
@Primary
public class TileServiceImpl implements TileService {
@Autowired
private MongoTemplate mongo;
/**
* 根据Tile切片对象过滤查询 -- and...and...and 多条件和
*/
@Override
public List<Tile> findData(Tile tile) {
Criteria criteria = new Criteria();
if(tile.getRow()!=null){
criteria.and("row").is(tile.getRow());
}
if(tile.getCol()!=null){
criteria.and("col").is(tile.getCol());
}
if(tile.getLevel()!=null){
criteria.and("level").is(tile.getLevel());
}
Query query = new Query(criteria);
//query.limit(pagesize); //-- 限定返回多少行记录
Field fields = query.fields();
fields.include("data"); //-- 只显示data这个字段
query.with(new Sort(new Sort.Order(Sort.Direction.ASC, "id"))); //按id进行 升序
//Query: { "row" : 1665 , "col" : 814 , "level" : 11}, Fields: { "data" : 1 }, Sort: { "id" : 1}
List<Tile> tiles =mongo.find(query, Tile.class);
return tiles;
}
}
注意这里(关键)
当然,Criteria对象还有其他属性,怎么构建条件,请自行研究,本篇不深入
Query实例完整的原型是
Query: { }, Fields: null, Sort: null
填充条件和完善query后,sql语句如下
Query: { "row" : 1665 , "col" : 814 , "level" : 11}, Fields: { "data" : 1 }, Sort: { "id" : 1}
Controller,将查询到的切片data(二进制)回写到HttpServletResponse响应对象中
package com.appleyk.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.appleyk.entity.Tile;
import com.appleyk.result.ResponseMessage;
import com.appleyk.result.ResponseResult;
import com.appleyk.service.TileService;
@RestController
@RequestMapping("/rest/v1.0.1/appleyk/tile")
public class TileController {
@Autowired
private TileService tileService;
@GetMapping("/query")
public ResponseResult Query(Tile tile,HttpServletResponse response){
List<Tile> tiles = tileService.findData(tile);
byte[] data = tiles.get(0).getData();
response.setContentType("image/jpg"); //设置返回的文件类型
OutputStream os;
try {
os = response.getOutputStream();
os.write(data);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return new ResponseResult(ResponseMessage.OK);
}
}
os.write(data) -- 搞定
浏览器查询测试一把
如果不过瘾,可以看我的另一篇博文:Java + 原生MongoDB驱动 API 使用案例详说(两种方式)
版权声明:本文为Appleyk原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。