需要先搭好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 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:resource/mybatis-config.xml"></property>
<!-- mapper扫描 -->
<property name="mapperLocations" value="classpath:resource/mapper/*.xml"></property>
</bean>
以下是mybatis-config.xml详细代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 全局映射器启用缓存 --> <setting name="cacheEnabled" value="true"/> <!-- 查询时,关闭关联对象即时加载以提高性能 --> <setting name="lazyLoadingEnabled" value="false"/> <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 --> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 允许使用列标签代替列名 --> <setting name="useColumnLabel" value="true"/> <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 --> <setting name="useGeneratedKeys" value="false"/> <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE --> <!-- <setting name="defaultExecutorType" value="BATCH" /> --> <!-- 数据库超过25000秒仍未响应则超时 --> <!-- <setting name="defaultStatementTimeout" value="25000" /> --> <!-- Allows using RowBounds on nested statements --> <setting name="safeRowBoundsEnabled" value="false"/> <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. --> <setting name="localCacheScope" value="SESSION"/> <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values like NULL, VARCHAR or OTHER. --> <setting name="jdbcTypeForNull" value="OTHER"/> <!-- Specifies which Object's methods trigger a lazy load --> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> <!-- 打印sql语句 --> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> </configuration>
最后在需要缓存的映射文件mapper.xml的namespace加上引用的
<?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.krk.sxytj.ytj2001.mapper.YTJ2001Mapper" >
<cache type="com.krk.sxytj.utils.redis.MybatisRedisCache>
然后就可以进行测试了,测试时先进行第一次查询,如果开启打印sql,将会发现控制台打印出刚才执行查询的SQL语句,再进行同样条件的查询,这时注意观察控制台,如果没有打印SQL,直接输出查询结果,说明是从Redis读取到数据返回给你,而不是去数据库中查询得到的结果。此时配置就成功了。
当然,我们可以开启Redis客户端和服务器端,在Redis安装目录下找到redis-cli,双击打开,输入keys *回车,将会看到生成的key,Redis就是根据key去获取所需的value。
在实际操作的过程中要注意RedisUtil工具类,需要根据自己的情况设置port、ADDR 、如果安装的Redis设置有密码,还需加上AUTH,如果没有设置密码,这项不要添加在工具类中,哪怕赋空值也不行,
曾经试过这样: private static String AUTH = " ";
结果运行的时候报了这个异常: jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
意思就是redis服务器没有设置密码,但客户端向其发送了AUTH请求。 此外,要缓存的类需要implements Serializable。 如果怀疑你安装的Redis有问题,可通过下面的方法进行测试:
@Test
public void testRedis(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("连接成功");
//查看服务是否运行
System.out.println("服务正在运行: "+jedis.ping());
//设置 redis 字符串数据
jedis.set("success", "oobom");
// 获取存储的数据并输出
System.out.println("redis 存储的字符串为: "+ jedis.get("success"));
}
更多redis知识请关注redis数据库教程栏目。
以上就是SSM项目加入Redis支持的方法介绍的详细内容,转载自php中文网
发表评论 取消回复