MongoRepository

ava操作MongoDB採用MongoRepository倉庫進行條件查詢
1.實體類:

public class Person implements Serializable{
    private static final long serialVersionUID = -8288372263395673353L;
    private String id;
    private String name;
    private int age;
    
    // set/get ...
}

2.倉庫:
如果只是用簡單的CRUD操作,則不需要添加其他的方法,MongoRepository提供的方法足夠我們使用。

public interface PersonRepository extends MongoRepository<Person, String>{
}
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&quot;:from,&quot; gt&quot; : from, &quot;gt":from,"lt” : to}}

Not(不包含)
方法名舉例:findByNameNot(String name)
query中的value舉例:{“age” : {"$ne" : name}}

Near(查詢地理位置相近的)
方法名舉例:findByLocationNear(Point point)
query中的value舉例:{“location” : {"$near" : [x,y]}}

多個字段查詢實例:
package com.pptv.ott.ucr.common.repository;

import com.pptv.ott.ucr.common.entity.PraiseAndTrample;
import org.springframework.stereotype.Repository;

/**
 * 贊和踩操作
 *
 * @author pengren
 */
@Repository
public interface PraiseAndTrampleRepository extends BaseRepository<PraiseAndTrample, String> {
    /**
     * 根據commentId和account查詢
     * @param commentId,account
     * @return
     */
    PraiseAndTrample findByCommentIdAndAccount(String commentId, String account);
}
發佈了120 篇原創文章 · 獲贊 42 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章