pom依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
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>
结构
代码使用
/**
* 品牌分页
* @param message 通用返回值
* @return
*/
@Override
public Message findPage(Message message) {
try {
//mybatis 分页插件startPage(当前页,当前页大小)
PageHelper.startPage(message.getTotal(),message.getTotalPage());
//mybatis查询所有方法 返回结果强转为Page
//import com.github.pagehelper.Page;
//import com.github.pagehelper.PageHelper;
Page page = (Page) tbBrandMapper.selectByExample(null);
// page.getTotal() 获取数据库总条数
message.setTotal((int) page.getTotal());
//page.getResult() 获取查询到的数据 message.setData 泛型T
message.setData(page.getResult());
// MessageUtils.getMessage 进行封装返回的结果
MessageUtils.getMessage(message, "获取分页品牌分类出错", "获取分页商品分类成功");
} catch (Exception e) {
MessageUtils.getMessage(message, "获取分页品牌分类出错", "获取分页商品分类成功");
}
return message;
}