Rudimentary Study Redis -- spring boot integration Redis + attached project code

1. Environment construction

We will continue to use the project used in Redis(4) for beginners

Project code
 Link: https://pan.baidu.com/s/1yiwBs1RZlD6D2jqc-qxUzQ 
Extraction code: kzz3

1.1 replace pom dependency

    <dependencies>
        <!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>
    </dependencies>

1.2 create application.yml file

spring:
  redis:
    host: 127.0.0.1 #redis service address
    database: 0     #Confirm to use the library
    port: 6379      #redis port number

1.3 create startup class

package com.manlu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Auther Manluh
 * Created by 2020-04-27 11:38
 */
@SpringBootApplication
public class TestRedisApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestRedisApplication.class,args);
    }
}

1.4 integrate Junit

package com.manlu;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * @Auther Manluh
 * Created by 2020-04-27 11:40
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestRedisApplication.class)
public class TestRedisTemplate {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void demo01(){
        System.out.println(stringRedisTemplate);
    }
}

2. Common methods of stringredistemplate

There are 5 types of Redis. StringRedisTemplate provides 5 types of operations. |Method description| | ------------ | ------------ | |opsForValue() | operation string| |delete(key) | delete data according to key| |opsForHash() | operation hash| |opsForList() | operation list| |opsForSet() | operation set| |opsForZSet() | operation order set|

2.1 opsForValue() operation

Method describe
ops.set(key,value) Insert data into redis. Permanent storage
ops.set(key,value,time,timeUtil) Insert data into redis. Parameter 3 is a long time and parameter 4 is a unit of time.
ops.get(key) Get the value value of the key specified in redis

2.2 test

2.2.1 add string

    /**
     * Add string data
     */
    @Test
    public void demo01(){
        stringRedisTemplate.opsForValue().set("demo01","I am demo01");
    }

Look at the visualizer after execution

2.2.2 add string (valid time)

    /**
     * Add string data (valid time)
     */
    @Test
    public void demo02(){
        stringRedisTemplate.opsForValue().set("demo02","I am demo02",1, TimeUnit.MINUTES);
    }

Look at the visualizer after execution

2.2.3 get string

    /**
     * Get string
     */
    @Test
    public void demo03(){
        String str = stringRedisTemplate.opsForValue().get("demo01");
        System.out.println(str);
    }

See execution results

2.2.4 delete the specified key

    /**
     * Delete the specified key
     */
    @Test
    public void demo04(){
        Boolean b = stringRedisTemplate.delete("demo01");
        System.out.println(b);
    }

Look at the visualizer after execution

Tags: Database Redis Spring Junit Jedis

Posted on Mon, 27 Apr 2020 07:45:39 -0700 by erupt