当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
12 Star 30 Fork 8

魔法编码者X / resultbounds
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

resultbounds - MyBatis分页插件

该插件的可取之处

以简单的方式提供一个优雅的 MyBatis 物理分页功能,不需要修改代码就可以把默认的分页换成物理分页。目前该插件的实现经过几次重构,可读性得到很大的提升,非常适合想了解 MyBatis 分页插件的以及想自己实现一个的同仁。 (不建议用在公司项目上,理由是未经过项目实践)

支持的 java 版本

java1.6+(包括java1.6)

支持的 MyBatis 版本

MyBatis 3.4: 支持  (在 MyBatis-3.4.4 版本下测试通过)
MyBatis 3.3: 支持  (在 MyBatis-3.3.1 版本下测试通过)
MyBatis 3.2: 支持  (在 MyBatis-3.2.8 版本下测试通过)
MyBatis 3.1: 支持  (在 MyBatis-3.1.1 版本下测试通过)
MyBatis 3.0: 不支持(在 MyBatis-3.0.6 版本下测试失败)
MyBatis 2.3: 不支持(在 MyBatis-2.3.5 版本下测试失败)

支持的数据库

目前只实现了 MySql 分页,可以自己实现 Dialect 接口,用来支持其它的数据库分页。

编译代码

编译代码
mvn -DskipTests install

执行测试
1、在数据库里执行文件 src/test/resources/resultbounds-mysql.sql 中的 SQL 语句。
2、把 src/test/resources/mybatis-config.xml 中的用户名和密码替换成自己数据库的用户名和密码。
3、执行测试命令 mvn test

在 MyBatis 配置文件中加上这个分页插件

<configuration>
  ...
  <plugins>
    <plugin interceptor="com.retaiyang.resultbounds.PaginationInterceptor">
      <!-- 可以把 value 属性值替换成自己实现的 Dialect -->
      <property name="dialect" value="com.retaiyang.resultbounds.dialect.MySqlDialect" />
    </plugin>
  </plugins>
  ...
</configuration>

在代码中使用这个插件进行分页的示例

// 示例一
// 获取 SqlSession 对象
SqlSession session = ...
// 从第 4 条开始,取 5 条记录
List<Student> studentList = session.selectList("test.student.getStudentList", null, new RowBounds(3, 5));

// 示例二
// 获取 SqlSession 对象
SqlSession session = ...
// 从第 4 条开始,取 5 条记录
List<Student> studentList = session.getMapper(StudentMapper.class).getStudentList(new RowBounds(3, 5));

在查询的同时统计记录总数

// 示例一
// 获取 SqlSession 对象
SqlSession session = ...
// 设置分页参数从第 4 条开始,取 5 条记录
ResultBounds resultBounds = new ResultBounds(3, 5);
// 获取分页后的查询结果
List<Student> studentList = session.selectList("test.student.getStudentList", null, resultBounds);
// 获取记录总数
long total = resultBounds.getTotal(session);

// 示例二
// 获取 SqlSession 对象
SqlSession session = ...
// 设置分页参数从第 4 条开始,取 5 条记录
ResultBounds resultBounds = new ResultBounds(3, 5);
// 获取分页后的查询结果
List<Student> studentList = session.getMapper(StudentMapper.class).getStudentList(resultBounds);
// 获取记录总数
long total = resultBounds.getTotal(session);

若使用了 mybatis-spring-xxxx.jar 包整合了 Spring

在 MyBatis 配置文件中加上该插件的 Spring 版本

<configuration>
  ...
  <plugins>
    <plugin interceptor="com.retaiyang.resultbounds.spring.TotalCountBeanInterceptor">
      <property name="dialect" value="com.retaiyang.resultbounds.dialect.MySqlDialect" />
    </plugin>
  </plugins>
  ...
</configuration>

在 Spring 配置文件中加上这个

...
<bean class="com.retaiyang.resultbounds.spring.TotalCountBean">
  <property name="sqlSession" ref="sqlSession" />
</bean>
...

使用该插件的 Spring 版本后

...
// 获取记录总数
// 调用 ResultBounds.getTotal 方法的时候不需要给 SqlSession 参数了
long total = resultBounds.getTotal(null);
...

说明该插件各个类是干嘛用的

---- dialect
  |---- Dialect                     生成分页语句、统计记录总数语句的接口
  |---- MySqlDialect                Dialect 接口的 MySql 实现
---- spring
  |---- TotalCountBean
  |---- TotalCountBeanInterceptor   这插件的 spring 版本
---- MyBatisClassCreator            创建 MyBatis 中的类,在 PaginationInterceptor 中需要用它对参数动些手脚
---- PaginationInterceptor          分页的主逻辑在这个类里
---- ResultBounds                   RowBounds 的子类,加了一些实用的方法
---- TotalCount                     用来统计记录总数

该分页插件的原理

通过 Mybatis 提供的插件机制实现拦截器 PaginationInterceptor 拦截查询操作,每次拦截查询操作后通过创建改造过的对象作为查询的参数达到分页的效果。

The MIT License (MIT) Copyright (c) 2017 cc.z Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

resultbounds 是一个 MyBatis 分页插件,支持 MyBatis3.1、3.2、3.3、3.4 版本,不需要修改代码就可以把默认分页换成物理分页。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/magicodex/resultbounds.git
git@gitee.com:magicodex/resultbounds.git
magicodex
resultbounds
resultbounds
master

搜索帮助