SpringMVC集成Redis

(1)添加pom依赖

 1     <dependency>
 2       <groupId>org.springframework.data</groupId>
 3       <artifactId>spring-data-redis</artifactId>
 4       <version>1.8.18.RELEASE</version>
 5     </dependency>
 6     <dependency>
 7       <groupId>org.apache.commons</groupId>
 8       <artifactId>commons-pool2</artifactId>
 9       <version>2.6.0</version>
10     </dependency>
11     <dependency>
12         <groupId>redis.clients</groupId>
13         <artifactId>jedis</artifactId>
14         <version>2.9.0</version>
15     </dependency>
16     <dependency>
17       <groupId>com.fasterxml.jackson.core</groupId>
18       <artifactId>jackson-databind</artifactId>
19       <version>2.9.8</version>
20     </dependency>

(2)注册相关bean

 1 package cn.coreqi.config;
 2 
 3 import cn.coreqi.entities.User;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.data.redis.connection.RedisConnectionFactory;
 7 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
 8 import org.springframework.data.redis.core.RedisTemplate;
 9 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
10 import org.springframework.data.redis.serializer.StringRedisSerializer;
11 
12 @Configuration
13 public class RedisConfig {
14 //    @Bean
15 //    public RedisConnectionFactory redis(){
16 //        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
17 //        redisStandaloneConfiguration.setHostName("192.168.205.128");
18 //        redisStandaloneConfiguration.setPort(6379);
19 //        redisStandaloneConfiguration.setDatabase(0);
20 //        //redisStandaloneConfiguration.setPassword(RedisPassword.of("123456"));
21 //
22 //        JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
23 //        jedisClientConfiguration.connectTimeout(Duration.ofMillis(6000));//  connection timeout
24 //
25 //        JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build());
26 //        return factory;
27 //    }
28     @Bean
29     public RedisConnectionFactory redis(){
30         JedisConnectionFactory factory = new JedisConnectionFactory();
31         factory.setHostName("192.168.205.128");
32         factory.setPort(6379);
33         return factory;
34     }
35 
36     @Bean
37     public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory redis){
38         RedisTemplate<String, User> redisTemplate = new RedisTemplate<>();
39         redisTemplate.setConnectionFactory(redis);
40         redisTemplate.setKeySerializer(new StringRedisSerializer());
41         redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<User>(User.class));
42         return redisTemplate;
43     }
44 
45 //    @Bean
46 //    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redis){
47 //        return new StringRedisTemplate(redis);
48 //    }
49 }

 

posted @ 2019-01-16 17:50  SpringCore  阅读(1372)  评论(0编辑  收藏  举报