如果你的SpringBoot項目想引入Redis的話,不妨點進來看看!

目錄

1.Redis是什麼?

2.Redis的使用

步驟1)

步驟2)

3.補充redis的用法

3.1 存取String類型的值:


1.Redis是什麼?

Redis (Remote Dictionary Server) 是一個使用ANST C編寫的開源,支持開源,基於內存,可選持久性的鍵值對存儲數據庫,也是於開發或者運維都是必須掌握的非關係型數據庫。

Redis 作爲高性能Key-Value 服務器,擁有多種數據結構,並提供豐富的功能以及對高可用分佈式的支持。

Redis 具有以下特點:1.速度快2.支持多種數據結構3.高可用,分佈式等。

Redis是什麼?爲什麼要用Redis?

Redis的8大使用場景

2.Redis的使用

步驟1)

1.我現在本地是安裝了redis 安裝步驟我這邊就不展示了。在我的計算機-服務裏面可以看到redis的服務已經啓動好了。

步驟2)

2.在項目中引入redis

     <!--SpringBoot中使用默認的redis的客戶端-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

 

3.在配置文件中配置redis的連接信息。

我這邊用的是MySQL8.0.12 (非必須) 我下面這個例子沒用到。

server.port=8006
# MySQL Database
spring.datasource.url=jdbc:mysql://localhost:3306/shiro_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# redis的使用
spring.session.store-type=redis
spring.redis.host=127.0.0.1
spring.redis.port=6379

 

4. 編寫一個簡單的實體類 BosUserModel

 如果要複製下面這個實體類的話,需要在pom.xml中引入這個jar包。

      <!-- spring boot start -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
package com.test.model;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @Author tanghh
 * @Date 2020/4/6 10:35
 */
@Entity
@Table(name = "bos_user", schema = "test3", catalog = "")
public class BosUserModel implements Serializable {
    private Integer id;
    private String userName;
    private String password;
    private String isDel="0";

    public BosUserModel() {

    }

    public BosUserModel(Integer id, String userName, String password, String isDel) {
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.isDel = isDel;
    }


    @Override
    public String toString() {
        return "BosUserModel{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", isDel='" + isDel + '\'' +
                '}';
    }

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    @Basic
    @Column(name = "user_name")
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    @Basic
    @Column(name = "password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    @Basic
    @Column(name = "is_del")
    public String getIsDel() {
        return isDel;
    }

    public void setIsDel(String isDel) {
        this.isDel = isDel;
    }
}

 

5. 編寫一個TestRedisController

 

package com.test.controller;

import com.test.service.BosUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author tanghh
 * @Date 2020/4/7 14:00
 */
@RestController
public class TestRedisController {

    @Autowired
    private BosUserService bosUserService;


    @ApiOperation(value = "1.1 測試Redis的使用")
    @GetMapping(value = "/testRedis")
    public void testRedis() {
        //redis的使用總結
        bosUserService.testRedis();
    }

}

 

6. BosUserService

package com.test.service.impl;

import com.test.model.BosUserModel;
import com.test.service.BosUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author tanghh
 * @Date 2020/4/7 14:05
 */
@Service
public class BosUserServiceImpl implements BosUserService {
    @Autowired
    private static RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        BosUserServiceImpl.redisTemplate = redisTemplate;
    }

    /**
     * 1.測試redis的使用
     */
    @Override
    public void testRedis() {
        BosUserModel bosUserModel = new BosUserModel(1,"soup_tang","123456","0");
        //1.將對象保存到redis中
        redisTemplate.opsForValue().set("userModel",bosUserModel);
        //2.從redis中取出這個對象。
        BosUserModel userModel = (BosUserModel) redisTemplate.opsForValue().get("userModel");
        System.out.println("用戶數據:---"+userModel);
    }
}

 

步驟3)

1.在瀏覽器上訪問一下這個接口。http://localhost:8006/testRedis

 

2.控制檯打印語句。

 

3. 從上圖可以看到我已經將數據保存到了redis中,接下來我們可以通過 RedisDesktopManager 這個軟件查看我們剛剛存到redis中的數據。

4. 存取的結果如下圖。

 

3.補充redis的用法

在寫這篇博客之前我有在網上做部分功課,發現有的博主用的是 StringRedisTemplate 我這篇博客用的是RedisTemplate,好奇的我看了一下倆者的區別,

倆個點,第一就是 StringRedisTemplate繼承RedisTemplate,第二個就是StringRedisTemplate 和 RedisTemplate 倆者的數據是不共通的,StringRedisTemplate默認採用的是String的序列化策略,而RedisTemplate 採用的是JDK的序列化策略。


上面部分我們通過redisTemplate 完整的將一個對象存儲到了 redis 中,其實redis不單單可以存儲對象,還可以其他內容,下面這部分是小編的補充內容。

3.1 存取String類型的值:

         //3.操作字符串
        String blogName = "soup_tang";
        redisTemplate.opsForValue().set("blogName",blogName);
        //4.從redis中取出這個值。
        System.out.println("取出來的值-----"+redisTemplate.opsForValue().get("blogName"));

 

關於RedisTemplate的詳細使用,可以參考:https://blog.csdn.net/ruby_one/article/details/79141940

這篇文章就到這裏啦,如果覺得小編寫的不錯的話,不妨給小編一個贊喔 文章中涉及的內容肯定是不全面的,如果你覺得有需要補充的,歡迎評論區留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章