小峰mybatis(3)mybatis分頁和緩存


一、mybatis分頁-邏輯分頁和物理分頁:                          

邏輯分頁:

mybatis內置的分頁是邏輯分頁;數據庫里有100條數據,要每頁顯示10條,mybatis先把100條數據取出來,放到內存里,從內存里取10條;雖然取出的是10條,但是性能不好,幾千條上萬條沒問題,數據量大性能就有問題了;小項目使用沒問題;正式的項目數據量都很大就不使用了;
 
物理分頁:
開發的時候用的:拼sql,真正實現分頁;
 
現有數據庫記錄:
 
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<Student> studentList=studentMapper.findStudent(rowBound);
        for(Student student:studentList){
            System.out.println(student);
        }
    }
    
    @Test
    public void findStudent2() {
        logger.info("查詢學生物理分頁");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", 0);
        map.put("size", 3);
        List<Student> studentList=studentMapper.findStudent2(map);
        for(Student student:studentList){
            System.out.println(student);
        }
    }
}

2)StudentMapper.java接口:

1   //邏輯分頁 RowBounds里面有分頁信息
2     public List<Student> findStudent(RowBounds rowBound);
3     
4     //物理分頁
5     public List<Student> findStudent2(Map<String, Object> 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緩存:                                          

 

什么時候使用緩存:
並發量很大,並且都是查詢的;這種情況使用緩存很好,服務器的內存要高點;這樣的話性能好,也減輕數據庫的壓力;

 

配置二級緩存:

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">
    
    <!--
        1,size:表示緩存cache中能容納的最大元素數。默認是1024;
        2,flushInterval:定義緩存刷新周期,以毫秒計;
        3,eviction:定義緩存的移除機制;默認是LRU(least recently userd,最近最少使用),還有FIFO(first in first out,先進先出)
        4,readOnly:默認值是false,假如是true的話,緩存只能讀。
     -->
    <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{   ...   }

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM