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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

【郑州校区】学成在线 第1天 讲义-项目概述 CMS接口开发 六

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

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中定义了很多现成的方法,如savedelete等,通过下边的代码来测试这里父类方法。
此小节内容请同学们自行测试。
6.2.3.1 添加

[AppleScript] 纯文本查看 复制代码
//添加
@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);
}

6.2.3.2 删除
[AppleScript] 纯文本查看 复制代码
 //删除
@Test
public void testDelete() {
cmsPageRepository
.deleteById("5b17a2c511fe5e0c409e5eb3");}


6.2.3.3 修改
[AppleScript] 纯文本查看 复制代码
 //修改
@Test
public void testUpdate() {
Optional<CmsPage> optional =
cmsPageRepository
.findOne(
"
5b17a34211fe5e2ee8c116c9
"
);
if(optional.isPresent()){
CmsPage cmsPage
=
optional.
get();
cmsPage.setPageName(
"
测试页面01
"
);
cmsPageRepository
.save(cmsPage);
}
}


关于Optional
Optionaljdk1.8引入的类型,Optional

个容器对象,它包括了我们需要的对象,使用isPresent方法判断所包
含对象是否为空,isPresent方法返回false则表示Optional包含对象为空,否则可以使用get()取出对象进行操作。
Optional的优点是:
1、提醒你非空判断。
2、将对象非空检测标准化。
6.2.3.4 自定义Dao方法
Spring Data JPA

Spring Data mongodb也提供自定义方法的规则,如下:
按照fifindByXXXfifindByXXXAndYYYcountByXXXAndYYY等规则定义方法,实现查询操作。

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


0 个回复

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