分页:

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. webpack学习之—— 依赖图(Dependency Graph) 及 构建目标(Targets)

    Dependency Graph 任何时候,一个文件依赖于另一个文件,webpack 就把此视为文件之间有依赖关系.这使得 webpack 可以接收非代码资源(non-code asset)(例如图像 ...

  2. Python3.7.4入门-6/7错误和异常/类

    6 错误和异常 while True: try: x = int(input("Please enter a number: ")) break except ValueError ...

  3. DirectX11笔记(二)--Direct3D初始化1之基本概念

    原文:DirectX11笔记(二)--Direct3D初始化1之基本概念 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010333737/art ...

  4. php中array_slice和array_splice函数解析方式方法

    array_slice array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_k ...

  5. C++不支持默认的int

    VS: 工程属性->C/C++->命令行->输入 /wd4430

  6. UCloud-201809-001:Redis服务未授权访问漏洞安全预警

    UCloud-201809-001:Redis服务未授权访问漏洞安全预警 尊敬的UCloud用户,您好! 发布时间  2018-09-11更新时间  2018-09-11漏洞等级  HighCVE编号 ...

  7. The World's Top 15 Stock Exchanges by Domestic Market Capitalization

     The World's Top 15 Stock Exchanges by Domestic Market Capitalization in 2008 4 Euronext Belgium, Fr ...

  8. Servlet表单处理

    HttpServletRequest   继承ServletRequest  HttpServletRequest生命周期: 一个HttpServletRequest对象在用户向web服务器发送请求时 ...

  9. Directx11教程(13) D3D11管线(1)

    原文:Directx11教程(13) D3D11管线(1)       从本篇教程开始,我们暂停代码的学习,先来了解一下D3D11的管线,这些管线不涉及具体的硬件,而是着重于理解能够支持D3D11的管 ...

  10. Otracle数据库定时任务--dbms_job

    一.dbms_job涉及到的知识点 1.创建job: variable jobno number; dbms_job.submit(:jobno, --job号 'your_procedure;'-- ...