mybaties分頁

首先引入jar包:java

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

而後在mybatis配置文件中配置:mysql

SqlMapConfig.xmlgit

 

 

<?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>

 

代碼中使用:github

Serviceweb

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 { @Autowired private TbBrandMapper brandMapper;  @Override public 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:spring

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 { @Reference private 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); }

 

測試:sql

相關文章
相關標籤/搜索