zoukankan      html  css  js  c++  java
  • Spring Boot集成Redis

    spring boot集成redis步骤:

    1、添加pom依赖

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

    2、在application.yml或aplicaction.properties中添加连接信息

    application.yml:

    spring:
      redis:
        host: 127.0.0.1
        port: 6379
        database: 0

    如果是applicaton.properties:

    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    

    3、配置完以上两个步骤后,Spring Boot会自动配置RedisTemplate,我们只需要在使用的时候注入即可

     1 package top.bigking.backstage.controller;
     2 
     3 import org.junit.jupiter.api.Test;
     4 import org.springframework.boot.test.context.SpringBootTest;
     5 import org.springframework.data.redis.core.RedisTemplate;
     6 import org.springframework.data.redis.serializer.StringRedisSerializer;
     7 
     8 import javax.annotation.Resource;
     9 
    10 /**
    11  * @Author ABKing
    12  * @since 2020/4/2 下午4:26
    13  **/
    14 @SpringBootTest
    15 public class VerifyCodeControllerTest {
    16     @Resource
    17     private RedisTemplate<String, String> redisTemplate;
    18     @Test
    19     public void redisConnectTest(){
    20         //防止key被序列化后,程序员看不懂。实际仍然可以使用
    21         //redisTemplate.setKeySerializer(new StringRedisSerializer());
    22         redisTemplate.opsForValue().set("a", "123");
    23         String a = redisTemplate.opsForValue().get("a");
    24         System.out.println(a);
    25     }
    26 }

    如果你在redis-cli中发现key变成这样:

    不用担心,这是因为数据被序列化了。当然,这很影响体验,所以我们可以通过redisTemplate.setKeySerializer(new StringRedisSerializer());来防止key被序列化。

    金麟岂是池中物,一遇风云便化龙!
  • 相关阅读:
    Java面向对象设计——购物车·
    查找

    栈和队列
    指针
    数组
    第四次博客——函数
    第三次博客作业
    第二次博客作业
    Java购物车大作业01
  • 原文地址:https://www.cnblogs.com/ABKing/p/12621260.html
Copyright © 2011-2022 走看看