Mybatis整合ehcache

Mybatis整合ehcache

加入jar包
在这里插入图片描述

二级缓存必须开启
在这里插入图片描述

创建ehcahce.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
         <!--<diskStore path="java.io.tmpdir" />-->
    <defaultCache
            maxElementsInMemory="10000"
            maxElementsOnDisk="10000000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU" />

           <!-- 

    name:Cache的唯一标识

    maxElementsInMemory:内存中最大缓存对象数

    maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大

    eternal:Element是否永久有效,一但设置了,timeout将不起作用

    overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中

    timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大

    timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间

        和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大 

    diskPersistent:是否缓存虚拟机重启期数据 

    diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒

    diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区

    memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用) 

    -->

        </ehcache>

我使用的是IDEA2017存在报错
在这里插入图片描述
处理报错
在这里插入图片描述
在这里插入图片描述
添加如图上图所示的内容http://ehcache.org/ehcache.xsd
修改ehcahce.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.xieli.liu.dao.UserInfoMapper">
	<!--开启二级缓存-->
	<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
	
	<resultMap id="userInfoResultMap" type="UserInfo">
		<id column="user_id" property="userId" />
		<result column="group_id" property="groupId" />
		<result column="user_name" property="userName" />
		<result column="nick_name" property="nickName" />
		<result column="user_code" property="userCode" />
		<result column="user_pwd" property="userPwd" />
		<result column="user_type" property="userType" />
		<result column="user_state" property="userState" />
		<result column="is_delete" property="isDelete" />
		<result column="create_by" property="createBy" />
		<result column="create_time" property="createTime" />
		<result column="udate_by" property="udateBy" />
		<result column="update_time" property="updateTime" />
	</resultMap>
	<!-- 查询数据库所有用户信息,用来测试数据库是否连接上 -->
	<select id="selectAll" resultMap="userInfoResultMap">
		select * from User_info
	</select>
	<!-- 根据前台传来的用户ID修改用户状态 -->
	<update id="updateStatics" parameterType="UserInfo">
		UPDATE user_info SET
		user_state=0
		WHERE user_id = #{userId}
	</update>
</mapper>

编写测试类
1.查询过程中没有操作数据库

package com.xieli.liu;

import com.xieli.liu.dao.UserInfoMapper;
import com.xieli.liu.pojo.Role;
import com.xieli.liu.pojo.UserInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2019-11-20.
 */
public class test {
    @Test
    public  void test() {
        SqlSessionFactory sessionFactory;
        //使用MyBatis提供的Resources类加载mybatis的配置文件
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("config/sqlMapConfig.xml");
            //构建sqlSession的工厂
            sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = sessionFactory.openSession();
            SqlSession sqlSession2 = sessionFactory.openSession();
            SqlSession sqlSession3 = sessionFactory.openSession();
            //发起第一次查询
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
            List<UserInfo> userInfos = mapper.selectAll();
            for (UserInfo userInfo: userInfos) {
                System.out.println(userInfo.getUserName());
            }
            //写入二级缓存
            sqlSession.close();

            //对数据库进行操作
         /*   UserInfoMapper mapper3 = sqlSession3.getMapper(UserInfoMapper.class);
            UserInfo u=new UserInfo();
            u.setUserState("1");
            mapper3.updateStatics(u);
            sqlSession3.commit();
            sqlSession3.close();*/

            //发起第二次查询
            UserInfoMapper mapper2 = sqlSession2.getMapper(UserInfoMapper.class);
            List<UserInfo> userInfos2  =mapper2.selectAll();
            for (UserInfo userInfo: userInfos2) {
                System.out.println(userInfo.getUserName());
            }
            sqlSession2.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

在这里插入图片描述
2.查询过程中操作数据库

package com.xieli.liu;

import com.xieli.liu.dao.UserInfoMapper;
import com.xieli.liu.pojo.Role;
import com.xieli.liu.pojo.UserInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2019-11-20.
 */
public class test {
    @Test
    public  void test() {
        SqlSessionFactory sessionFactory;
        //使用MyBatis提供的Resources类加载mybatis的配置文件
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("config/sqlMapConfig.xml");
            //构建sqlSession的工厂
            sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = sessionFactory.openSession();
            SqlSession sqlSession2 = sessionFactory.openSession();
            SqlSession sqlSession3 = sessionFactory.openSession();
            //发起第一次查询
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
            List<UserInfo> userInfos = mapper.selectAll();
            for (UserInfo userInfo: userInfos) {
                System.out.println(userInfo.getUserName());
            }
            //写入二级缓存
            sqlSession.close();

            //对数据库进行操作
            UserInfoMapper mapper3 = sqlSession3.getMapper(UserInfoMapper.class);
            UserInfo u=new UserInfo();
            u.setUserState("1");
            mapper3.updateStatics(u);
            sqlSession3.commit();
            sqlSession3.close();

            //发起第二次查询
            UserInfoMapper mapper2 = sqlSession2.getMapper(UserInfoMapper.class);
            List<UserInfo> userInfos2  =mapper2.selectAll();
            for (UserInfo userInfo: userInfos2) {
                System.out.println(userInfo.getUserName());
            }
            sqlSession2.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

下图为第二次查询的日志
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桀骜浮沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值