SpringBoot——》引入Redis

推荐链接:
    总结——》【Java】
    总结——》【Mysql】
    总结——》【Redis】
    总结——》【Kafka】
    总结——》【Spring】
    总结——》【SpringBoot】
    总结——》【MyBatis、MyBatis-Plus】
    总结——》【Linux】
    总结——》【MongoDB】
    总结——》【Elasticsearch】

SpringBoot——》引入Redis

  • 一、增加依赖
  • 二、增加配置
  • 三、代码示例
  • 四、更改Redis序列化方式

一、增加依赖

<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

二、增加配置

#配置springboot对redis的依赖
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=0

三、代码示例

package com.xiaoxian.demo.controller;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RedisTestController {

    @Autowired
    RedisTemplate redisTemplate;


    @GetMapping("/redis/test")
    public void test() {
        List<User> list = new ArrayList<>();
        User user = new User();
        user.setId(1);
        user.setName("测试");
        user.setBirthday(LocalDateTime.now());
        list.add(user);
        redisTemplate.opsForValue().set("strTest1", list);

        Map<String, Object> map = new HashMap<>(2);
        map.put("id", 1);
        map.put("name", "测试");
        map.put("birthday", LocalDateTime.now());
        redisTemplate.opsForHash().putAll("mapTest1", map);
    }
}
@Data
class User implements Serializable {
    Integer id;
    String name;
    LocalDateTime birthday;
}

四、更改Redis序列化方式

参考链接:SpringBoot——》更改Redis序列化方式

你可能感兴趣的:(SpringBoot,spring,boot,redis,序列化)