SpringBoot整合Redis

注意:

  • StringRedisTemplate是默认就有的直接@AutoWried就可以 它里边存的都是String类型的
  • RedisTemplate也是默认有直接@AutoWried就可以 它里边存的是Object类型

1. 引入坐标

<!--        fastJson依赖-->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>2.0.52</version>
</dependency>
<!--        redis依赖-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2. 配置数据库

spring:
  data:
    redis:
      host: localhost
      port: 6379

3. 如果要自定义RedisTemplate就操作这一步,否则跳过

package com.ape.springboot_redis.config;

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;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<Object,Object> jsonRedisTemplate(RedisConnectionFactory factory){
        //        1.创建自定义模板类
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        //        2.配置json类型的序列化工具
        template.setKeySerializer(new StringRedisSerializer());//String
        template.setDefaultSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        template.setConnectionFactory(factory);
        return template;
    }
}

4. 实体类因为使用默认的RedisTemplate要序列化,所以实现Serializable接口

package com.ape.springboot_redis.pojo;

import lombok.*;

import java.io.Serializable;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class Student implements Serializable {
    private Integer sId;
    private String sName;
    private String sHobby;
}

5. 测试StringRedisTemplate

package com.ape.springboot_redis.test;

import com.ape.springboot_redis.pojo.Student;
import org.junit.jupiter.api.Test;
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 java.util.List;
import java.util.Set;

@SpringBootTest
public class Test01 {
    //    专用对象
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    //    字符串类型模板
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //    自定义模板
    @Autowired
    private RedisTemplate jsonRedisTemplate;

    /**
     * 测试使用stringRedisTemplate
     */
    @Test
    public void testStringTemplate() {
        //        使用正则表达找到数据库中的key
        Set<String> keys = stringRedisTemplate.keys("*");
        for (String key : keys) {
            System.out.println(key);
        }
        //        类型
        stringRedisTemplate.opsForValue();//String
        stringRedisTemplate.opsForList();//List
        stringRedisTemplate.opsForHash();//Hash
        stringRedisTemplate.opsForSet();//Set
        stringRedisTemplate.opsForZSet();//ZSet

        //        举例
        stringRedisTemplate.opsForValue().set("name", "zyk");
        System.out.println(stringRedisTemplate.opsForValue().get("name"));

        //        操作List
        stringRedisTemplate.opsForList().leftPush("list1", "曹操");
        stringRedisTemplate.opsForList().leftPush("list1", "曹植");
        stringRedisTemplate.opsForList().leftPushAll("list1", "曹睿", "曹爽", "曹真");
        List<String> list1 = stringRedisTemplate.opsForList().range("list1", 0, -1);
        for (String s : list1) {
            System.out.println(s);
        }
    }
}

6. 测试RedisTemplate

package com.ape.springboot_redis.test;

import com.ape.springboot_redis.pojo.Student;
import org.junit.jupiter.api.Test;
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 java.util.List;
import java.util.Set;

@SpringBootTest
public class Test01 {
    //    专用对象
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    //    字符串类型模板
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //    自定义模板
    @Autowired
    private RedisTemplate jsonRedisTemplate;

    /**
     * 测试默认模板RedisTemplate
     * 1.测试RedisTemplate与stringRedisTemplate存的数据相互独立
     * 2.redisTemplate默认使用key序列化方式和value的序列化方式都使用的是jdk serializer序列化
     * 所以存对象会乱码
     */
    @Test
    public void testRedisTemplate() {
        //        添加/修改的set方法
        redisTemplate.opsForValue().set("name", "张无忌");
        String name = (String) redisTemplate.opsForValue().get("name");
        System.out.println(name);

        //        类型
        redisTemplate.opsForValue();//String
        redisTemplate.opsForList();//List
        redisTemplate.opsForHash();//Hash
        redisTemplate.opsForSet();//Set
        redisTemplate.opsForZSet();//ZSet

        //        存一个Student对象
        Student s = new Student(999, "周永康", "睡觉");
        redisTemplate.opsForValue().set("student", s);
        Student student = (Student) redisTemplate.opsForValue().get("student");
        System.out.println(student);

        //        操作set集合
        redisTemplate.opsForSet().add("set1", s, new Student(1, "刘备", "编制草鞋"), new Student(2, "张三", "法律科普"));
        Set<Object> set1 = redisTemplate.opsForSet().members("set1");
        for (Object stu : set1) {
            Student stu1 = (Student) stu;
            System.out.println(stu1);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值