mybatis 缓存的简单配置

 

mybatis-config.xml配置

 

    <settings>

       <setting name="cacheEnabled" value="true"/>

    </settings>

默认为false,这里需要设置为true

 

同时将PersonMapper.xml中添加

 

<cache></cache>

 

同时将Person javabean 实现Serializable接口,注意比须实现,不然会出现异常.

 

测试如下:

 

package com.mybatis.test;

 

import java.io.InputStream;

import java.util.List;

 

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

 

import com.mybatis.dao.PersonMapper;

import com.mybatis.domain.Person;

 

public class PersonTest {

        

         private static SqlSessionFactory factory ;

        

         static{

                   InputStream in = PersonTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");

                   factory = new SqlSessionFactoryBuilder().build(in,"test1");

         }

        

         public static void main(String args[]){

                   SqlSession session = factory.openSession();

                   PersonMapper personMapper = session.getMapper(PersonMapper.class);

                   Person person = new Person();

                   person.setName("jysemel");

                   List<Person> list =  personMapper.selectPerson("jysemel");

                   System.out.println(list.get(0).getName()+"==================");

                   session.commit();

                   session.close();

                   SqlSession session1 = factory.openSession();

                   PersonMapper personMapper1 = session1.getMapper(PersonMapper.class);

                   person.setName("jysemel");

                   System.out.println(personMapper = personMapper1);

                   List<Person> list1 =  personMapper1.selectPerson("jysemel1");

                   System.out.println(list1.size()+"***********************");

                   session1.commit();

                   session1.close();

         }

}

 

注意:所谓缓存,只有当前session关闭之后,重启开启session,才能看出缓存的作用

 

 

你可能感兴趣的:(mybatis)