使用mybatis实现分页
1.逻辑分页 ( 数据表加载到内存中分页 )
<!-- 使用逻辑分页
查询所有数据到内存
mybatis 使用RowBounds进行分页
-->
<select id="findEmps1" resultMap="empRes" >select * from employee
</select>
Junit代码
@Test
public void testFindEmps1(){logger.info("逻辑分页:查找雇员!");
RowBounds rowBounds = new RowBounds(0, 4);
List<Employee> emps= empMapper.findEmps1(rowBounds);
for (Employee employee : emps) {
System.out.println(employee);
}
sqlSession.commit();
}
2.物理分页 (传递参数使用mysql的limit实现分页)
<!-- 物理分页 -->
<select id="findEmps2" resultMap="empRes" parameterType="Map">
select * from employee
<if test="start!=null and size!=null">
limit #{start},#{size}
</if>
</select>
//物理分页测试
@Test
public void testFindEmps2(){logger.info("物理分页:查找雇员!");
Map<String ,Object> page = new HashMap<>();
page.put("start", 3);
page.put("size", 3);
List<Employee> emps= empMapper.findEmps2(page);
for (Employee employee : emps) {
System.out.println(employee);
}
}
mybatis 缓存配置
Mybatis默认情况下启用一级缓存 ,
用户可以自己配置二级缓存(全局)
默认情况下 select查询使用缓存 ,insert,update,delete 不
使用缓存
可在mepper中配置缓存
<!--
size=1024 :定义缓存大小 默认1024
flushInterval="60000" : 缓存刷新周期 ms
eviction="LRU" :缓存移除机制 LRU(最近最少使用),还有FIFO
readOnly=fase: 缓存可读写
-->
<cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false" />
也可在select update 等标签中使用 useCache 指定
转载请注明:左手代码右手诗 » Mybatis学习之路—分页和缓存
哈哈