SSM:PageHelper分页查询

1.使用逆向工程生成相关的pojo、mapper,并拷贝到相关的位置,配置好

  

2、在pom文件中引入相关的依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
</dependency>

3.在SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <!--使用pageHelper插件-->
   <plugins>
      <!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
      <plugin interceptor="com.github.pagehelper.PageHelper">
         <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
         <property name="dialect" value="mysql"/>
      </plugin>
   </plugins>
</configuration>

4.1.编写servevice接口层---pageResult返回给controller的查询结果

package com.pinyougou.sellergoods.service;
import com.pinyougou.pojo.TbBrand;
import entity.PageResult;

import java.util.List;
/**
 * author:admin
 * Data:2018/6/14
 * Description
 */
public interface BrandService {
   

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    public PageResult findPage(int page,int size);
}

4.2service接口实现层(tbBrands包含着分页查询的结果信息--根据需要取出相关的信息)

@Service
public class BrandServiceImpl  implements BrandService {
    @Autowired
    TbBrandMapper tbBrandMapper;

    @Override
    public PageResult findPage(int pageNum, int  pageSize) {
        PageHelper.startPage( pageNum, pageSize);
        Page<TbBrand> tbBrands = (Page<TbBrand>) tbBrandMapper.selectByExample(null);

        long total = tbBrands.getTotal();
        List<TbBrand> result = tbBrands.getResult();


        return new PageResult(total,result);
    }

5.编写controller层(如果前台未给出查询页数和每页size,则使用默认)

package com.pinyougou.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
import entity.PageResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * author:admin
 * Data:2018/6/15
 * Description
 */

/**
 * 品牌controller
 * @author Administrator
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Reference
    private BrandService brandService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestParam(defaultValue = "1") int page ,@RequestParam(defaultValue = "10") int size){
        return  brandService.findPage(page ,size);
    }

6.测试使用Postman

启动相关工程输入访问路径

猜你喜欢

转载自blog.csdn.net/weixin_36276193/article/details/80722767