springboot 如何操作redis

1.首先应该引入 依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
    @Autowired
    StringRedisTemplate stringRedisTemplate; //操作字符串的
    @Autowired
    RedisTemplate redisTemplate; //k -v 操作对象的
   stringRedisTemplate.opsForValue().append("msg","hello");
      String msg = stringRedisTemplate.opsForValue().get("msg");
      System.out.println(msg);
      stringRedisTemplate.opsForHash();
      stringRedisTemplate.opsForList().leftPush("myList","1");
      stringRedisTemplate.opsForZSet();
      stringRedisTemplate.opsForSet();

自定义序列化器

package cn.edu.aynu.config;

import cn.edu.aynu.bean.Employee;
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 java.rmi.UnknownHostException;

@Configuration
public class MyRedisConfig {
    @Bean
    public RedisTemplate<Object, Employee> empredisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Employee> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;
    }


}
package cn.edu.aynu;

import cn.edu.aynu.bean.Employee;
import cn.edu.aynu.mapper.EmployeeMapper;
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.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootCacheApplicationTests {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate; //操作字符串的
    @Autowired
    RedisTemplate redisTemplate; //k -v 操作对象的
    @Autowired
    RedisTemplate<Object, Employee>  empredisTemplate;
    @Test
    public void contextLoads() {
     /*redis常见的5大数据类型*/
      /*List(列表) String(字符串) zset(有序集合)  set(集合) Hash(散列)*/
      stringRedisTemplate.opsForValue().append("msg","hello");
      String msg = stringRedisTemplate.opsForValue().get("msg");
      System.out.println(msg);
    /*  stringRedisTemplate.opsForHash();
      stringRedisTemplate.opsForList().leftPush("myList","1");
      stringRedisTemplate.opsForZSet();
      stringRedisTemplate.opsForSet();*/
    }
    @Test
    public void  Test02(){
      Employee empById = employeeMapper.getEmpById(1);
      //默认如果保存对象,使用jdk序列化机制,序列化后的数据保存到redis中
      empredisTemplate.opsForValue().set("emp_01",empById);
      /*redisTemplate.opsForValue().set("emp_id",empById);*/
      System.out.println(empById);
    }

}
序列化的结果
{
"id": 1, "lastName": "zs", "email": "zs@qq.com", "gender": null, "department": null, "birth": null }
原文地址:https://www.cnblogs.com/zhulina-917/p/10505271.html