首頁 >資料庫 >Redis >SSM計畫加入Redis支持的方法介紹

SSM計畫加入Redis支持的方法介紹

尚
轉載
2019-12-21 17:45:202119瀏覽

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 -->
    <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&#39;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中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除