spring boot集成redis

首先是配置文件的配置

pom.xml



	org.springframework.boot
	spring-boot-starter-data-redis



    org.apache.commons
    commons-pool2
    2.5.0

application.properties

#redis
#Redis数据库索引
spring.redis.database=0
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#redis服务器连接密码
spring.redis.password=
#连接池最大连接数
spring.redis.lettuce.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制) 
spring.redis.lettuce.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=10
# 连接池中的最小空闲连接 
spring.redis.lettuce.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=1000

然后是配置类
RedisConfigure.java

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
@EnableCaching
public class RedisConfigure {
	@Bean
    RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setValueSerializer(serializer);        
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);
        template.afterPropertiesSet();        
        return template;
    }

}

简单工具类 RedisUtils

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

@Component
public class RedisUtils {
	@Autowired
	RedisTemplate redisTemplate;
	/**
	 * add
	 */
	public void add(String key,Object obj ,Long times) {
		redisTemplate.opsForValue().set(key, obj, times);
	}
	/**
	 * add
	 */
	public void add(String key,Object obj ) {
		redisTemplate.opsForValue().set(key, obj);
	}
	/**
	 * delete
	 */
	public void delete(String key) {
		redisTemplate.opsForValue().getOperations().delete(key);
	}
	/**
	 * get
	 */
	public Object get(String key) {
		return redisTemplate.opsForValue().get(key);
	}
}

测试类

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.example.demo.redis.RedisUtils;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class LoveToolsApplicationTests {
	private static Logger logger=LoggerFactory.getLogger(LoveToolsApplicationTests.class);
	@Autowired
	RedisUtils util;
	@Test
	public void contextLoads() {
		util.add("te", "test");
		System.out.println(util.get("te"));
		util.add("te", "test");
		System.out.println(util.get("te"));
		util.delete("te");
		System.out.println(util.get("te"));
	}

}

测试结果
test
test
null

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