Spring中使用Redis
普通使用Redis的方法很简单,前面的文章也有讲解,这篇文章主要就是讲解通过注解的方式实现Spring和Redis的整合。
这里我们创建了3个类:
1.Config 全局配置类,相当于xml配置文件
2.RedisTest 启动类,就一个main方法,同时初始化Spring容器
3.SpringStart 具体操作类,在这个类里面操作具体的Redis
pom.xml
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.4.RELEASE</version> </dependency>
config
package cn.duanjt; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration public class Config { @Bean("poolConfig") public JedisPoolConfig JedisPoolConfig(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(20); config.setMaxIdle(5); config.setMaxWaitMillis(100); config.setTestOnBorrow(true); return config; //return new JedisPool(config, "172.23.88.107", 6379); } @Bean("jedisConnectionFactory") public JedisConnectionFactory JedisConnectionFactory(JedisPoolConfig poolConfig){ JedisConnectionFactory factory=new JedisConnectionFactory(); factory.setHostName("172.23.88.107"); factory.setPort(6379); factory.setPoolConfig(poolConfig); factory.setUsePool(true); return factory; } @Bean("redisTemplate") public RedisTemplate<String, Object> RedisTemplate(JedisConnectionFactory factory){ RedisTemplate<String, Object> template=new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new JdkSerializationRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new JdkSerializationRedisSerializer()); template.setEnableTransactionSupport(true);//开启事务 return template; } @Bean public SpringStart SpringStart(){ return new SpringStart(); } }
RedisTest
package cn.duanjt; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class RedisTest { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); SpringStart test=context.getBean(SpringStart.class); test.start(); } }
SpringStart
package cn.duanjt; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; public class SpringStart { @Autowired RedisTemplate<String, Object> redisTemplate; public void start() { // 设置值 redisTemplate.opsForValue().set("age", 100); // 获取值 System.out.println("String年龄"+redisTemplate.opsForValue().get("age")); Map<String, String> map = new HashMap<>(); map.put("age", "100"); map.put("name", "张三"); redisTemplate.opsForHash().putAll("stu:001", map); System.out.println("Hash姓名:"+redisTemplate.opsForHash().get("stu:001", "age")); //redisTemplate.opsForSet(); //redisTemplate.opsForZSet(); } }
1.注意最后的redisTemplate.opsForSet()和redisTemplate.opsForZSet(),Redis里面的不同类型对应了一个不同方法
2.开始启动的时候报了类没有找到的错误,后来排查是因为jedis和spring-data-redis的版本不兼容,后来将jedis修改为2.9.0之后完美解决。