大家好,又见面了,我是你们的朋友全栈君。
service代码
package com.youyou.address.service;
import com.youyou.address.dao.ContacterDao;
import com.youyou.address.dao.ContacterEORepo;
import com.youyou.address.entity.ContacterEO;
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.data.domain.Sort;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class ContacterService {
@Autowired
private ContacterDao dao;
@Autowired
private ContacterEORepo contacterEORepo;
public void insertOne(ContacterEO contacterEO) {
dao.insertOne(contacterEO);
}
public List<ContacterEO> findAll() {
return dao.findAll();
}
/**
* 保存联系人
*
* @param contacterEO
*/
public void saveOne(ContacterEO contacterEO) {
contacterEORepo.save(contacterEO);
}
/**
* 分页查询
* @param page
* @return
*/
public Page<ContacterEO> findByPage(PageRequest page) {
Page<ContacterEO> all = contacterEORepo.findAll(page );
return all;
}
}
controller代码如下:
package com.youyou.address.controller;
import com.youyou.address.entity.ContacterEO;
import com.youyou.address.service.ContacterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Api(description = "联系人接口")
@RestController
@RequestMapping(value = "/contacter")
public class ContacterController {
@Autowired
private ContacterService service;
@ApiOperation(value = "新增")
@GetMapping("/add")
public ContacterEO add(ContacterEO contacterEO) {
System.out.println("添加一个联系人");
service.saveOne(contacterEO);
return contacterEO;
}
@ApiOperation(value = "查询所有")
@GetMapping("/findAll")
public List<ContacterEO> findAll() {
return service.findAll();
}
@ApiOperation(value = "分页查询")
@GetMapping("/findByPage")
public Page<ContacterEO> findByPage(int pageNo, int size ,Sort.Direction direction) {
PageRequest page = PageRequest.of(pageNo, size, Sort.by(direction, "name"));
return service.findByPage(page);
}
}
核心代码就是
PageRequest page = PageRequest.of(pageNo, size, Sort.by(direction, "name"));
pageNo:页码(从0开始)
size:每页条数
direction:排序标识,即ASC或DESC
name:需要排序的字段
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106463.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...