mybaties分页
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                mybaties分页
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                首先引入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"><!-- 設(shè)置數(shù)據(jù)庫(kù)類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六種數(shù)據(jù)庫(kù)--><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) {//聲明下面的查詢要使用分頁(yè)插件 PageHelper.startPage(pageNum, pageSize);//查詢方法一:直接將查詢結(jié)果強(qiáng)轉(zhuǎn)成 page對(duì)象Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(null);return new PageResult(page.getTotal(), page.getResult()); //查詢方法二:將查詢結(jié)果封裝成pageInfo對(duì)象 // 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: 分頁(yè)查詢<p>* @date 2018年11月19日* @param page 當(dāng)前頁(yè)碼* @param size 每頁(yè)記錄條數(shù)* @return*/@RequestMapping("/findPage")public PageResult findPage(int page,int size) {return brandService.findPage(page,size);}
?
測(cè)試:
?
轉(zhuǎn)載于:https://www.cnblogs.com/libin6505/p/9983222.html
總結(jié)
以上是生活随笔為你收集整理的mybaties分页的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: python关于字典的操作
- 下一篇: 大众途锐多少钱啊?
