Java的新專案學成線上筆記-day1(七)
6.2 Dao
6.2.1 分頁查詢測試 6.2.1.1 定義Dao介面
本專案使用Spring Data Mongodb完成Mongodb資料庫的查詢,Spring Data Mongodb提供一套快捷操作 mongodb的方法。 建立Dao,繼承MongoRepository,並指定實體型別和主鍵型別。
public interface CmsPageRepository extends MongoRepository<CmsPage,String> { }
6.2.1.2編寫測試類
test下的包路徑與main下的包路徑保持一致。 測試程式使用@SpringBootTest和@RunWith(SpringRunner.class)註解,啟動測試類會從main下找springBoot啟 動類,載入spring容器。
測試程式碼如下:
[mw_shl_code=applescript,true]package com.xuecheng.manage_cms; import com.xuecheng.framework.domain.cms.CmsPage; import com.xuecheng.manage_cms.dao.CmsPageRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.*; import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @RunWith(SpringRunner.class) public class CmsPageRepositoryTest { @Autowired CmsPageRepository cmsPageRepository; } [/mw_shl_code]
6.2.1.3 分頁查詢測試 //分頁測試
@Test
public void testFindPage() {
int page = 0;//從0開始
int size = 10;//每頁記錄數
Pageable pageable = PageRequest.of(page,size);
Page<CmsPage> all = cmsPageRepository.findAll(pageable);
System.out.println(all);
}
6.2.2 基礎方法測試
這裡Dao介面繼承了MongoRepository,在MongoRepository中定義了很多現成的方法,如save、delete等,通 過下邊的程式碼來測試這裡父類方法。
此小節內容請同學們自行測試。
6.2.3.2 刪除
[/size][/font][mw_shl_code=applescript,true]CmsPageRepository cmsPageRepository;
}
//分頁測試
@Test
public void testFindPage() {
int page = 0;//從0開始
int size = 10;//每頁記錄數
Pageable pageable = PageRequest.of(page,size);
Page<CmsPage> all = cmsPageRepository.findAll(pageable);
System.out.println(all);
}
//新增 @Test public void testInsert(){
//定義實體類
CmsPage cmsPage = new CmsPage();
cmsPage.setSiteId("s01");
cmsPage.setTemplateId("t01");
cmsPage.setPageName("測試頁面");
cmsPage.setPageCreateTime(new Date());
List<CmsPageParam> cmsPageParams = new ArrayList<>();
CmsPageParam cmsPageParam = new CmsPageParam();
cmsPageParam.setPageParamName("param1");
cmsPageParam.setPageParamValue("value1");
cmsPageParams.add(cmsPageParam);
cmsPage.setPageParams(cmsPageParams);
cmsPageRepository.save(cmsPage);
System.out.println(cmsPage); }
//刪除 @Test public void testDelete() {
cmsPageRepository.deleteById("5b17a2c511fe5e0c409e5eb3"); }
[/mw_shl_code]
6.2.3.3 修改
[mw_shl_code=applescript,true]//修改 @Test public void testUpdate() {
Optional<CmsPage> optional = cmsPageRepository.findOne("5b17a34211fe5e2ee8c116c9");
if(optional.isPresent()){
CmsPage cmsPage = optional.get();
cmsPage.setPageName("測試頁面01");
cmsPageRepository.save(cmsPage);
}
}
[/mw_shl_code]
關於Optional: Optional是jdk1.8引入的型別,Optional是一個容器物件,它包括了我們需要的物件,使用isPresent方法判斷所包 含物件是否為空,isPresent方法返回false則表示Optional包含物件為空,否則可以使用get()取出物件進行操作。
Optional的優點是: 1、提醒你非空判斷。
2、將物件非空檢測標準化。 6.2.3.4 自定義Dao方法
同Spring Data JPA一樣Spring Data mongodb也提供自定義方法的規則,如下: 按照findByXXX,findByXXXAndYYY、countByXXXAndYYY等規則定義方法,實現查詢操作。
[mw_shl_code=applescript,true]public interface CmsPageRepository extends MongoRepository<CmsPage,String> {
//根據頁面名稱查詢
CmsPage findByPageName(String pageName);
//根據頁面名稱和型別查詢
CmsPage findByPageNameAndPageType(String pageName,String pageType);
//根據站點和頁面型別查詢記錄數
int countBySiteIdAndPageType(String siteId,String pageType);
//根據站點和頁面型別分頁查詢
Page<CmsPage> findBySiteIdAndPageType(String siteId,String pageType, Pageable pageable);
}
[/mw_shl_code]
6.3 Service
定義頁面查詢方法,根據條件查詢暫時不實現:
package com.xuecheng.manage_cms.service; [/size][/font][mw_shl_code=applescript,true]import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.manage_cms.dao.CmsPageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@Service public class PageService {
@Autowired
CmsPageRepository cmsPageRepository;
/** * 頁面列表分頁查詢
* @param page 當前頁碼
* @param size 頁面顯示個數
* @param queryPageRequest 查詢條件
* @return 頁面列表
*/
public QueryResponseResult findList(int page,int size,QueryPageRequest queryPageRequest){
if (queryPageRequest == null) {
queryPageRequest = new QueryPageRequest();
}
if (page <= 0) {
page = 1;
}
page = page ‐ 1;//為了適應mongodb的介面將頁碼減1
if (size <= 0) {
size = 20;
}
//分頁物件
Pageable pageable = new PageRequest(page, size);
//分頁查詢
Page<CmsPage> all = cmsPageRepository.findAll(pageable);
QueryResult<CmsPage> cmsPageQueryResult = new QueryResult<CmsPage>();
cmsPageQueryResult.setList(all.getContent());
cmsPageQueryResult.setTotal(all.getTotalElements());
//返回結果
return new QueryResponseResult(CommonCode.SUCCESS,cmsPageQueryResult);
}
}