springboot与redis

SpringBoot与Redis 整合

springboot整合redis其实是spring-data-redis框架操作redis

redis 安装与使用

redis安装与使用

springboot 与redis整合

1 创建springboot的maven工程
  • 引入相关依赖
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--spring data redis 框架引入-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--引入客户端连接池-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <!--redis数据与java实体类转化工具-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
  • 创建配置文件application.yml
debug: true
spring:
  redis:
    database: 0
    host: localhost
    port: 6379
  devtools:
    restart:
      enabled: true
  freemarker:
    cache: false
  • 创建实体
package com.spring.vo;

import lombok.Data;

import java.io.Serializable;

@Data
public class UserVO implements Serializable {
    private Long userId;
    private String name;
    private String age;
    private String phone;
    private String location;
    private String resultCode;
    private String resultMsg;
    public Long getUserId() {
        return userId;
    }
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public String getResultCode() {
        return resultCode;
    }
    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }
    public String getResultMsg() {
        return resultMsg;
    }
    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }
}

  • 创建控制类
package com.spring.controller;

import com.spring.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.*;

@RestController
public class UserController {
    @Autowired
    private
    RedisTemplate redisTemplate;

    @PostMapping("/addUser")
    public UserVO addUser(@RequestBody UserVO userVO){
        UserVO curUserVO = userVO;
        if(null == userVO || null == userVO.getUserId()){
            curUserVO = new UserVO();
            curUserVO.setResultCode("10001");
            curUserVO.setResultMsg("Add User Error");
            return curUserVO;
        }
        redisTemplate.opsForValue().set("USER_"+curUserVO.getUserId(),curUserVO);
        curUserVO.setResultCode("00000");
        return curUserVO;
    }

    @GetMapping("/queryUserInfo/{userId}")
    public UserVO queryUserInfo(@PathVariable("userId") Long userId){
        return (UserVO)redisTemplate.opsForValue().get("USER_"+userId);
    }
    @DeleteMapping("/deleteUser/{userId}")
    public boolean deleteUser(@PathVariable("userId") Long userId){
        redisTemplate.delete("USER_"+ userId);
        return redisTemplate.hasKey("USER_"+ userId);
    }

    @GetMapping("/optionList/{userId}")
    public List<UserVO> queryUserList(@PathVariable("userId") Long userId,@RequestParam("beginIndex") Long beginIndex,
                                      @RequestParam("endIndex") Long endIndex){
        UserVO curUserVO = (UserVO)redisTemplate.opsForValue().get("USER_"+ userId);
        List<UserVO> userVOList = new ArrayList<>();
        for(int id =1; id <10;id++){
            UserVO newUser = new UserVO();
            newUser.setUserId(curUserVO.getUserId()+id);
            newUser.setName(curUserVO.getName()+id);
            userVOList.add(newUser);
        }
        // 清除list
        redisTemplate.delete("ADMIN_USER");
        redisTemplate.opsForList().rightPushAll("ADMIN_USER",userVOList);
        if(endIndex == null){
            endIndex = redisTemplate.opsForList().size("ADMIN_USER");
        }
        beginIndex = beginIndex == null? 0: beginIndex ;
        return redisTemplate.opsForList().range("ADMIN_USER",beginIndex,endIndex);
    }

    @GetMapping("/optionSet")
    public Set<String> queryUserSet(){
        //ZSet 为有序集合
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("SET_001","Hello");
        setOperations.add("SET_001","Hello");
        setOperations.add("SET_001","Redis");
        setOperations.add("SET_001","Redis");
        return  setOperations.members("SET_001");
    }

    @GetMapping("/optionMap")
    public Object queryUserList( @RequestParam("key") String mapKey){
        HashOperations hashOperations =  redisTemplate.opsForHash();
        hashOperations.put("HASH-MAP","MAP-1","map1-vlue");
        hashOperations.put("HASH-MAP","MAP-2","map2-vlue");
        hashOperations.put("HASH-MAP","MAP-3","map3-vlue");
        return redisTemplate.opsForHash().get("HASH-MAP",mapKey);
    }
}

  • 创建启动类
package com;

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

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

  • 项目层次文件
    在这里插入图片描述

  • 验证结果
    在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值