mybatis PageHelper分页插件 和 LRU算法缓存读取数据
分页:
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算法缓存读取数据的更多相关文章
- mybatis pagehelper分页插件使用
使用过mybatis的人都知道,mybatis本身就很小且简单,sql写在xml里,统一管理和优化.缺点当然也有,比如我们使用过程中,要使用到分页,如果用最原始的方式的话,1.查询分页数据,2.获取分 ...
- mybatis pageHelper 分页插件使用
转载大神 https://blog.csdn.net/qq_33624284/article/details/72828977 把插件jar包导入项目(具体上篇有介绍http://blog.csdn. ...
- 关于Spring+mybatis+PageHelper分页插件PageHelper的使用策略
把插件jar包导入项目(具体上篇有介绍http://blog.csdn.net/qq_33624284/article/details/72821811) spring-mybatis.xml文件中配 ...
- Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
- Mybatis的分页插件PageHelper
Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina. ...
- SpringBoot集成MyBatis的分页插件 PageHelper
首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...
- SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页
SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...
- Mybatis之分页插件pagehelper的简单使用
最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...
随机推荐
- zoj 1028 Flip and Shift(数学)
Flip and Shift Time Limit: 2 Seconds Memory Limit: 65536 KB This puzzle consists of a random se ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- No.6 Verilog 其他论题
(1)任务 **任务类似于一段程序,可以提供一种能力,使设计者可以从设计描述的不同位置执行共同的代码段.任务可以包含时序控制, 可以调用其它任务和函数. 任务的定义格式: task[automat ...
- python 对位运算
- windbg双机调试
win10 测试,当出现下列情况 ,请使用管理员身份运行 设置添加系统环境变量_NT_SYMBOL_PATH 的值为:srv*c:\symbols*http://msdl.microsoft.com ...
- 51nod1196 字符串的数量
用N个不同的字符(编号1 - N),组成一个字符串,有如下要求:(1) 对于编号为i的字符,如果2 * i > n,则该字符可以作为结尾字符.如果不作为结尾字符而是中间的字符,则该字符后面可以接 ...
- Qt添加右键菜单
QAction *hideAction = new QAction(tr(" 隐藏"),this); addAction(hideAction); setContextMenuPo ...
- java memory allocation(转)
Java的运行时数据存储机制 Java程序在运行时需要为一系列的值或者对象分配内存,这些值都存在什么地方?用什么样的数据结构存储?这些数据结构有什么特点?本文试图说明此命题的皮毛之皮毛. 概念 对 ...
- 【JZOJ4814】【NOIP2016提高A组五校联考2】tree
题目描述 给一棵n 个结点的有根树,结点由1 到n 标号,根结点的标号为1.每个结点上有一个物品,第i 个结点上的物品价值为vi. 你需要从所有结点中选出若干个结点,使得对于任意一个被选中的结点,其到 ...
- 【JZOJ4782】【NOIP2016提高A组模拟9.15】Math
题目描述 输入 输出 样例输入 3 5 样例输出 -1 数据范围 解法 观察式子,可以得知整个式子与d(i*j)的奇偶性有关. d(n)为奇数当且仅当n是完全平方数. 对于一个i,如果d(i*j) ( ...