您的位置:首页 > 其它

Mybatis分页插件PageHelper简单使用

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

于是就简单的写了一个测试项目

1配置:sqlmap-config.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>
<!-- DEBUG模式开启sql打印 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<plugins>
<plugin interceptor="com.zhongan.tech.cashier.api.intercepter.MybatisLocalCacheInterceptor"></plugin>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

</configuration>



2,编写mapper.xml文件

测试工程就不复杂了,简单的查询一个表,没有条件

<select id="selectByPageAndSelections" resultMap="BaseResultMap">
SELECT *
FROM doc
ORDER BY doc_abstract
</select>


然后在Mapper.java中编写对应的接口

public List<Doc> selectByPageAndSelections();



3,分页

@Service
public class DocServiceImpl implements IDocService {
@Autowired
private DocMapper docMapper;

@Override
public PageInfo<Doc> selectDocByPage1(int currentPage, int pageSize) {
PageHelper.startPage(currentPage, pageSize);
List<Doc> docs = docMapper.selectByPageAndSelections();
PageInfo<Doc> pageInfo = new PageInfo<>(docs);
return pageInfo;
}
}


参考文档说明,我使用了PageHelper.startPage(currentPage, pageSize);

我认为这种方式不入侵mapper代码。

其实一开始看到这段代码时候,我觉得应该是内存分页。其实插件对mybatis执行流程进行了增强,添加了limit以及count查询,属于物理分页

再粘贴一下文档说明中的一段话

4. 什么时候会导致不安全的分页?

PageHelper 方法使用了静态的 ThreadLocal 参数,分页参数和线程是绑定的。

只要你可以保证在 PageHelper 方法调用后紧跟 MyBatis 查询方法,这就是安全的。因为 PageHelper 在 finally 代码段中自动清除了 ThreadLocal 存储的对象。

如果代码在进入 Executor 前发生异常,就会导致线程不可用,这属于人为的 Bug(例如接口方法和 XML 中的不匹配,导致找不到 MappedStatement 时), 这种情况由于线程不可用,也不会导致 ThreadLocal 参数被错误的使用。

但是如果你写出下面这样的代码,就是不安全的用法:

PageHelper.startPage(1, 10);
List<Country> list;
if(param1 != null){
list = countryMapper.selectIf(param1);
} else {
list = new ArrayList<Country>();
}
这种情况下由于 param1 存在 null 的情况,就会导致 PageHelper 生产了一个分页参数,但是没有被消费,这个参数就会一直保留在这个线程上。当这个线程再次被使用时,就可能导致不该分页的方法去消费这个分页参数,这就产生了莫名其妙的分页。

上面这个代码,应该写成下面这个样子:

List<Country> list;
if(param1 != null){
PageHelper.startPage(1, 10);
list = countryMapper.selectIf(param1);
} else {
list = new ArrayList<Country>();
}
这种写法就能保证安全。

如果你对此不放心,你可以手动清理 ThreadLocal 存储的分页参数,可以像下面这样使用:

List<Country> list;
if(param1 != null){
PageHelper.startPage(1, 10);
try{
list = countryMapper.selectAll();
} finally {
PageHelper.clearPage();
}
} else {
list = new ArrayList<Country>();
}
这么写很不好看,而且没有必要。


 


4,结果

controller层中简单的调用然后返回json字符串如下:可以看出,结果基于doc_abstract排序后返回1-10条的数据

自动生成count的写法
//PageHelper.startPage(pageNum, pageSize);
PageHelper.startPage(1, 10);
List<CashierReconDetail> list = cashierReconDetailMapper.queryListOfDay(date);
//返回的pageInfo包含所有的分页信息
PageInfo<CashierReconDetail> pageInfo = new PageInfo<>(list);
//会执行两条sql:
/lect count(1) from cashier_recon_detail
//如果第一条返回为0的话,第二条不执行
/lect …… from cashier_recon_detail limit 0,10
只需要Limit,不需要Count的写法
//最后一个参数表示是否执行“select count(*) from ……”
//false表示不执行,true表示执行。现在的配置是默认执行count
//第一个参数PageNum参数表示当前页,
//第二个PageSize表示每页显示多少条
//最后一个参数可选。
PageHelper.startPage(2, 10,false);
List list = cashierReconDetailPOMapper.getList(vo);
//只会执行一条sql
select …… from cashier_recon_detail limit 0,10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: