Boot 整合Redis

pom.xml

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

application.yml

spring:
  application:
    name: redis-ceshi
  redis:
    host: 127.0.0.1
    port: 6379
server:
  port: 8888

启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

Controller测试层

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    RedisService redisService;
    @GetMapping("/get")
    public String get(@RequestParam String id){
        return redisService.get(id);
    }
    @PostMapping("/post")
    public String post(@RequestParam String id ,@RequestParam String name ){
        return redisService.post(id,name);
    }

    @DeleteMapping("/delete")
    public String delete(@RequestParam String id){
        return redisService.delete(id);
    }
    @PutMapping("/update")
    public String update(@RequestParam String id , @RequestParam String name){
        return redisService.update(id,name);
    }


}

Service

package com.mine.service;

/**
 * @Author: Wz
 * @Date: 2020/9/4 13:48
 */
public interface RedisService {
    String get(String a);

    String post(String id, String name);

    String update(String id, String name);

    String delete(String id);
}

ServiceImpl

package com.mine.service.impl;

import com.mine.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author: Wz
 * @Date: 2020/9/4 13:49
 */
@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Override
    public String get(String a) {
        return redisTemplate.opsForValue().get(a);
    }

    @Override
    public String post(String id, String name) {
        try {
            redisTemplate.opsForValue().set(id,name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "添加成功";
    }

    @Override
    public String update(String id, String name) {
        redisTemplate.opsForValue().set(id,name);
        return "修改成功";
    }

    @Override
    public String delete(String id) {
        redisTemplate.delete(id);
        return "删除成功";
    }
}

实体类层

package com.mine.domain;

import lombok.Data;

/**
 * @Author: Wz
 * @Date: 2020/9/4 14:17
 */
@Data
public class Student {
    String id;
    String name;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值