JAVA语言之Java的新项目学成在线笔记-day1(七)
小标 2019-03-04 来源 : 阅读 652 评论 0

摘要:本文主要向大家介绍了JAVA语言之Java的新项目学成在线笔记-day1(七),通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言之Java的新项目学成在线笔记-day1(七),通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

JAVA语言之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);  
   }
}

   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程