A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 谷粒姐姐 于 2018-12-19 16:35 编辑

6.2 Dao
6.2.1 分页查询测试 6.2.1.1 定义Dao接口
本项目使用Spring Data Mongodb完成Mongodb数据库的查询,Spring Data Mongodb提供一套快捷操作 mongodb的方法。 创建Dao,继承MongoRepository,并指定实体类型和主键类型。
[AppleScript] 纯文本查看 复制代码
public interface CmsPageRepository extends MongoRepository<CmsPage,String> { }

6.2.1.2编写测试类

test下的包路径与main下的包路径保持一致。 测试程序使用@SpringBootTest和@RunWith(SpringRunner.class)注解,启动测试类会从main下找springBoot启 动类,加载spring容器。
测试代码如下:

[AppleScript] 纯文本查看 复制代码
[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

[AppleScript] 纯文本查看 复制代码
 CmsPageRepository cmsPageRepository;  
   }
[/mw_shl_code]
6.2.1.3 分页查询测试
//分页测试         
[AppleScript] 纯文本查看 复制代码
@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等,通 过下边的代码来测试这里父类方法。
此小节内容请同学们自行测试。
[AppleScript] 纯文本查看 复制代码
 
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); }

[AppleScript] 纯文本查看 复制代码
//删除 @Test public void testDelete() { 
    cmsPageRepository.deleteById("5b17a2c511fe5e0c409e5eb3"); }
[/mw_shl_code]
6.2.3.3 修改
[AppleScript] 纯文本查看 复制代码
[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等规则定义方法,实现查询操作。

[AppleScript] 纯文本查看 复制代码
[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
定义页面查询方法,根据条件查询暂时不实现:
[AppleScript] 纯文本查看 复制代码
 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);  
   }
}





0 个回复

您需要登录后才可以回帖 登录 | 加入黑马