1.  引入Maven依赖

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.cjs.example</groupId>
<artifactId>cjs-mybatis-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>cjs-mybatis-example</name>
<description></description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> <!-- http://www.mybatis.org/generator/configreference/xmlconfig.html -->
<!-- http://www.mybatis.org/generator/running/runningWithMaven.html -->
<!-- mvn mybatis-generator:generate -->
<!-- mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</plugin>
</plugins>
</build> </project>

2.  生成Mapper文件

在src/main/resources下创建一个generatorConfig.xml文件,然后在终端命令行下执行  mvn mybatis-generator:generate 即可自动生成

具体参见  http://www.mybatis.org/generator/running/runningWithMaven.html

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <!-- http://www.mybatis.org/generator/configreference/xmlconfig.html --> <generatorConfiguration>
<classPathEntry location="C:/Users/Administrator/.m2/repository/mysql/mysql-connector-java/5.1.46/mysql-connector-java-5.1.46.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://10.123.52.189:3306/oh_coupon"
userId="devdb"
password="d^V$0Fu!/6-&lt;">
</jdbcConnection> <javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <javaModelGenerator targetPackage="com.cjs.example.model" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.cjs.example.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator> <table tableName="tb_coupon" domainObjectName="Coupon" >
<ignoreColumn column="FRED" />
</table> </context>
</generatorConfiguration>

3.  application.yml配置

spring:
datasource:
url: jdbc:mysql://10.123.52.189:3306/oh_coupon
username: devdb
password: d^V$0Fu!/6-<
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.cjs.example.model
mapper-locations: classpath:mapper/*.xml
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
row-bounds-with-count: true
logging:
level:
com.cjs.example.dao: debug

4.  PageHelper用法

具体用法文档中写得比较详细了,这里只结合实际项目情况,给出演示:

参见

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/en/HowToUse.md

https://github.com/pagehelper/Mybatis-PageHelper

Mapper

 package com.cjs.example.dao;

 import com.cjs.example.model.Coupon;
import com.cjs.example.model.CouponExample;
import java.util.List; import com.github.pagehelper.PageRowBounds;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository; @Repository
@Mapper
public interface CouponMapper { List<Coupon> selectByExample(CouponExample example); List<Coupon> selectByExample(CouponExample example, PageRowBounds pageRowBounds); }

Service

 package com.cjs.example.service.impl;

 import com.cjs.example.dao.CouponMapper;
import com.cjs.example.model.Coupon;
import com.cjs.example.model.CouponExample;
import com.cjs.example.service.CouponService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; /**
* https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/en/HowToUse.md
*/
@Service
public class CouponServiceImpl implements CouponService { @Autowired
private CouponMapper couponMapper; /**
* 静态方法startPage
*/
@Override
public List<Coupon> getCouponListByPage(CouponExample couponExample, Integer pageNum, Integer pageSize) {
// 在你需要进行分页的 MyBatis 查询方法前调用 PageHelper.startPage 静态方法即可,紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。
// 只要你可以保证在 PageHelper 方法调用后紧跟 MyBatis 查询方法,这就是安全的
PageHelper.startPage(pageNum, pageSize);
return couponMapper.selectByExample(couponExample);
} /**
* 分页时,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>
* 因为 public class Page<E> extends ArrayList<E> implements Closeable
*/
@Override
public Page<Coupon> getCouponListByPage1(CouponExample couponExample, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Coupon> list = couponMapper.selectByExample(couponExample);
if (null != list) {
Page<Coupon> page = (Page<Coupon>) list;
System.out.println(page);
return page;
}
return null;
} /**
* 用PageRowBounds
*/
@Override
public List<Coupon> getCouponListByPage2(CouponExample couponExample, Integer pageNum, Integer pageSize) {
PageRowBounds pageRowBounds = new PageRowBounds(pageNum, pageSize);
List<Coupon> couponList = couponMapper.selectByExample(couponExample, pageRowBounds); System.out.println(pageRowBounds.getTotal()); Page<Coupon> page = (Page<Coupon>) couponList;
System.out.println(page); return couponList;
} @Override
public Page<Coupon> getCouponListByPage3(CouponExample couponExample, Integer pageNum, Integer pageSize) {
Page<Coupon> page = PageHelper.startPage(pageNum, pageSize).doSelectPage(()->couponMapper.selectByExample(couponExample));
System.out.println(page);
return page;
} /**
* 方法参数
*/
@Override
public PageInfo<Coupon> getCouponListByPage4(CouponExample couponExample, Integer pageNum, Integer pageSize) {
PageInfo<Coupon> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(()->couponMapper.selectByExample(couponExample));
System.out.println(pageInfo);
return pageInfo;
} /**
* PageInfo
*/
@Override
public PageInfo<Coupon> getCouponListByPage5(CouponExample couponExample, Integer pageNum, Integer pageSize) {
List<Coupon> list = couponMapper.selectByExample(couponExample);
if (null == list) {
return null;
}
PageInfo<Coupon> pageInfo = new PageInfo<>(list);
System.out.println(pageInfo);
return pageInfo;
} @Override
public Page<Coupon> getCouponListByPage6(CouponExample couponExample, Integer offset, Integer limit) {
return (Page<Coupon>) couponMapper.selectByExample(couponExample, new PageRowBounds(offset, limit));
}
}

