一级缓存
一级缓存指的就是session,是默认使用的
User user1 = userMapper.getUserById(1);
System.out.println(user1);
User user2 = userMapper.getUserById(1);
System.out.println(user2);
输出如下
保存,删除,更新,一级缓存的数据会自动清空,下次查询,会执行sql语句
User user1 = userMapper.getUserById(1);
System.out.println(user1);
userMapper.save(new User("zhangsan","1",null,"北京"));
User user2 = userMapper.getUserById(1);
System.out.println(user2);
输出如下
二级缓存
全局文件SqlMapConfig.xml开启二级缓存
<settings>
<!-- 允许开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
映射文件UserMapper.xml配置缓存,type值:默认使用的是mybaits自带的缓存技术,perpetualCache
<cache></cache>
User需要实现序列化
public class User implements Serializable
保存,删除,更新,一级缓存的数据会自动清空
@Test
public void func1() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session1 = sessionFactory.openSession();
SqlSession session2 = sessionFactory.openSession();
SqlSession session3 = sessionFactory.openSession();
UserMapper mapper1 = session1.getMapper(UserMapper.class);
UserMapper mapper2 = session2.getMapper(UserMapper.class);
UserMapper mapper3 = session3.getMapper(UserMapper.class);
User user1 = mapper1.getUserById(1);
System.out.println(user1);
//session关闭后,才会提交到二级缓存
session1.close();
//保存用户
mapper3.save(new User("zhangsan", "1", null, "北京"));
session3.commit();
session3.close();
User user2 = mapper2.getUserById(1);
System.out.println(user2);
session2.close();
}
ehcache
导入 ehcache-core
添加 ehcache.xml
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
</ehcache>
UserMapper.xml 文件内缓存type更改为ehcache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
某个方法禁用缓存 useCache
<select id="getUserById" parameterType="int" resultType="user" useCache="false">
默认情况下,插入、更新、删除会清空二级缓存,useCache设置为false不清空二级缓存
<insert id="save" parameterType="user" flushCache="false">
mybaties整合spring
导包
导入mybatis包
导入mysql数据库驱动包
添加applicationContext.xml
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mydatabase?characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="root123"></property>
<!-- 最大空闲数 -->
<property name="maxIdle" value="5"></property>
</bean>
<!-- 配置会话工厂 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
</bean>
<!-- 配置dao方式一:-->
<bean id="userDao" class="com.company.model.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
添加 SqlMapConfig.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>
<!-- 配置别名 -->
<typeAliases>
<package name="com.company.model"/>
</typeAliases>
<!-- 配置加载全局映射文件 -->
<mappers>
<mapper resource="com/company/model/sqlmap/User.xml"></mapper>
</mappers>
</configuration>
方式一
添加User.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="user">
<select id="getUserById" parameterType="int" resultType="user">
select * from user where id = #{id}
</select>
</mapper>
UserDaoImpl
package com.company.model.dao;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
public User getUserById(int id) {
return this.getSqlSession().selectOne("user.getUserById",id);
}
}
测试
@Test
public void func() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) context.getBean("userDao");
User user = userDao.getUserById(10);
System.out.println(user);
}
方式二
添加UserMapper
package com.company.model.mapper;
import com.company.model.dao.User;
public interface UserMapper {
public User getUserById(int id);
}
添加UserMapper.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.company.model.mapper.UserMapper">
<select id="getUserById" parameterType="int" resultType="user">
select * from user where id = #{id}
</select>
</mapper>
配置SqlMapConfig.xml
<!-- 配置加载全局映射文件 -->
<mappers>
<package name="com.company.model.mapper"/>
</mappers>
配置applicationContext.xml
<!-- 配置dao方式二: 由spring创建一个userMapper对象,使用工厂来创建
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sessionFactory"></property>
<property name="mapperInterface" value="com.company.model.mapper.UserMapper"></property>
</bean>
-->
<!-- 配置dao方式三: 批量创建mapper的bean对象
内部会扫描指定包下的mapper,创建代理对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.company.model.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
</bean>
测试
@Test
public void func1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) context.getBean("userMapper");
User user = userMapper.getUserById(10);
System.out.println(user);
}