Spring boot uses redis through redisTemplate without manual serialization

Keywords: Redis Spring Database Junit

                     

For some operations of redisTemplate, please refer to the following two articles
http://blog.csdn.net/whatlookingfor/article/details/51863286
http://www.jianshu.com/p/7bf5dc61ca06

Import redis dependency

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
  • 1
  • 2
  • 3
  • 4

Jackson depends on

<dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>    <version>2.3.2</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

Configure the application.yml configuration file

#REDIS (RedisProperties)spring: redis: database: 0 the redisdatabase index (the default is 0) of redis database index (redisproperties (redisproperties) spring: redisserver (redisproperties) is the redisserver address of the redisserver. port: 6379 the redisserver connection port. The redisserver connection port is the redisserver connection port of the redisserver connection port.Password: redis server connection password (empty by default) The maximum number of connections in the pool (a negative value indicates no limit) the maximum number of connections in the pool (a negative value indicates no limit), the maximum number of free connections in the pool maxwait: - 1 ᦇ connection pool maximum blocking waiting time (use negative value to indicate no limit) Min idle: 0 the minimum free connection in the connection pool
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

The redisTemplate does not need to be serialized manually, and replaces the default serialization tool with jackson

import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;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;/** * Created by zhengwei on 2017/10/17. */@Configurationpublic class RedisConfig {    /**     * redisTemplate The jdkSerializeable used for serialization stores binary bytecode, so the custom serialization class is * @ param redisConnectionFactory * @ return */    @Bean    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();        redisTemplate.setConnectionFactory(redisConnectionFactory);        // Replace the default serialization with Jackson 2jsonredisserializer Jackson 2jsonredisserializer = new Jackson 2jsonredisserializer (object.class);        ObjectMapper objectMapper = new ObjectMapper();        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);        // Set value's serialization rule and key's serialization rule redisTemplate.setKeySerializer(new StringRedisSerializer());        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);        redisTemplate.afterPropertiesSet();        return redisTemplate;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

Start using

import com.example.entity.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class RedisApplicationTests {    @Autowired    private RedisTemplate<Object, Object> template;    @Test    public void contextLoads() {        User user = new User(1,"Geoduck");        template.opsForValue().set(user.getId()+"",user);        //opsForValue() can only operate on strings. Now you can operate on the object User result = (User) template.opsForValue().get(user.getId()+"");        System.out.println(result.toString());    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

Output results:

User{id=1, name='Geoduck'}
  • 1

Many of the remaining operations go to Baidu to explore

           

Posted by departedmind on Mon, 25 Nov 2019 12:58:04 -0800