springBoot + myBatis+PageHelper分页查询

springBoot + myBatis+PageHelper分页查询

  1. 加载依赖包

     <!-- 分页插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>4.1.0</version>
    </dependency>
    

2.在app文件下main方法后加一个Bean方法也可以单独写类,但要记得扫描

 @Bean
public PageHelper pageHelper() {
    PageHelper pageHelper = new PageHelper();
    Properties p = new Properties();
    p.setProperty("offsetAsPageNum", "true");
    p.setProperty("rowBoundsWithCount", "true");
    p.setProperty("reasonable", "true");
    p.setProperty("dialect", "mysql");
    p.setProperty("supportMethodsArguments", "false");
    p.setProperty("pageSizeZero", "true");
    pageHelper.setProperties(p);
    return pageHelper;
}

3.分页的类

	package com.lbl.entity;
	/**
	 * 分页bean
	 */
	
import java.util.List;

public class PageBean<T> {
    // 当前页
    private Integer currentPage = 1;
    // 每页显示的总条数
    private Integer pageSize = 10;
    // 总条数
    private Integer totalNum;
    // 是否有下一页
    private Integer isMore;
    // 总页数
    private Integer totalPage;
    // 开始索引
    private Integer startIndex;
    // 分页结果
    private List<T> items;

public PageBean() {
    super();
}

public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
    super();
    this.currentPage = currentPage;
    this.pageSize = pageSize;
    this.totalNum = totalNum;
    this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize;
    this.startIndex = (this.currentPage-1)*this.pageSize;
    this.isMore = this.currentPage >= this.totalPage?0:1;
}

public Integer getCurrentPage() {
    return currentPage;
}

public void setCurrentPage(Integer currentPage) {
    this.currentPage = currentPage;
}

public Integer getPageSize() {
    return pageSize;
}

public void setPageSize(Integer pageSize) {
    this.pageSize = pageSize;
}

public Integer getTotalNum() {
    return totalNum;
}

public void setTotalNum(Integer totalNum) {
    this.totalNum = totalNum;
}

public Integer getIsMore() {
    return isMore;
}

public void setIsMore(Integer isMore) {
    this.isMore = isMore;
}

public Integer getTotalPage() {
    return totalPage;
}

public void setTotalPage(Integer totalPage) {
    this.totalPage = totalPage;
}

public Integer getStartIndex() {
    return startIndex;
}

public void setStartIndex(Integer startIndex) {
    this.startIndex = startIndex;
}

public List<T> getItems() {
    return items;
}

public void setItems(List<T> items) {
    this.items = items;
  }
}

4.Mapper(dao)接口的一些注解方法,数据库处理

@Mapper
public interface UserMapper {
    //获取用户信息
    @Select("select * from user where ID = 1")
    User getUser();
    @Select("select * from user")
    List<User> findAll();
    @Select("select count(1) from user")
    Integer countItem();

}

5.逻辑处理层,接口以及其实现
a.接口

@Service
@Transactional//事务注解暂时没用
@CacheConfig(cacheNames = "userCache")
public interface IUserService {

     User getUser(int id);

     List<User> findAll();

     List<User> findItemByPage(Integer currentPage,Integer pageSize);
}

b.实现类:注意实现类也要加@Service注解,否则也会扫描不到

@Service
public class UserServiceImpl implements IUserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUser(int id) {
        logger.info("获取数据 : service");
        User entity = userMapper.getUser();
        logger.info("数据获取成功 : service");
        return entity;
    }
    
    @Override
    public List<User>  findAll(){
        List<User> users = userMapper.findAll();
        return users;
    }

    @Override
    public List<User> findItemByPage(Integer currentPage, Integer pageSize) {
        //设置分页信息,分别是当前页数和每页显示的总记录数【记住:必须在mapper接口中的方法执行之前设置该分页信息】
        PageHelper.startPage(currentPage, pageSize);

        List<User> allItems = userMapper.findAll();        //全部商品
        int countNums = userMapper.countItem();            //总记录数
        PageBean<User> pageData = new PageBean<>(currentPage, pageSize, countNums);
        pageData.setItems(allItems);
        return pageData.getItems();
    }

    @Override
    public Integer countItem() {
        return userMapper.countItem();
    }	
}

6.Controllle 的控制层就只需接请求,前端传参 即可

/**
     * 商品分页功能(集成mybatis的分页插件pageHelper实现)
     * @param currentPage    :当前页数
     * @param pageSize        :每页显示的总记录数
     * @return
     */
    @RequestMapping("/findByPage")
    @ResponseBody
    public List<User> itemsPage(int currentPage,int pageSize){
        return userService.findItemByPage(currentPage, pageSize);
    }

7.效果显示,在地址栏传参,即可控制第几页,每页显示几条(当然可以不传,这就需要进一步的逻辑处理,根据需求来,不传,或者传错,给一个默认值,否则就会报错)

在这里插入图片描述

8.emmm。。。暂时差不多了。。。往后继续加油!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值