SpringBoot+Redis的入門教程

本博客 貓叔的博客,轉載請申明出處

本系列教程爲HMStrange項目附帶。

歷史文章

教程內容

備註:本系列開發工具均爲IDEA

1、構建項目,選擇Lombok(後面發現其實沒有用到)、Web、Redis三個基本的Maven依賴。

image

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.github.myself</groupId>
    <artifactId>redisdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>redisdemo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、準備Redis,這裏可以參考歷史文章的安裝Redis環節,我使用Redis Desktop Manager桌面工具進行鏈接

image

3、構建項目目錄,我構建了一個普通的web項目目錄,切了dao層,僅僅由service、controller

image

4、填寫application.yml,默認生成不是yml,不過我覺得yml視覺效果好一些,就改了一下,我們需要填寫Redis鏈接信息

spring:
  redis:
    host: 192.168.192.133

5、構建service接口,這裏普通實現兩個set、get信息的業務

package com.github.myself.service;

/**
 * Created by MySelf on 2019/4/11.
 */
public interface MsgService {

    public String setMsg(String key,String msg);

    public String getMsg(String key);

}

6、接口實現,引入RedisTemplate

package com.github.myself.service.impl;

import com.github.myself.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by MySelf on 2019/4/11.
 */
@Service
public class MsgServiceImpl implements MsgService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String setMsg(String key,String msg) {
        redisTemplate.opsForValue().set(key,msg);
        return "success";
    }

    @Override
    public String getMsg(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
}

7、controller層的業務實現了

package com.github.myself.controller;

import com.github.myself.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by MySelf on 2019/4/11.
 */
@RestController
@RequestMapping("/msg")
public class MsgController {

    @Autowired
    private MsgService msgService;

    @GetMapping("/set")
    public String setMsg(@RequestParam(value = "key") String key,@RequestParam(value = "msg") String msg){
        return msgService.setMsg(key,msg);
    }

    @GetMapping("/get")
    public String getMsg(@RequestParam(value = "key") String key){
        return msgService.getMsg(key);
    }

}

8、啓動項目,使用Postmane測試

image

image

image

9、項目下載地址

歡迎到HMStrange項目進行下載:https://github.com/UncleCatMy...

公衆號:Java貓說

學習交流羣:728698035

現架構設計(碼農)兼創業技術顧問,不羈平庸,熱愛開源,雜談程序人生與不定期乾貨。

Image Text

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