SpringBoot开发(7)——整合redis

  1. 在pom.xml中添加依赖即可
    <!--redis依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
  2. 在application.xml中添加redis配置
    spring:
      redis:
        port: 6379
        host: localhost
  3. 测试是否整合成功,在pom.xml中添加junit依赖
    <!--junit依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
  4. 在test\java下新建包com.cxsbg包,在cxsbg包下新建测试类
    package com.cxsbg;
    
    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.StringRedisTemplate;
    
    @SpringBootTest
    public class WebTest {
        @Autowired
        private StringRedisTemplate redisTemplate;
        @Test
        public void testRedis(){
            redisTemplate.opsForValue().set("name","cxsbg");
        }
    
        @Test
        public void testRedisGet(){
            String name = redisTemplate.opsForValue().get("name");
            System.out.println(name);
        }
    }

发表评论

邮箱地址不会被公开。 必填项已用*标注