分页:

PageHelper的优点是,分页和Mapper.xml完全解耦。实现方式是以插件的形式,对Mybatis执行的流程进行了强化,添加了总数count和limit查询。属于物理分页。

一、首先注入依赖:

  <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>

二、配置xml引入插件:

 <!--插件 分页-->
<!--<plugins>-->
<!--<plugin interceptor=""></plugin>-->
<!--</plugins>-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"></property>
</plugin>
</plugins>

三、查询时调用:指定页码(pageNum)和每页的大小(pageSize)分页,pageNum - 第N页, pageSize - 每页M条数

  PageHelper.startPage([pageNum],[pageSize]);

  List<?> pagelist = queryForList( xxx.class, "queryAll" , param);

        PageHelper.startPage(2,2);

四、显示某些值:

PageHelper的其他API

String orderBy = PageHelper.getOrderBy();    //获取orderBy语句

Page<?> page = PageHelper.startPage(Object params);

Page<?> page = PageHelper.startPage(int pageNum, int pageSize);

Page<?> page = PageHelper.startPage(int pageNum, int pageSize, boolean isCount);

Page<?> page = PageHelper.startPage(pageNum, pageSize, orderBy);

Page<?> page = PageHelper.startPage(pageNum, pageSize, isCount, isReasonable);    //isReasonable分页合理化,null时用默认配置

Page<?> page = PageHelper.startPage(pageNum, pageSize, isCount, isReasonable, isPageSizeZero);    //isPageSizeZero是否支持PageSize为0,true且pageSize=0时返回全部结果,false时分页,null时用默认配置

  PageInfo pageInfo=new PageInfo(search);
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("当前页数:"+pageInfo.getPageNum());
System.out.println("显示的条数:"+pageInfo.getPageSize());
System.out.println("最后一条是第:"+pageInfo.getEndRow());
System.out.println("toString:"+pageInfo.getList());

LRU算法缓存:

把数据预先加载到内存中,当真正需要数据的时候,访问速度就会加快

加快程序运行的速度

1) 一级缓存
session SqlSession 级别缓存, 默认开启,

2) 二级缓存
SqlSessionFactory 级别缓存, 可以跨session存在, 配置
对象要实现 implements Serializable ,序列化接口

一级缓存和二级缓存区别 ?
二级缓存 范围广,存在时间久
二级缓存,不建议使用, 不会变的数据,量可控, 配置相关数据,菜单

缓存策略:
如果数据量大于缓存的大小时候,如果处理?

FIFO: 先进先出
LRU: 最近最常使用

    <!--配置算法缓存-->
<cache
eviction="LRU"
flushInterval="60000"
size="1024"
readOnly="true"
/>
 <settings>
<setting name="cacheEnabled" value="true"/>
</settings>

源码:

HouseDAO.xml:
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.etc.dao.HouseDAO"> <!--配置算法缓存-->
<cache
eviction="LRU"
flushInterval="60000"
size="1024"
readOnly="true"
/> <!--查询单个条件 相当于switch=choose when=case otherwise=default-->
<select id="searchSim" resultType="house">
select * from t_house
<where>
<choose>
<when test="title!=null">
title like '%${title}%'
</when>
<when test="price!=null">
price=#{price}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>
<!--查询多个条件 if 如果存在就拼接
select * from t_house where title like '%?%' and price=?
-->
<select id="searchOdd" resultType="house">
select * from t_house
<where>
<if test="title!=null">
title like '%${title}%'
</if>
<if test="price!=null">
and price=#{price}
</if>
</where>
</select>
<!--查询in collection集合名 item:参数名 open close=() separator用逗号拼接
select * from t_house where id in (1,2,3)
-->
<select id="searchByIds" resultType="house">
select * from t_house where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<!--更新(只更新存在的值)-->
<update id="update">
update t_house
<set>
<if test="title!=null">
title=#{title},
</if>
<if test="price!=null">
price=#{price}
</if>
</set>
where id =#{id}
</update> </mapper>

