当前位置:Gxlcms > 数据库问题 > 第十二章 springboot + mongodb(复杂查询)

第十二章 springboot + mongodb(复杂查询)

时间:2021-07-01 10:21:17 帮助过:1人阅读

说明:

  • mongo2.x支持以上两种配置方式
  • mongo3.x仅支持uri方式

2、Admin

技术分享
package com.xxx.firstboot.domain;

import org.springframework.data.annotation.Id;

/**
 * 测试复杂的mongo查询
 */
public class Admin {
    @Id
    private String adminId;
    private String name;
    private Integer sex;
    private String address;

    public String getAdminId() {
        return adminId;
    }

    public void setAdminId(String adminId) {
        this.adminId = adminId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}
View Code

注意:

  • @Id必须有

3、AdminRepository

package com.xxx.firstboot.mongo;

import org.springframework.data.mongodb.repository.MongoRepository;

import com.xxx.firstboot.domain.Admin;

public interface AdminRepository extends MongoRepository<Admin, String> {
}

说明:该接口用于简单查询。这里是一个空接口,具有CRUD功能。

4、CustomerController

技术分享
/*********************测试复杂的mongo查询**********************/
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    
    @ApiOperation("增加一个Admin")
    @RequestMapping(value = "/addAdmin", method = RequestMethod.GET)
    public Admin addAdmin(@RequestParam("name") String name,
                          @RequestParam("sex") Integer sex,
                          @RequestParam("address") String address) {
        Admin admin = new Admin();
        admin.setName(name);
        admin.setSex(sex);
        admin.setAddress(address);
        return adminRepository.save(admin);
    }
    
    @ApiOperation("获取所有的Admin")
    @RequestMapping(value = "/getAllAdmin", method = RequestMethod.GET)
    public List<Admin> getAllAdmin() {
        return adminRepository.findAll();
    }
    
    @ApiOperation("复杂的admin查询")
    @RequestMapping(value = "/getAdminByNameAndSexOrAddress", method = RequestMethod.GET)
    public Admin getAdminByNameAndSexOrAddress(@RequestParam("name") String name,
                                                 @RequestParam(value="sex",required=false) Integer sex,
                                                 @RequestParam(value="address",required=false) String address) {
        /**
         * OR
         */
        BasicDBList orList = new BasicDBList(); //用于记录
        if (sex != null) {
            orList.add(new BasicDBObject("sex", sex));
        }
        if (StringUtils.isNotBlank(address)) {
            orList.add(new BasicDBObject("address", address));
        }
        BasicDBObject orDBObject = new BasicDBObject("$or", orList);
        
        /**
         * and
         */
        BasicDBList andList = new BasicDBList();
        andList.add(new BasicDBObject("name", name));
        andList.add(orDBObject);
        BasicDBObject andDBObject = new BasicDBObject("$and", andList);
        
        return mongoTemplate.findOne(new BasicQuery(andDBObject), Admin.class);

    }
View Code

说明:

  • getAdminByNameAndSexOrAddress要实现select * from admin where name = ? and (sex = ? or address = ?)
  • 通过BasicDBList、BasicDBObject构建查询参数
  • findOne返回第一个符合条件的结果、find返回符合条件的结果列表
  • 以上查询的collection是admin(VO的简单类名),也可以指定从某一个collection中查询(查看find、findOne等方法)

注意:mongodb的查询字段必须是小写

测试:

启动mongo,启动应用,打开swagger,访问即可。

 

参考:

http://blog.csdn.net/congcong68/article/details/47183209

第十二章 springboot + mongodb(复杂查询)

标签:base   min   value   val   opened   结果   mongod   import   isp   

人气教程排行