mybaties分页
生活随笔
收集整理的這篇文章主要介紹了
mybaties分页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先引入jar包:
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId> </dependency>
然后在mybatis配置文件中配置:
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><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>
?
代碼中使用:
Service
package com.pinyougou.sellergoods.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import com.alibaba.dubbo.config.annotation.Service; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.pinyougou.mapper.TbBrandMapper; import com.pinyougou.pojo.TbBrand; import com.pinyougou.sellergoods.service.BrandService;import entity.PageResult;@Service public class BrandServiceImpl implements BrandService {@Autowiredprivate TbBrandMapper brandMapper; @Overridepublic PageResult findPage(int pageNum, int pageSize) {//聲明下面的查詢要使用分頁插件 PageHelper.startPage(pageNum, pageSize);//查詢方法一:直接將查詢結果強轉成 page對象Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(null);return new PageResult(page.getTotal(), page.getResult()); //查詢方法二:將查詢結果封裝成pageInfo對象 // List<TbBrand> list = brandMapper.selectByExample(null); // PageInfo<TbBrand> pageInfo = new PageInfo<>(list); // return new PageResult(pageInfo.getTotal(), pageInfo.getList()); }}
?
Controller:
package com.pinyougou.manager.controller;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.UUID;import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import com.alibaba.dubbo.config.annotation.Reference; import com.pinyougou.pojo.TbBrand; import com.pinyougou.sellergoods.service.BrandService;import entity.PageResult;@RestController @RequestMapping("/brand") public class BrandController {@Referenceprivate BrandService brandService;/***<p>Description: 分頁查詢<p>* @date 2018年11月19日* @param page 當前頁碼* @param size 每頁記錄條數* @return*/@RequestMapping("/findPage")public PageResult findPage(int page,int size) {return brandService.findPage(page,size);}
?
測試:
?
轉載于:https://www.cnblogs.com/libin6505/p/9983222.html
總結
以上是生活随笔為你收集整理的mybaties分页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python关于字典的操作
- 下一篇: 20181113-2 每周例行报告