
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
com.github.xiwh:mybatis-paginator
Advanced tools
它是一款基于Springboot的高效易用易扩展的Mybatis全自动物理分页插件,提供基础分页、N+1分页、无侵入式物理分页、微侵入式易用分页等方式
mysqlmariadboraclesqlserver>=2012posgresqlsqliteGradle:
compile 'com.github.xiwh:mybatis-paginator:1.0.2'
Maven:
<dependency>
<groupId>com.github.xiwh</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>1.0.0</version>
</dependency>
(必须)将"com.xiwh.paginator"加入到自动扫描路径下,如下
@SpringBootApplication
@ComponentScan({"com.xiwh.paginator","com.example.demo"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
普通分页
public interface SimplePagingMapper {
@Select("SELECT * FROM a")
@NormalPaginator()
SimplePage<Bean> select();
public static void main(String[] args){
SimplePagingMapper mapper = xxx;
int page = 0;
int size = 10;
Paginator.paginate(page,size);
SimplePage<Bean> page = mapper.select();
System.out.println(page);
System.out.println(page.hasLast());
System.out.println(page.hasNext());
System.out.println(page.totalPage());
System.out.println(page.total());
System.out.println(page.size());
System.out.println(page.list());
System.out.println(page.toMap());
for (Bean item:page) {
item.setName(item.getId()+":"+item.getName());
}
}
}
N+1分页
public interface NPlusOnePagingMapper {
@Select("SELECT * FROM a")
@NPlusOnePaginator
NPlusOnePage<Bean> select();
public static void main(String[] args){
NPlusOnePagingMapper mapper = xxx;
Paginator.paginate(0,10);
NPlusOnePage<Bean> page = mapper.select();
System.out.println(page);
System.out.println(page.hasNext());
System.out.println(page.toMap());
for (Bean item:page) {
item.setName(item.getId()+":"+item.getName());
}
}
}
无侵入物理分页
public interface SimplePagingMapper {
@Select("SELECT * FROM a")
@NormalPaginator(cache=true,cacheExpiryTime=3600)
List<Bean> select(RowBounds rowBounds);
}
无侵入物理分页进阶(取分页结构)
public interface SimplePagingMapper {
@Select("SELECT * FROM a")
@NormalPaginator(cache=true,cacheExpiryTime=3600)
List<Bean> select(PagingRowBounds rowBounds);
public static void main(String[] args){
NPlusOnePagingMapper mapper = xxx;
List<Bean> list = mapper.select();
int page = 0;
int size = 10;
PagingRowBounds rowBounds = new PagingRowBounds(page,size);
NormalPaginator page = rowBounds.toNormalPage(NormalPaginator.class, list);
}
}
设置起始页偏移量(用以兼容前端分页参数,通常前端分页从1开始)
public interface SimplePagingMapper {
//第一种方式
@Select("SELECT * FROM a")
@NormalPaginator(pageOffset=1)
List<Bean> select();
//第二种方式(通过RowBounds 的传参方式,注解内pageOffset将不会生效)
@Select("SELECT * FROM a")
@NormalPaginator()
List<Bean> selectRowBounds(PagingRowBounds rowBounds);
public static void main(String[] args){
NPlusOnePagingMapper mapper = xxx;
List<Bean> list = mapper.select();
int page = 0;
int size = 10;
int pageOffset = 1;
boolean forceCounting = true;
PagingRowBounds rowBounds = new PagingRowBounds(page,size,pageOffset, forceCounting);
NormalPaginator page = rowBounds.toNormalPage(NormalPaginator.class, list);
}
}
通过当前GET请求自动注入分页参数
@Mapper
public interface SimplePagingMapper {
@Select("SELECT * FROM a")
@NormalPaginator(auto = true,pageOffset = 1)
SimplePage<Bean> select();
}
@RestController
public class PagingController{
@Autowired
SimplePagingMapper simplePagingMapper;
/**
* http://127.0.0.1:8080/test?page=1&size=20
* {
* "total": 16,
* "size": 10,
* "total_page": 2,
* "has_last": false,
* "has_next": true,
* "page": 1,
* "list": [{
* "id": 1,
* "name": "a",
* "value": "v1"
* }, {
* "id": 2,
* "name": "a",
* "value": "v1"
* }, {
* "id": 3,
* "name": "a",
* "value": "v1"
* }, {
* "id": 4,
* "name": "d",
* "value": "v4"
* }, {
* "id": 5,
* "name": "e",
* "value": "v5"
* }, {
* "id": 6,
* "name": "f",
* "value": "v6"
* }, {
* "id": 7,
* "name": "g",
* "value": "v7"
* }, {
* "id": 8,
* "name": "h",
* "value": "v8"
* }, {
* "id": 9,
* "name": "i",
* "value": "v9"
* }, {
* "id": 10,
* "name": "j",
* "value": "v10"
* }]
* }
*/
@RequestMapping("/test")
public Map test(HttpServletRequest request){
//默认自动读取page 和 size参数,如不存在则取默认值, 参数key和默认值可在配置修改
Map map = simplePagingMapper.select().toMap();
return map;
}
}
启用Count结果缓存
public interface SimplePagingMapper {
@Select("SELECT * FROM a")
//缓存有效时间3600秒
@NormalPaginator(cache=true,cacheExpiryTime=3600)
SimplePage<Bean> select();
}
自定义Count方法
public interface SimplePagingMapper {
@Select("SELECT count(1) FROM a where id != ${bb} or id != #{aa}")
Integer customCount(String aa, Integer bb);
//与Count方法参数必须要完全一致!
@Select("SELECT * FROM a where id != ${bb} or id != #{aa}")
@NormalPaginator(countMethod = "customCount")
SimplePage<Bean> customCountSelect(String aa, Integer bb);
}
自定义Limit语句(兼容XML Mapper)
public interface SimplePagingMapper {
@Select("SELECT count(1) FROM a where id != ${bb} or id != #{aa}")
Integer customCount(String aa, Integer bb);
//开启自定义limit后,必须同时自定义count方法
//提供:offset :limit :end(与oracle等数据库配套使用)三个内置变量
@Select("SELECT * FROM a WHERE id != ${bb} OR id != #{aa} LIMIT :offset,:limit")
@NormalPaginator(customLimit = true, countMethod = "customCount")
SimplePage<Bean> customLimitSelect(String aa, Integer bb);
}
自定义分页返回数据结构
public class MyPagingResult<T> extends NormalPageWrapperBase<T> {
private int total;
private int startOffset;
private int physicalPage;
private int page;
private int size;
private int totalPage;
private List<T> list;
public boolean hasNext(){
return list.size()>=size;
}
public boolean hasLast(){
return physicalPage>0;
}
public int size(){
return size;
}
public int page(){
return page;
}
public int total(){
return total;
}
public int totalPage(){
return totalPage;
}
public List<T> list() {
return list;
}
/**
* Paged callback
* @param list
* @param count
* @param startOffset
* @param physicalPage
* @param size
*/
@Override
public void onInit(List<T> list, int count, int startOffset, int physicalPage, int size) {
this.list = list;
this.total = count;
this.physicalPage = physicalPage;
this.startOffset = startOffset;
this.page = physicalPage + startOffset;
this.size = size;
this.totalPage = total/size+(total%size==0?0:1);
}
}
public interface SimplePagingMapper {
@Select("SELECT * FROM a")
@NormalPaginator()
MyPagingResult<Bean> select();
}
#显示分页SQL
logging.level.com.xiwh.paginator: debug
paginator:
# 根据Get请求自动完成参数注入Key
size-key: size
page-key: page
# 根据Get请求自动注入默认页大小
default-size: 10
MIT License
Copyright (c) 2020 xiwh
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.
FAQs
A powerful and easy-to-use mybatis physical paginator
We found that com.github.xiwh:mybatis-paginator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.