Controller

 package com.cjs.example.controller;

 import com.cjs.example.domain.PageBean;
import com.cjs.example.model.Coupon;
import com.cjs.example.model.CouponExample;
import com.cjs.example.service.CouponService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
@RequestMapping("/coupon")
public class CouponController { @Autowired
private CouponService couponService; @RequestMapping("/list")
public List<Coupon> list() {
CouponExample example = new CouponExample();
return couponService.getCouponListByPage(example, 1, 5);
} @RequestMapping("/list2")
public List<Coupon> list2() {
CouponExample example = new CouponExample();
return couponService.getCouponListByPage2(example, 0, 5);
} @RequestMapping("/list3")
public List<Coupon> list3() {
CouponExample example = new CouponExample();
return couponService.getCouponListByPage3(example, 1, 5);
} @RequestMapping("/list4")
public PageInfo<Coupon> list4() {
CouponExample example = new CouponExample();
return couponService.getCouponListByPage4(example, 1, 5);
} @RequestMapping("/list5")
public PageInfo<Coupon> list5() {
CouponExample example = new CouponExample();
return couponService.getCouponListByPage5(example, 1, 5);
} /**
* Bootstrap Table
* http://bootstrap-table.wenzhixin.net.cn/documentation/
*/
@RequestMapping("/listPage")
public PageBean<Coupon> listPage(Integer offset, Integer limit) {
CouponExample example = new CouponExample();
example.or().andVendorIdEqualTo(10001L).andYnEqualTo(1);
Page<Coupon> page = couponService.getCouponListByPage6(example, offset, limit);
PageBean<Coupon> pageBean = new PageBean<>();
pageBean.setTotal(page.getTotal());
pageBean.setRows(page.getResult());
return pageBean;
}
}

5.  index.html

http://bootstrap-table.wenzhixin.net.cn/documentation/

 <!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Index</title> <link rel="stylesheet" href="/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<link rel="stylesheet" href="/bootstrap-table/bootstrap-table.css"> <script src="/jquery/jquery-3.3.1.min.js"></script>
<script src="/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="/bootstrap-table/bootstrap-table.js"></script>
<script src="/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
</head>
<body>
<div class="row">
<div class="col-xs-6">
<table id="table"></table>
</div>
</div> <script type="text/javascript">
/**
* http://bootstrap-table.wenzhixin.net.cn/documentation/
*/
$('#table').bootstrapTable({
sidePagination: 'server', // 服务器端分页
pagination: true,
pageNumber: 1,
pageSize: 10,
url: '/coupon/listPage',
columns: [{
field: 'id',
title: 'ID',
sortable: true
}, {
field: 'couponName',
title: '名称'
}, {
field: 'couponNum',
title: '数量'
}, {
field: 'couponAmount',
title: '金额'
}, {
field: 'releaseStartTime',
title: '开始时间'
}, {
field: 'releaseStartTime',
title: '结束时间'
}]
});
</script>
</body>
</html>

6.  分页效果

7.  工程结构及源码

代码上传至  https://github.com/chengjiansheng/cjs-mybatis-example.git

8.  小结

个人感觉,还是PageRowBoundsPageHelper.startPage(pageNum, pageSize).doSelectPage()比较实用

