SpringBoot(十一):SpringBoot整合Redis

详解springboot整合redis:https://blog.csdn.net/qq_36781505/article/details/86612988

一、环境准备

  • Redis-x64-3.2.100.zip
  • SpringBoot 1.5.10.RELEASE

Redis-x64-3.2.100.zip 下载地址:https://github.com/MicrosoftArchive/redis/releases

pom依赖:

<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>


二:application.yml:

spring:
    redis:
      host: 120.79.82.191 
      port: 6379
      password: Lysb_TestRedis!2019
      jedis:
        pool:
          max-active: 8
          max-wait: -1
          max-idle: 500
          min-idle: 0
      lettuce:
        shutdown-timeout: 0

    datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.1.20:3306/test?useSSL=false
      username: root
      password: root123        
        

logging.level.com.demo.mapper: debug

 bean:

package cn.demo.bean;

import java.io.Serializable;

public class Days implements Serializable {
    
    private String openId;
    private String daysId;
    //每天的标题
    private String title;
    //代办事项的数量
    private int itemNumber;
    //日程
    private String date;
    public String getOpenId() {
        return openId;
    }
    public void setOpenId(String openId) {
        this.openId = openId;
    }
    public String getDaysId() {
        return daysId;
    }
    public void setDaysId(String daysId) {
        this.daysId = daysId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getItemNumber() {
        return itemNumber;
    }
    public void setItemNumber(int itemNumber) {
        this.itemNumber = itemNumber;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    @Override
    public String toString() {
        return "Days [openId=" + openId + ", daysId=" + daysId + ", title=" + title + ", itemNumber=" + itemNumber
                + ", date=" + date + "]";
    }
    
}

redisconfig:

package cn.demo.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;

import cn.demo.bean.Days;

@Configuration
public class RedisConfig{
    @Bean
    public RedisTemplate<String, Days>redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Days>template=new RedisTemplate<>();
        //关联
        template.setConnectionFactory(factory);
        //设置key的序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化器
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Days.class));
        return template;
    }
}

redisTest:

package cn.demo.test;

import org.junit.Before;
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;

import cn.demo.bean.Days;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {

    @Autowired
    private RedisTemplate<String,Days> redisTemplate;
    
    //注入一个对象缓存在redis
    @Test
    public void testSet(){
        Days d=new Days();
        d.setDate("123");
        d.setDaysId("456");
        d.setItemNumber(123);
        d.setOpenId("dawda");
        d.setTitle("title");
        this.redisTemplate.opsForValue().set("days",d);
        System.out.println("redisTemplate=="+(redisTemplate.opsForValue().get("days")));
    }
}
原文地址:https://www.cnblogs.com/lgg20/p/11724501.html