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的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...
随机推荐
- web前端学习(二)html学习笔记部分(11)-- 没有标号记录的知识合集
这一部分内容相对比较简单,就不按规矩排序了.(主要是网站上也没有这一部分内容的排序) 1. html5的 非主体结构元素 学习笔记(1)里面记录过. 2. html5表单提交和PHP环境搭建 1. ...
- CF981H K Paths
CF981H K Paths 题解 一道不错的分治ntt题目 题目稍微转化一下,就是所有k条链的存在交,并且交的部分都被覆盖k次 所以一定是两个点,之间路径选择k次,然后端点两开花 f[x]表示x子树 ...
- 【JZOJ5081】【GDSOI2017第三轮模拟】Travel Plan 背包问题+双指针+树的dfs序
题面 100 注意到ban的只会是一个子树,所以我们把原树转化为dfs序列. 然后题目就转化为,询问一段ban的区间,之后的背包问题. 比赛的时候,我想到这里,于是就开始想区间合并,于是搞了线段树合并 ...
- 关于父组件通过v-on接收子组件多个参数的一点研究
写组件的时候遇到一个需求,我需要在子组件向父组件传递信息 this.$emit('myEvent', 信息1, 信息2) 在父组件使用v-on来接收 <my-component @myEvent ...
- input 手机数字键盘
要一点击提起数字键盘,安卓只要设置input的类型是number或tel, ios 需要 pattern="number"可以直接打开搜狗输入法的数字键盘,可以输入.和数字如果只能 ...
- 报错OPTION SQL_SELECT_LIMIT=
org.quartz.JobPersistenceException: Couldn't acquire next trigger: You have an error in your SQL syn ...
- scorllview嵌套gridview和listview的兼容问题
ScrollView嵌套GridView或ListView,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全. 解决办法:自定义一个GridView控件 pac ...
- 【JZOJ4858】【GDOI2017模拟11.4】Walk
题目描述 在比特镇一共有n 个街区,编号依次为1 到n,它们之间通过若干条单向道路连接. 比特镇的交通系统极具特色,除了m 条单向道路之外,每个街区还有一个编码vali,不同街区可能拥有相同的编码.如 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图 代码工程地址: https://github.c ...
- “获取access_token”接口新增IP白名单保护