MyBatis 分页插件 PageHelper 使用的更多相关文章

  1. Mybatis分页插件PageHelper的配置和使用方法

     Mybatis分页插件PageHelper的配置和使用方法 前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分 ...

  2. Mybatis分页插件PageHelper使用

    一. Mybatis分页插件PageHelper使用  1.不使用插件如何分页: 使用mybatis实现: 1)接口: List<Student> selectStudent(Map< ...

  3. Java SSM框架之MyBatis3(三)Mybatis分页插件PageHelper

    引言 对于使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真不想花双倍的时间写count和select ...

  4. Mybatis学习---Mybatis分页插件 - PageHelper

    1. Mybatis分页插件 - PageHelper说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,S ...

  5. Mybatis分页插件PageHelper的实现

    Mybatis分页插件PageHelper的实现 前言 分页这个概念在做web网站的时候很多都会碰到 说它简单吧 其实也简单 小型的网站,完全可以自己写一个,首先查出数据库总条数,然后按照分页大小分为 ...

  6. 基于Mybatis分页插件PageHelper

    基于Mybatis分页插件PageHelper 1.分页插件使用 1.POM依赖 PageHelper的依赖如下.需要新的版本可以去maven上自行选择 <!-- PageHelper 插件分页 ...

  7. Mybatis分页插件-PageHelper的使用

    转载:http://blog.csdn.net/u012728960/article/details/50791343 Mybatis分页插件-PageHelper的使用 怎样配置mybatis这里就 ...

  8. (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示

    http://blog.csdn.net/yerenyuan_pku/article/details/72774381 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...

  9. springmvc mybatis 分页插件 pagehelper

    springmvc mybatis 分页插件 pagehelper 下载地址:pagehelper 4.2.1 , jsqlparser 0.9.5 https://github.com/pagehe ...

  10. MyBatis 分页插件PageHelper 后台报错

    今天遇到一个问题,使用MyBatis 分页插件PageHelper 进行排序分页后,能正常返回正确的结果,但后台却一直在报错 net.sf.jsqlparser.parser.ParseExcepti ...

随机推荐

  1. 032 Java再次总结

    1.大纲 多线程怎么用的,线程池,事务,传播机制与隔离级别,反射,泛型,数据库引擎的区别,数据库merge,窗口函数,fastJson,JVM调优,GC钩子,Linux的awk,shell,HashM ...

  2. 20175305张天钰Java结对编程四则运算(二)

    Java结对编程四则运算(二) 一.题目描述及要求 Git提交粒度不要太粗,建议一个文件/一个类/一个函数/一个功能/一个bug修复都进行提交,不能一天提交一次,更不能一周一次,参考Commit Me ...

  3. 20165235 实现pwd功能

    20165235 实现pwd功能 要求 学习pwd命令 2.研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3.实现mypwd 测试mypwd 实现过程 pwd是将当前的文件目录 ...

  4. 动态网页获取ajax,post方法,url里面不直接显示参数

    记录一下,爬去ajax数据时,需要注意一下是post方法还是get方法,get方法就正常做就行了,但是post方法的话,需要这样,如下 a = requests.request('post',url) ...

  5. Vue使用过程中常见问题

    目录 一.vue监听不到state数组/json对象内的元素的值的变化,要手动通知触发 二.vue用splice删除多维数组元素导致视图更新失败情况 三.vue项目如何部署到php或者java环境的服 ...

  6. Zathura: 轻巧好用的 PDF 查看器]

    [Zathura: 轻巧好用的 PDF 查看器](https://linuxtoy.org/archives/zathura.html) 这个文件很轻巧,且支持VIM方式的 快捷键

  7. CentOS7上安装Snipe-IT4.6.3详细过程及注意事项

    笔者采用的是CentOS7,先对系统进行Update,然后安装军哥的LNMPA,详情请参考lnmp.org 注意:安装LNMPA前需要修改lnmp.conf中这一行为下面,也就是要安装PHP的File ...

  8. 虚拟机安装Linux系统

    Mware Workstation 12 序列号: 5A02H-AU243-TZJ49-GTC7K-3C61N 步骤一: 右键-->新建虚拟机 步骤二:自定义(高级)-->下一步 步骤三: ...

  9. hashlib模块,shutil,模块 ,,xml 文件解析,configparser,模块,类,什么是类

    1 什么是hash hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值 如果把hash算法比喻为一座工厂 那传给hash算法的内容就是原材料 生成的hash值就是生产出的产品 2.为 ...

  10. Qt5+MSVC2015编译器编译发布的Release程序运行崩溃,如何查找崩溃的原因??

    除了加log信息,还有什么方法?? ==================================2019/4/26============================= 1 常见的c++内 ...