mybatis-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> <settings>
<setting name="cacheEnabled" value="true"/>
</settings> <!-- 别名 -->
<typeAliases>
<package name="com.etc.entity"></package>
</typeAliases> <!--插件 分页-->
<!--<plugins>-->
<!--<plugin interceptor=""></plugin>-->
<!--</plugins>-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"></property>
</plugin>
</plugins> <!-- 配置环境变量 -->
<!-- 开发 测试 预生产 生产 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://127.0.0.1:3310/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments> <!-- 配置mappers -->
<mappers>
<mapper resource="HouseDAO.xml"></mapper>
</mappers> </configuration>
HouseTest:
 package com.etc.dao;

 import com.etc.entity.House;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; public class HouseTest { @Test
public void test() throws IOException {
//加载配置文件 会话工厂
InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); //会话 ==相当于数据库连接
SqlSession session=sqlSessionFactory.openSession(); HouseDAO houseDAO=session.getMapper(HouseDAO.class); House house=new House();
// house.setTitle("he");
// house.setPrice(1300.0);
// house.setId(1); // List<House> search=houseDAO.searchSim(house); //分页插件使用
PageHelper.startPage(2,2);
List<House> search=houseDAO.searchOdd(house);
// List<House> search=houseDAO.searchByIds(Arrays.asList(1,2,3));
// houseDAO.update(house);
for (House h:search)
System.out.println(h); //实例化这个才能使用下边的条件查询
PageInfo pageInfo=new PageInfo(search);
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("当前页数:"+pageInfo.getPageNum());
System.out.println("显示的条数:"+pageInfo.getPageSize());
System.out.println("最后一条是第:"+pageInfo.getEndRow());
System.out.println("toString:"+pageInfo.getList()); session.commit();
session.close(); // System.out.println("第二次查询");
//
// SqlSession session2=sqlSessionFactory.openSession();
//
// HouseDAO houseDAO2=session2.getMapper(HouseDAO.class);
//
// List<House> search2=houseDAO2.searchOdd(house);
// for (House h:search2)
// System.out.println(h);
//
// session2.commit();
// session2.close(); }
}

mybatis PageHelper分页插件 和 LRU算法缓存读取数据的更多相关文章

  1. mybatis pagehelper分页插件使用

    使用过mybatis的人都知道,mybatis本身就很小且简单,sql写在xml里,统一管理和优化.缺点当然也有,比如我们使用过程中,要使用到分页,如果用最原始的方式的话,1.查询分页数据,2.获取分 ...

  2. mybatis pageHelper 分页插件使用

    转载大神 https://blog.csdn.net/qq_33624284/article/details/72828977 把插件jar包导入项目(具体上篇有介绍http://blog.csdn. ...

  3. 关于Spring+mybatis+PageHelper分页插件PageHelper的使用策略

    把插件jar包导入项目(具体上篇有介绍http://blog.csdn.net/qq_33624284/article/details/72821811) spring-mybatis.xml文件中配 ...

  4. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  5. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  6. Mybatis的分页插件PageHelper

    Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschina. ...

  7. SpringBoot集成MyBatis的分页插件 PageHelper

    首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...

  8. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  9. Mybatis之分页插件pagehelper的简单使用

    最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...

随机推荐

  1. Directx11教程(6) 画一个简单的三角形(2)

    原文:Directx11教程(6) 画一个简单的三角形(2)      在上篇教程中,我们实现了在D3D11中画一个简单的三角形,但是,当我们改变窗口大小时候,三角形形状却随着窗口高宽比例改变而改变, ...

  2. WPF/Silverlight深度解决方案:(六)HLSL自定义渲染特效之完美攻略(上)

    原文:WPF/Silverlight深度解决方案:(六)HLSL自定义渲染特效之完美攻略(上) Shader Effect种位图特效及2种渲染特效,而Silverlight中仅有这2种渲染特效: Bl ...

  3. iOS从零开始 Code Review

    http://www.cocoachina.com/ios/20151117/14208.html 这篇帖子不是通篇介绍Code Review的方法论, 而是前大段记录了我们团队怎么从没有这个习惯到每 ...

  4. No PostCSS Config found解决办法

    npm install报错 Module build failed: Error: No PostCSS Config found 解决办法是同级package.json文件新建postcss.con ...

  5. docker在windows下的安装

    Docker for Windows会默认包含两个引擎containers(linux和windows) 1. 下载Docker for Windows,https://docs.docker.com ...

  6. 洛谷P3324 [SDOI2015]星际战争

    题目:洛谷P3324 [SDOI2015]星际战争 思路: 类似<导弹防御塔>,因为题目保证有解,花费时间小于最终答案时一定无法消灭所有敌人,只要花费时间大于等于最终答案都可以消灭所有敌人 ...

  7. 2019-4-7-VisualStudio-解决方案筛选器-slnf-文件

    title author date CreateTime categories VisualStudio 解决方案筛选器 slnf 文件 lindexi 2019-04-07 11:34:59 +08 ...

  8. 注意特殊情况!最长上升子序列!!poj2533

    poj 2533 简单的动归.用O(n^2)的算法也能过.但是有个细节!刚开始ans初始化为0时是错的!!!要初始化为1.因为只有1个数的时候,下面的循环是不会执行的.....或者特判.. #incl ...

  9. poj 1085 Triangle War (状压+记忆化搜索)

    Triangle War Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2685   Accepted: 1061 Desc ...

  10. Spring读取mybatis在多个jar包下的的mapper文件

     刚开始的时候我的配置文件在同名目录下都是在/mapper下,导致只能读取一个jar中的mapper文件.先解决如下: 1.将mapper文件放在不能放在同名的目录下.        比如:user. ...