springboot + mongodb 查询实例
来源:互联网 发布:java整形转换为字符串 编辑:程序博客网 时间:2024/06/08 00:07
1、新建一个Persoon的实体类(此处省略);
2、创建dao层接口;
public interface PersonRepository extends MongoRepository<Person, String>{}3、实例
下面是支持的查询类型,每三条数据分别对应:(方法后缀,方法例子,mongodb原生查询语句)GreaterThan(大于) findByAgeGreaterThan(int age) {"age" : {"$gt" : age}}LessThan(小于) findByAgeLessThan(int age) {"age" : {"$lt" : age}}Between(在...之间) findByAgeBetween(int from, int to) {"age" : {"$gt" : from, "$lt" : to}}IsNotNull, NotNull(是否非空) findByFirstnameNotNull() {"age" : {"$ne" : null}}IsNull, Null(是否为空) findByFirstnameNull() {"age" : null}Like(模糊查询) findByFirstnameLike(String name) {"age" : age} ( age as regex)(No keyword) findByFirstname(String name) {"age" : name}Not(不包含) findByFirstnameNot(String name) {"age" : {"$ne" : name}}Near(查询地理位置相近的) findByLocationNear(Point point) {"location" : {"$near" : [x,y]}}Within(在地理位置范围内的) findByLocationWithin(Circle circle) {"location" : {"$within" : {"$center" : [ [x, y], distance]}}}Within(在地理位置范围内的) findByLocationWithin(Box box) {"location" : {"$within" : {"$box" : [ [x1, y1], x2, y2]}}}
4、补充
1.实体类:
public class Person {
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}2.仓库:
public interface PersonRepository extends MongoRepository<Person, String>{
}
如果只是用简单的CRUD操作,则不需要添加其他的方法,MongoRepository提供的方法足够我们使用。
3.利用MongoRepository中的查询进行操作
首先,在service层中,将PersonRepository注入到service类中
public class PersonServiceImpl implements IPersonService{
@Autowired
private PersonRepository personRepository;
} 1)查询所有的数据:
public List<Person> queryAll() throws Exception {
return personRepository.findAll();
} 2)查询所有的数据带分页:
方法为:Page<?> findAll(Pageable pageable); 该方法中的参数是一个借口,我们需要构造一个子对象,即:PageRequest对象,这个对象中有两个属性,第一个是页码,第二个是页面大小。注意:页码在mongodb中是从0开始的。
public Page<Person> queryAllByPage(int page,int rows) throws Exception {
PageRequest pageRequest = new PageRequest(page-1,rows);
return personRepository.findAll(pageRequest);
} 3)查询所有的数据的数量:
public int count() throws Exception {
long size = personRepository.count();
int count = Integer.valueOf(String.valueOf(size));
return count;
} 4)根据实体类中的属性进行查询:
当需要根据实体类中的属性查询时,MongoRepository提供的方法已经不能满足,我们需要在PersonRepository仓库中定义方法,定义方法名的规则为:find + By + 属性名(首字母大写),如:根据姓名查询Person
仓库中添加的方法:
public Person findByName(String name);
它会自动根据name查询。
service中的方法:
public void queryByFirstName(String name) throws Exception {
Person person = personRepository.findByName(name);
} 若根据其他属性查询,方法类似!
5)根据实体类中的属性进行模糊查询:
当需要根据实体类中的属性进行模糊查询时,我们也需要在PersonRepository仓库中定义方法,模糊查询定义方法名的规则为:find + By + 属性名(首字母大写) + Like,如:根据姓名进行模糊查询Person
仓库中添加的方法:
public List<Person> findByNameLike(String name);
service中的方法:
在service中直接调用仓库中我们刚才定义的方法即可!
public List<Person> queryByFirstNameLike(String name) throws Exception {
return personRepository.findByNameLike(name);
} 6)根据实体类中的属性进行模糊查询带分页:
带分页的模糊查询,其实是把模糊查询以及分页进行合并,同时我们也需要在PersonRepository仓库中定义方法,定义方法名的规则和模糊查询的规则一致,只是参数不同而已。
仓库中添加的方法:
public Page<Person> findByNameLike(String name,Pageable pageable);
在service中对仓库中的方法的调用:
public List<Person> queryByNameAndPage(int page, int rows, String name)
throws Exception {
PageRequest pageRequest = new PageRequest(page-1,rows);
return personRepository.findByNameLike(name, pageRequest).getContent();
} 7)根据实体类中的属性进行模糊查询带分页,同时指定返回的键(数据库中的key,实体类中的属性):
解释一下什么是指定返回的键:也就是说当我们进行带分页的模糊查询时,不想返回数据库中的所有字段,只是返回一部分字段。例如:只返回Person中的id和name,不返回age.
若想指定返回的键,我们需要在PersonRepository中添加方法,同时使用注解@Query。
仓库中定义的方法:
@Query(value="{'name':?0}",fields="{'name':1}")
public Page<Person> findByNameLike(String name,Pageable pageable); 其中value是查询的条件,?0这个是占位符,对应着方法中参数中的第一个参数,如果对应的是第二个参数则为?1。fields是我们指定的返回字段,其中id是自动返回的,不用我们指定,bson中{'name':1}的1代表true,也就是代表返回的意思。
在service中对仓库中的方法的调用:
public List<Person> queryByNameAndPage(int page, int rows, String name)
throws Exception {
PageRequest pageRequest = new PageRequest(page-1,rows);
return personRepository.findByNameLike(name, pageRequest).getContent();
}特殊查询:
1)需要查询所有数据,同时指定返回的键
当我们需要查询所有数据,同时需要指定返回的键时,则不能使用仓库中自带的findAll()方法了。我们可以查询所有id不为空的数据,同时指定返回的键。当我们需要根据一个key且该key不为空进行查询,方法名的定义规则为:find + By + 属性名(首字母大写) + NotNull。
仓库中定义的方法:
@Query(value="{'_id':{'$ne':null}}",fields="{'name':1}")
public Page<Person> findByIdNotNull(Pageable pageable); service中调用仓库中的方法:
public List<Person> queryAll(int page, int rows) throws Exception {
PageRequest pageRequest = new PageRequest(page-1,rows);
return personRepository.findByIdNotNull(pageRequest).getContent();
} 2)MongoDB的其他查询不一一列举,但将java中的仓库定义的方法名的规则列举如下,使用时将仓库中方法上的注解@Query中的value进行适当泰欧正即可。
GreaterThan(大于)
方法名举例:findByAgeGreaterThan(int age)
query中的value举例:{"age" : {"$gt" : age}}
LessThan(小于)
方法名举例:findByAgeLessThan(int age)
query中的value举例:{"age" : {"$lt" : age}}
Between(在...之间)
方法名举例:findByAgeBetween(int from, int to)
query中的value举例:{"age" : {"$gt" : from, "$lt" : to}}
Not(不包含)
方法名举例:findByNameNot(String name)
query中的value举例:{"age" : {"$ne" : name}}
Near(查询地理位置相近的)
方法名举例:findByLocationNear(Point point)
query中的value举例:{"location" : {"$near" : [x,y]}}
阅读全文
0 0
- springboot + mongodb 查询实例
- mongodb高级查询实例
- springboot构建solr查询实例
- SpringBoot操作MongoDB实现分页查询
- mongodb java高级查询实例
- SpringBoot-MongoDB
- 六.SpringBoot集成实例系列-单数据源mongodb(权限控制)
- 七.SpringBoot集成实例系列-多数据源mongodb(一)
- springboot副本集mongoDB
- SpringBoot -- 集成MongoDB
- SpringBoot -- 集成MongoDB
- springboot连接mongodb
- SpringBoot中集成MongoDB
- springboot+mongodb最简。。
- Springboot+MongoDB例子
- springboot整合mongodb
- SpringBoot整合MongoDB
- SpringBoot操作MongoDB
- http1.0 和 http1.1区别?
- 关于mysql中使用枚举类型还是int类型存取有限个固定值
- java大数算法完成五则运算之加减
- spark-streaming 读取kafka的方式
- 51Nod-1085-背包问题
- springboot + mongodb 查询实例
- vs2017无法查找或打开 pdb 文件
- microsoft visual c++ 6.0中文版两种使用方法
- 说说单例模式
- 关于springMVC无法加载静态资源的问题
- springMVC原理(二):SpringMVC核心分发器DispatcherServlet分析[附带源码分析]
- 530_自定义彩色进度条SeekBar
- 字符串问题---回文最小分割数
- win10安装PowerDesigner16.5报错:An error[-5001 ... 的解决办法