Spring-Boot + MongoDB + Query ( 结合实例进行条件过滤查询)

本篇案列是谷歌切片的查询,但适用其他场景,所谓万变不离其宗,主要看思路。



切片实体:


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 使用案例详说(两种方式)




  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值