SpringBoot模块系列:Redis增删改查

25 篇文章 7 订阅

1 pom.xml

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

2 application.yml

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password: china@asia 
    jedis: 
      pool: 
        max-active: 8 
        max-idle: 8
        max-wait: -1ms 
        min-idle: 0

3 配置

package com.sb.config;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

@Configuration 
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig{
    @Bean 
    public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

4 实体类

package com.sb.po;

import java.io.Serializable;

public class PeopleInfos implements Serializable{
    private String name;
    private String address;

    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }

    public void setAddress(String address){
        this.address = address;
    }
    public String getAddress(){
        return address;
    }
}

5 增删改查

package com.sb.controller;

import com.sb.po.PeopleInfos;
import java.util.Map;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;

@CrossOrigin(origins="*", maxAge=3600) 
@Controller 
@RequestMapping("/api/redis")
public class RedisOpController{
    @Autowired 
    private StringRedisTemplate stringRedisTemplate;
    @Autowired 
    private RedisTemplate redisTemplate;

    @RequestMapping(value="/add", method=RequestMethod.POST)
    @ResponseBody 
    public Map addDataToRedis(@RequestBody Map infos){
        Map tempMap = new HashMap();
        try{
            if(infos.containsKey("username")&&infos.containsKey("password")&&infos.containsKey("address")&&infos.containsKey("objname")){
            
                String username = infos.get("username").toString();
                String password = infos.get("password").toString();
                String address = infos.get("address").toString();
                String objname = infos.get("objname").toString();
                stringRedisTemplate.opsForValue().set("username", username);
                stringRedisTemplate.opsForValue().set("password", password);
                PeopleInfos peopleInfos = new PeopleInfos();
                peopleInfos.setName(username);
                peopleInfos.setAddress(address);
                redisTemplate.opsForValue().set(objname, peopleInfos);
                tempMap.put("code", 200);
                tempMap.put("infos", "添加成功");
                return tempMap;
            }else{
                tempMap.put("code", 400);
                tempMap.put("infos", "检查参数");
                return tempMap;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return tempMap; 
    }

    @RequestMapping(value="/delete")
    @ResponseBody 
    public Map deleteByKey(@RequestBody Map infos){
        Map tempMap = new HashMap();
        try{
            if(infos.containsKey("username")&&infos.containsKey("password")&&infos.containsKey("objname")){
                String username = infos.get("username").toString();
                String password = infos.get("password").toString();
                String objname = infos.get("objname").toString();
                stringRedisTemplate.delete(username);
                stringRedisTemplate.delete(password);
                redisTemplate.delete(objname);
                tempMap.put("code", 200);
                tempMap.put("infos", "删除成功");
            }else{
                tempMap.put("code", 400);
                tempMap.put("infos", "检查参数");

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return tempMap;
    }

    @RequestMapping(value="/search/obj", method=RequestMethod.POST) 
    @ResponseBody 
    public PeopleInfos searchDataInRedis(@RequestBody Map infos){
        PeopleInfos peopleInfos = new PeopleInfos();
        try{
            if(infos.containsKey("objname")){
                String objname = infos.get("objname").toString();
                peopleInfos = (PeopleInfos) redisTemplate.opsForValue().get(objname);
                redisTemplate.opsForValue().set("userInfosObj", peopleInfos);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return peopleInfos;
    }

    @RequestMapping(value="/search/string", method=RequestMethod.POST) 
    @ResponseBody 
    public Map searchDataInRedisString(@RequestBody Map infos){
        Map infosOut = new HashMap();
        String usernameR, passwordR;
        try{
            if(infos.containsKey("username")&&infos.containsKey("password")){
                String username = infos.get("username").toString();
                String password = infos.get("password").toString();
                usernameR = stringRedisTemplate.opsForValue().get(username);
                passwordR = stringRedisTemplate.opsForValue().get(password);
                infosOut.put("username", usernameR);
                infosOut.put("password", passwordR);
                
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return infosOut;
    }
}

6 小结

序号描述
1配置redis使用序列化存储RedisTemplate<String, Serializable>
2实体类数据存储使用序列化
3存储字符串使用StringRedisTemplate,存储对象使用RedisTemplate
4数据查询使用键名搜索

【参考文献】
[1]https://blog.csdn.net/stronglyh/article/details/81173563
[2]https://blog.csdn.net/pw191410147/article/details/82144183

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值