小峰mybatis(3)mybatis分页和缓存

一、mybatis分页-逻辑分页和物理分页:                          

逻辑分页:

mybatis内置的分页是逻辑分页;数据库里有100条数据,要每页显示10条,mybatis先把100条数据取出来,放到内存里,从内存里取10条;虽然取出的是10条,但是性能不好,几千条上万条没问题,数据量大性能就有问题了;小项目使用没问题;正式的项目数据量都很大就不使用了;
 
物理分页:
开发的时候用的:拼sql,真正实现分页;
 
现有数据库记录:
小峰mybatis(3)mybatis分页和缓存_第1张图片
 
1、逻辑分页
1)测试代码StudentTest2.java:
package com.cy.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.cy.mapper.StudentMapper;
import com.cy.model.Student;
import com.cy.util.SqlSessionFactoryUtil;

public class StudentTest2 {
    private static Logger logger = Logger.getLogger(StudentTest2.class);
    
    private SqlSession sqlSession=null;
    private StudentMapper studentMapper=null;
    
    @Before
    public void setUp() throws Exception {
        sqlSession=SqlSessionFactoryUtil.openSession();
        studentMapper=sqlSession.getMapper(StudentMapper.class);
    }
    
    @After
    public void tearDown() throws Exception {
        sqlSession.close();
    }
    
   
    /**
     * 逻辑分页,实现过程:先把所有数据都查出来,再从内存中从0开始,取3条数据;
     */
    @Test
    public void findStudent() {
        logger.info("查询学生逻辑分页");
        int offset = 0;            //start;开始
        int limit = 3;            //limit: 每页大小;
        RowBounds rowBound = new RowBounds(offset, limit);    //RowBounds里面有分页信息
        List studentList=studentMapper.findStudent(rowBound);
        for(Student student:studentList){
            System.out.println(student);
        }
    }
    
    @Test
    public void findStudent2() {
        logger.info("查询学生物理分页");
        Map map = new HashMap();
        map.put("start", 0);
        map.put("size", 3);
        List studentList=studentMapper.findStudent2(map);
        for(Student student:studentList){
            System.out.println(student);
        }
    }
}

2)StudentMapper.java接口:

1   //逻辑分页 RowBounds里面有分页信息
2     public List findStudent(RowBounds rowBound);
3     
4     //物理分页
5     public List findStudent2(Map map);

3)StudentMapper.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.cy.mapper.StudentMapper">

    <resultMap type="com.cy.model.Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="remark" column="remark"/>
    resultMap>
    
    
    <select id="findStudent" resultMap="StudentResult">
        select * from t_student
    select>
    
    
    <select id="findStudent2" parameterType="Map"  resultMap="StudentResult">
        select * from t_student
        <if test="start!=null and size!=null">
            limit #{start}, #{size}
        if>
    select>
mapper>

console:

 

 

 

 

二、mybatis缓存:                                          

 小峰mybatis(3)mybatis分页和缓存_第2张图片

什么时候使用缓存:
并发量很大,并且都是查询的;这种情况使用缓存很好,服务器的内存要高点;这样的话性能好,也减轻数据库的压力;

 

配置二级缓存:

1)StudentMapper.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.cy.mapper.StudentMapper">
    
    
    <cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"/>
    
    <resultMap type="com.cy.model.Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="remark" column="remark"/>
    resultMap>
    
    <select id="findStudents" resultMap="StudentResult" flushCache="false" useCache="true">
        select * from t_student
    select>
    
    <insert id="insertStudent" parameterType="Student" flushCache="true">
        insert into t_student values(null,#{name},#{age},#{pic},#{remark});
    insert>
    
mapper>
select:
useCache: 默认true;默认使用缓存;
flushCashe:清空缓存;false:不清空缓存;
 
insert:
flushCashe:默认true;清掉缓存;update,delete默认flushCache也是true;
 
参考之前的mybatis二级缓存的文章;model好像要实现Serializable:
public class Student implements Serializable{   ...   }

 

 

 

 

转载于:https://www.cnblogs.com/tenWood/p/6676331.html

你可能感兴趣的:(小峰mybatis(3)mybatis分页和缓存)