SSM:PageHelper分頁查詢
阿新 • • 發佈:2018-11-11
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" ?> <!DOCTYPEconfiguration 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
啟動相關工程輸入訪問路徑