Introduction to SpringBoot+Redis

Keywords: Java Redis Spring github Maven

This blog Uncle cat's blog , reprint please state the source

This series of tutorials is HMStrange project Incidental.

Historical articles

Tutorial content

Note: this series of development tools are IDEA

1. To build a project, choose three basic Maven dependencies: Lombok (I found later that it was not used), Web and Redis.

pom file

<?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. Prepare for Redis. Here you can refer to the installation of Redis in the historical article. I use the Redis Desktop Manager desktop tool to link

3. To build a project directory, I built a common web project directory, which cuts the dao layer, and only consists of service and controller

4. Fill in application.yml, the default generation is not YML, but I think the visual effect of YML is better, so we need to fill in the Redis link information

spring:
  redis:
    host: 192.168.192.133

5. Build a service interface, where two set and get information businesses are generally implemented

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. Interface implementation, introduction of 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. The business of the controller layer is realized

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. Start the project and use Postmane test

9. Project download address

Welcome to hmspan project for download: https://github.com/UncleCatMy...

Public number: Java cat says

Learning exchange group: 728698035

Now architecture design (code farming) and entrepreneurial technology consultant, uninhibited mediocre, love open source, talk about program life and irregular dry goods.

Posted by beselabios on Fri, 29 Nov 2019 12:39:27 -0800