Spring整合redis带测试用例Demo完整代码

项目结构

Spring整合redis带测试用例Demo完整代码_第1张图片

jar包

        
        
            redis.clients
            jedis
            2.7.3
        
        
            org.springframework.data
            spring-data-redis
            1.6.1.RELEASE
        

 

xml文件



    
    
    
    
    
    
        
        
        
    
    
    
        
        
        
        
        
    
    
        
        
            
        
        
            
        
    


配置文件

redis.host=127.0.0.1
redis.port=6379
redis.password=
redis.maxIdle=100
redis.maxActive=300
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=100000

实体类

package com.xdja.cssp.demo.service.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author: ggp
 * @Date: 2018/11/9 15:41
 * @Description: redis工具类
 */
@Component
public final class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result = null;
        ValueOperations operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }
}



测试用例

package com.xdja.cssp.demo.test;


import com.xdja.cssp.demo.service.redis.RedisUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author: ggp
 * @Date: 2018/11/9 17:16
 * @Description:
 */
@RunWith(SpringJUnit4ClassRunner.class)//使用junit4进行测试
@ContextConfiguration("classpath:/META-INF/spring/applicationContext-redis.xml")
public class RedisGetTest {
    @Autowired
    RedisUtil redisUtil ;

    @Test
    public void put(){

        System.out.println(redisUtil.get("ssh"));


    }
}
package com.xdja.cssp.demo.test;


import com.xdja.cssp.demo.service.redis.RedisUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author: ggp
 * @Date: 2018/11/9 15:49
 * @Description: redis测试用例
 */

@RunWith(SpringJUnit4ClassRunner.class)//使用junit4进行测试
@ContextConfiguration("classpath:/META-INF/spring/applicationContext-redis.xml")
public class RedisPutTest {

    @Autowired
    RedisUtil redisUtil ;

    @Test
    public void put(){

        redisUtil.set("ssh",11);


    }


}

记得开启redis服务

你可能感兴趣的:(spring,redis)