In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
如何将SSM项目加入Redis支持?针对这个问题,今天小编总结了这篇文章,希望能帮助更多想解决这个问题的朋友找到更加简单易行的办法。
需要先搭好SSM开发环境,并安装好Redis,下面是具体的实现步骤:
1、在项目中引入jedis架包:jedis-2.8.2.jar、spring-data-redis-1.6.2.RELEASE.jar和commons-pool-1.6.jar,注意引入的jar版本,过高或过低都有可能引发异常,上面提到这些版本组合亲测可用;
2、编写Redis需要用的2个工具类 RedisUtil.java和SerializeUtil.java
3、新增一个Cache类MybatisRedisCache,实现 org.apache.ibatis.cache.Cache 接口
4、开启mybatis对缓存的支持,在本项目中,是修改 mybatis-config.xml文件
5、在相关的 mapper.xml 添加自定义的缓存类MybatisRedisCache
RedisUtil工具类是用以跟Redis数据通信,SerializeUtil为序列化工具类,也是lang包下的工具,主要用于序列化操作,同时提供对象克隆接口。下面是具体代码:
import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class RedisUtil { private static String ADDR = "127.0.0.1"; private static int PORT = 6379; private static int MAX_ACTIVE = 1024; private static int MAX_IDLE = 200; private static int MAX_WAIT = 100000; private static int TIMEOUT = 10000; private static boolean TEST_ON_BORROW = true; private static JedisPool jedisPool = null; static { try{ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(MAX_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool = new JedisPool(config,ADDR,PORT,TIMEOUT); }catch (Exception e) { e.printStackTrace(); } } public synchronized static Jedis getJedis(){ try{ if(jedisPool != null){ Jedis jedis = jedisPool.getResource(); return jedis; }else{ return null; } }catch (Exception e) { e.printStackTrace(); return null; } } public static void returnResource(final Jedis jedis){ if(jedis != null){ jedisPool.returnResource(jedis); } }}import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class SerializeUtil { public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { e.printStackTrace(); } return null; } public static Object unserialize(byte[] bytes) { if (bytes == null) return null; ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return null; }}import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock;import org.apache.ibatis.cache.Cache;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class MybatisRedisCache implements Cache { private static Logger logger = LoggerFactory.getLogger(MybatisRedisCache.class); /** The ReadWriteLock. */ private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); private String id; public MybatisRedisCache(final String id) { if (id == null) { throw new IllegalArgumentException("Cache instances require an ID"); } logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>MybatisRedisCache:id="+id); this.id = id; } public String getId() { return this.id; } public void putObject(Object key, Object value) { logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>putObject:"+key+"="+value); RedisUtil.getJedis().set(SerializeUtil.serialize(key.toString()), SerializeUtil.serialize(value)); } public Object getObject(Object key) { Object value = SerializeUtil.unserialize(RedisUtil.getJedis().get(SerializeUtil.serialize(key.toString()))); logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>getObject:"+key+"="+value); return value; } public Object removeObject(Object key) { return RedisUtil.getJedis().expire(SerializeUtil.serialize(key.toString()),0); } public void clear() { RedisUtil.getJedis().flushDB(); } public int getSize() { return Integer.valueOf(RedisUtil.getJedis().dbSize().toString()); } public ReadWriteLock getReadWriteLock() { return readWriteLock; }}
mybatis-config.xml文件,在spring-mybatis.xml文件中选择全局加载:
以下是mybatis-config.xml详细代码:
最后在需要缓存的映射文件mapper.xml的namespace加上引用的
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.