Java的新项目学成在线笔记-day1(七)

news/2024/7/3 6:24:03

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编写测试类
Java的新项目学成在线笔记-day1(七)
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);  
   }
}

转载于:https://blog.51cto.com/13517854/2332917


http://www.niftyadmin.cn/n/4002367.html

相关文章

写作词语_平等

success and failure 成功与失败during a tough time在一个艰难的时期be almost entirely controlled by men几乎完全由男子控制firmly believe坚信is supposed to be an enlightened age被认为是一个开明的时代average man and average woman普通的男人和普通的女人adult soci…

Python学习【第17篇】:网络编程之粘包

网络编程-之------粘包现象一、什么是粘包 须知&#xff1a;只有TCP有粘包现象&#xff0c;UDP永远不会粘包 粘包不一定会发生 如果发生了&#xff1a;1.可能是在客户端已经粘了 2.客户端没有粘&#xff0c;可能是在服务端粘了 首先需要掌握一个socket收发消息的原理 应用程序所…

小作文―描写文

小作文―描写文 曲线图 数据典型&#xff0c;语言典型简洁 第一段&#xff1a;2句话&#xff0c;考官就知道图表全貌&#xff0c;考官手里没有图表 第一句&#xff1a;介绍图表&#xff0c;改写原文&#xff0c;4处&#xff0c;变成自己的话 1. below去掉; 2. 动词shows换…

Github开源安全工具集合

2019独角兽企业重金招聘Python工程师标准>>> 本仓库收集的初衷是为向各类企业安全从业人员提供在企业信息安全防护体系建设过程中可以参考的开源安全扫描工具&#xff0c;以期望企业能够利用这些扫描器对自身业务进行自检&#xff0c;从而提高自身业务安全性。集合g…

在linux服务器安装windows,在Windows操作系统中安装Linux系统

很多人对我写下面的东西已不屑一看了&#xff0c;但我写这个帖子的原因是&#xff1a;很多在Windows下想安装Linux的人不知如何开始&#xff0c;如何下手安装。等进入到安装界面又出现了很问题。不得不中途放弃。好了&#xff0c;从我做起&#xff0c;开始新手上路!我想要从win…

大作文 第一段

2011年7月9日 There are many extinct animal species in the world nowadays. Some people say we should protect these animals from dying out, while others say we should concentrate more on problems of human beings. Discuss both opinions and give your personal…

口语题目

Part 1考题总结 考题总结&#xff1a; Self-introduction and personal information What’s your name? What does it mean to you? What names are popular in your culture? What name would you like to give to your own child in future? Are you a student or …

推动新能源汽车,传统车企、科技公司、媒体成三辆马车头?

“贾老板”的话题贯穿了2018年整年&#xff0c;最近又与恒大“分手”&#xff0c;甚至还闹上了法庭。但相关报道称&#xff0c;11月29日&#xff0c;香港国际仲裁中心驳回了贾跃亭要求剥夺恒大资产抵押权的申请&#xff0c;“贾老板”的造车梦又耽搁下来了。 大洋那头的新能源汽…