分页查询。

在 Service 层中,调用 getPageData 方法即可:可以将分页方法写在工具类中,让其返回一个包含分页数据的 List 对象。在 Service 层和 DAO 层中,直接调用该方法即可。

下面是 Page 类:

public class Page<T> {
    private int start; // 当前位置
    private int count; // 每页显示的数据数量
    private int total; // 数据总个数
    
    // 默认每页显示5条数据
    private static final int DEFAULT_COUNT = 5;
    
    public Page() {
        this(DEFAULT_COUNT, 0);
    }
    
    public Page(int count, int start) {
        this.count = count;
        this.start = start;
    }
    
    public int getStart() {
        return start;
    }
    
    public void setStart(int start) {
        this.start = start;
    }
    
    public int getCount() {
        return count;
    }
    
    public void setCount(int count) {
        this.count = count;
    }
    
    public int getTotal() {
        return total;
    }
    
    public void setTotal(int total) {
        this.total = total;
    }
    
    public boolean isHasPrevious() {
        return start > 0;
    }
    
    public boolean isHasNext() {
        return start + count < total;
    }
    
    public int getTotalPage() {
        if (total == 0) {
            return 1;
        }
        
        if (total % count == 0) {
            return total / count;
        } else {
            return total / count + 1;
        }
    }
    
    public int getLast() {
        return total - total % count;
    }
    
    public List<T> getPageData(List<T> list) {
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }
        
        int size = list.size();
        int end = start + count;
        if (end > size) {
            end = size;
        }
        
        return list.subList(start, end);
    }
    
    @Override
    public String toString() {
        return "Page [start=" + start + ", count=" + count + ", total=" + total + ", getStart()=" + getStart()
                + ", getCount()=" + getCount() + ", isHasPrevious()=" + isHasPrevious() + ", isHasNext()="
                + isHasNext() + ", getTotalPage()=" + getTotalPage() + ", getLast()=" + getLast() + "]";
    }
}

在 Service 层中,调用 getPageData 方法即可: 



@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public List<User> findUsersByPage(int page, int pageSize) {
        Page<User> pager = new Page<>(pageSize, (page - 1) * pageSize);
        List<User> userList = userDao.listAll();
        pager.setTotal(userList.size());
        return pager.getPageData(userList);
    }
}

在Dao层

   @Override
    public List<User> listAll() {
        return userDatabase;
    }

 

在 Controller 层中,则直接调用 Service 层的方法即可:

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/users")
    public ModelAndView users(@RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int pageSize) {
        ModelAndView modelAndView = new ModelAndView("users");
        List<User> userList = userService.findUsersByPage(page, pageSize);
        modelAndView.addObject("userList", userList);
        return modelAndView;
    }
}

这样的话,就可以在工具类中实现分页逻辑,并且让 Service 层和 DAO 层直接调用。这样就可以减少代码重复,提高代码的复用性

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值