@Test
public void testFindAll(){
List<User> all = userRepository.findAll();
System.out.println(all);}
分页查询
@Test
public void testFindPage(){
//分页参数
int page =1;//从0开始
int size =10;
Pageable pageable = PageRequest.of(page,size);
Page<User> all = userRepository.findAll(pageable);
System.out.println(all);}
修改
@Test
public void testUpdate(){
//查询对象
Optional<User> optional = userRepository.findById("5a92141cb00ffc5a448ff1a0");
if(optional.isPresent()){
User user = optional.get();
//设置要修改值
user.setName("test01");
//...
//修改
User save = userRepository.save(user);
System.out.println(save);}}
根据条件查询一条记录
//根据Unionid查询
@Test
public void testfindByUnionid(){
User user = userRepository.findByUnionid("o8-ysvwSkjQWJIWbGbcarQytJyes");
System.out.println(user);}
根据条件查询多条记录
//根据Unionid查询
@Test
public void testfindByOpenid(){
List<User> userList = userRepository.findByOpenid("666");
System.out.println(userList);}
条件查询+分页
//自定义条件查询测试
//查询openid=666
@Test
public void testFindAllByExample(){
//分页参数
int page =0;//从0开始
int size =10;
Pageable pageable = PageRequest.of(page,size);
//条件值对象
User user= new User();
//要查询5a751fab6abb5044e0d19ea1站点的页面
user.setOpenid("666");
//条件匹配器
ExampleMatcher exampleMatcher = ExampleMatcher.matching();
//定义Example
Example<User> example = Example.of(user,exampleMatcher);
Page<User> all = userRepository.findAll(example, pageable);
List<User> content = all.getContent();
System.out.println(content);}
条件查询(包含关键字)+分页
/**
* 查询openid包含6记录(包含分页) 方法1
*/
@Test
public void testFindAllByExample2(){
//分页参数
int page =0;//从0开始
int size =10;
Pageable pageable = PageRequest.of(page,size);
//条件值对象
User user= new User();
//设置页面别名
user.setOpenid("6");
//条件匹配器
ExampleMatcher exampleMatcher = ExampleMatcher.matching();
exampleMatcher = exampleMatcher.withMatcher("openid", ExampleMatcher.GenericPropertyMatchers.contains());//包含关键字
//定义Example
Example<User> example = Example.of(user,exampleMatcher);
Page<User> all = userRepository.findAll(example, pageable);
List<User> content = all.getContent();
System.out.println(content);}
/**
* 查询openid包含6记录(包含分页) 方法2
*/
@Test
public void testFindAllByExample3(){
//分页参数
int page =0;//从0开始
int size =10;
Pageable pageable = PageRequest.of(page,size);
//条件值对象
User user= new User();
//设置页面别名
user.setOpenid("6");
//条件匹配器
// ExampleMatcher exampleMatcher = ExampleMatcher.matching();
// exampleMatcher = exampleMatcher.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());//包含关键字
ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("openid", ExampleMatcher.GenericPropertyMatchers.contains());
ExampleMatcher.GenericPropertyMatchers.contains();//包含关键字
ExampleMatcher.GenericPropertyMatchers.startsWith();//前缀匹配
//定义Example
Example<User> example = Example.of(user,exampleMatcher);
Page<User> all = userRepository.findAll(example, pageable);
List<User> content = all.getContent();
System.out.println(content);}
/**
* 查询openid以oK前缀记录(包含分页)
*/
@Test
public void testFindAllByExample4(){
//分页参数
int page =0;//从0开始
int size =10;
Pageable pageable = PageRequest.of(page,size);
//条件值对象
User user= new User();
//设置页面别名
user.setOpenid("oK");
//条件匹配器
ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("openid", ExampleMatcher.GenericPropertyMatchers.startsWith());//前缀匹配
//ExampleMatcher.GenericPropertyMatchers.contains() 包含关键字
// ExampleMatcher.GenericPropertyMatchers.startsWith()//前缀匹配
//定义Example
Example<User> example = Example.of(user,exampleMatcher);
Page<User> all = userRepository.findAll(example, pageable);
List<User> content = all.getContent();
System.out.println(content);}
/**
* 查询openid包含66记录,name=11,type=1的记录(包含分页)
*/
@Test
public void testFindAllByExample6(){
//分页参数
int page =0;//从0开始
int size =10;
Pageable pageable = PageRequest.of(page,size);
//条件值对象
User user= new User();
//要查询5a751fab6abb5044e0d19ea1站点的页面
user.setOpenid("66");
//设置模板id条件
user.setName("11");
//设置页面别名
user.setType("1");
//条件匹配器
ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("openid", ExampleMatcher.GenericPropertyMatchers.contains());//包含关键字
//定义Example
Example<User> example = Example.of(user,exampleMatcher);
Page<User> all = userRepository.findAll(example, pageable);
List<User> content = all.getContent();
System.out.println(content);}