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的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...
随机推荐
- ajax成功请求到后台,但是前端报404错误
记录下今天遇见的一个问题,一个删除的ajax请求,传递一个主键到后台,后台成功接受并执行删除,但是前端报POST404错误. 查找原因是由于Controller忘记写返回状态码 @Responsebo ...
- 洛谷P1541 乌龟棋 [2010NOIP提高组]
P1541 乌龟棋 题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家 ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- windows10 中微信(UWP)版本不显示通知消息
前言: 前段时间笔者更换了升级了WINDOWS10系统,从应用商店安装微信后,使用期间不会推送消息通知,右下角的通知栏也无法添加微信图标.搜索百度和Google后,发现很多人都是这样,这是微信(UWP ...
- Eclipse Jobs 和后台进程
Eclipse后台进程 1.主线程(Main thread) 一个Eclipse客户端只能跑一个进程,但却可以创建很多线程. 在Eclipse框架中,会用一个单线程去运行所有的代码指令,这个线程执行客 ...
- JavaScript--封装好的运动函数+旋转木马例子
封装好的运动函数: 1.能控制目标元素的多种属性 2.能自动获取元素的样式表: 3.获取样式函数兼容 4.能对于元素的多属性进行动画(缓动动画)修改 5.能区分透明度等没单位的属性和px属性的变化 a ...
- php表单传值--GET和POST
一. 传值 1. 传值/接收方法: 1) GET(5种方式!) a) 表单Form: method = ‘get’ GET接收数据方式: b) ...
- 通过IP地址訪问Jbossserver上的应用
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/liu765023051/article/details/28882533 环境介绍 Web项目中.在 ...
- python 正则表达式简介
- Leetcode819.Most Common Word最常见的单词
给定一个段落 (paragraph) 和一个禁用单词列表 (banned).返回出现次数最多,同时不在禁用列表中的单词.题目保证至少有一个词不在禁用列表中,而且答案唯一. 禁用列表中的单词用小写字母表 ...