Springboot整合redis

spring Data Redis 操作Redis

1.pom.xml

<?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> <groupId>org.example</groupId>
<artifactId>springbootRedis</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 整合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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

2.创建实体类:

要存储对象必须实现序列化接口

package com.southwind.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Date; @Data
public class Student implements Serializable {
private Integer id;
private String name;
private Double score;
private Date birthday;
}

3.创建控制器:

private RedisTemplate redisTemplate;

package com.southwind.handler;

import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*; @RestController
public class studentHandler {
@Autowired
private RedisTemplate redisTemplate;
@PostMapping("/set")
public void set(@RequestBody Student student){
redisTemplate.opsForValue().set("student",student);
}
@GetMapping("/get/{key}")
public Student get(@PathVariable("key") String key){
return (Student) redisTemplate.opsForValue().get(key);
}
@DeleteMapping("/delete/{key}")
public boolean delete(@PathVariable("key")String key){
redisTemplate.delete(key);
return redisTemplate.hasKey(key);
}
}

4.配置文件:

spring:
redis:
database: 0
host: localhost
port: 6379

5.启动类:

package com.southwind;

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);
}
}

Redis的五种数据类型

  • 字符串:
//    字符串
@GetMapping("/string")
public String addString(String s){
redisTemplate.opsForValue().set("str","hellow");
String str=(String) redisTemplate.opsForValue().get("str");
return str;
}
  • list列表:
//    列表
@GetMapping("/list")
public List<String> addList(){
ListOperations<String,String> listOperations = redisTemplate.opsForList();
// key值是一样的因为是集合push pop
listOperations.leftPush("list","hello");
listOperations.leftPush("list","hello");
listOperations.leftPush("list","hello");
listOperations.leftPush("list","hello");
// 取rangge("key",开始的下标,结束的下标)
List<String> list=listOperations.range("list",1,2);
return list;
}
  • set集合
//    set集合
@GetMapping("/set")
public Set<String> addSet(){
SetOperations setOperations =redisTemplate.opsForSet();
// set集合不能重复
setOperations.add("set","hello");
setOperations.add("set","hello");
setOperations.add("set","b");
setOperations.add("set","b");
Set<String> set =setOperations.members("set");
return set;
}
  • 有序集合
//有序集合 zset
@GetMapping("/zset")
public Set<String> addZset(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("hello","a",1);
zSetOperations.add("hello","a",2);
zSetOperations.add("hello","a",3);
Set<String> set =zSetOperations.range("set",0,3);
return set;
}
  • 哈希

HashMap需要一个key和value

HashOperations key hashkey value

key是每一组数据的ID,hashkey和value是一组完整的HashMap数据,通过key来区分不同的HashMap

HashMap hashMap1 =new HashMap();
hashMap1.put(key1,value1);
HashMap hashMap2 =new HashMap();
hashMap1.put(key2,value3);
HashMap hashMap3 =new HashMap();
hashMap1.put(key3,value3);
HashOperations<String,String> hashOperation =redisTemplate.opsForHash();
hashOperation.put(hashMap1,key1,value1);
hashOperation.put(hashMap2,key1,value1);
hashOperation.put(hashMap3,key1,value1);
//    哈希
@GetMapping("/hash")
public void hashMap(){
HashOperations<String,String,String> hashOperations =redisTemplate.opsForHash();
hashOperations.put("key","hashkey","hello");
hashOperations.get("key","hashkey");
}

Spring Boot整合Redis-CRUD的更多相关文章

  1. SpringBoot入门系列(七)Spring Boot整合Redis缓存

    前面介绍了Spring Boot 中的整合Mybatis并实现增删改查,.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/ ...

  2. Spring Boot 整合 Redis 和 JavaMailSender 实现邮箱注册功能

    Spring Boot 整合 Redis 和 JavaMailSender 实现邮箱注册功能 开篇 现在的网站基本都有邮件注册功能,毕竟可以通过邮件定期的给用户发送一些 垃圾邮件 精选推荐

  3. (转)spring boot整合redis

    一篇写的更清晰的文章,包括redis序列化:http://makaidong.com/ncjava/330749_5285125.html 1.项目目录结构 2.引入所需jar包 <!-- Sp ...

  4. Spring Boot2 系列教程(二十六)Spring Boot 整合 Redis

    在 Redis 出现之前,我们的缓存框架各种各样,有了 Redis ,缓存方案基本上都统一了,关于 Redis,松哥之前有一个系列教程,尚不了解 Redis 的小伙伴可以参考这个教程: Redis 教 ...

  5. Spring Boot 整合 Redis 实现缓存操作

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!   『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』   本文提纲 ...

  6. spring boot整合redis,以及设置缓存过期时间

    spring-boot 整合 redis 注:redis服务器要先开启 pom文件: <dependency> <groupId>org.springframework.boo ...

  7. spring boot 2.x 系列 —— spring boot 整合 redis

    文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...

  8. Spring Boot2 系列教程(二十九)Spring Boot 整合 Redis

    经过 Spring Boot 的整合封装与自动化配置,在 Spring Boot 中整合Redis 已经变得非常容易了,开发者只需要引入 Spring Data Redis 依赖,然后简单配下 red ...

  9. Spring Boot 整合Redis 实现缓存

      本文提纲 一.缓存的应用场景 二.更新缓存的策略 三.运行 springboot-mybatis-redis 工程案例 四.springboot-mybatis-redis 工程代码配置详解   ...

  10. spring boot 整合 redis

    自己开发环境需要安装 redis 服务,百度一下很多,下面主要说明Springboot 集成 redis 讲解 我的版本 java8 + redis3.0 + springboot 1.5.9. Sp ...

随机推荐

  1. linux子网掩码修改记录

    1.输入密码进入linux,并且进入root2.输入ifconfig.返回网卡信息,释:其中eno1为当前以太网名称.Inet IP/子网掩码位置数 Bcast广播地址 或者mask子网掩码3.修改子 ...

  2. Java开发学习(四十二)----MyBatisPlus查询语句之条件查询

    一.条件查询的类 MyBatisPlus将书写复杂的SQL查询条件进行了封装,使用编程的形式完成查询条件的组合. 这个我们在前面都有见过,比如查询所有和分页查询的时候,都有看到过一个Wrapper类, ...

  3. SpringCLoud_Aibaba

    微服务项目核心组件 https://gitee.com/gtnotgod/spring-cloud_-alibaba_-study001.git 注册中心:nacos API网关:gateway 生产 ...

  4. combotree 的简单使用

    一.前端 combotree HTML: <input id="201711281652407353448711985811" class="easyUI-comb ...

  5. python-面向对象属性的访问与self的理解

    属性访问 类属性与对象属性 在类中定义的名字,都是类的属性,细说的话,类有两种属性:数据属性和函数属性,可以通过__dict__访问属性的值,比如Person1.__dict__['student'] ...

  6. 异常处理语法结构、yield生成器及其表达式

    今日内容回顾 目录 今日内容回顾 异常处理语法结构 异常处理实战应用 生成器对象 自定义range功能 yield冷门用法 yield与return对比 生成器表达式 笔试题 异常处理语法结构 异常处 ...

  7. java8新特性学习笔记

    目录 1.速度更快 2.Lambda表达式 2.1.匿名内部类的Lambda转换 2.2.java8内置的四大核心函数式接口 2.3.方法引用和构造器 2.3.1.方法引用 2.3.2.构造器引用 2 ...

  8. S2-048 CVE-2017-9791 远程命令执行

    漏洞名称 S2-048 CVE-2017-9791 远程命令执行 利用条件 Struts 2.3.x 开启Struts 1 plugin and Struts 1 action插件 漏洞原理 漏洞产生 ...

  9. Java基础篇——JUC初步

    1.基础知识 java默认的两个线程:Main线程+GC守护线程 java并不能开启线程,需要调用底层用c语言写的本地方法 wait和sleep的区别: wait方法会释放线程锁,并且只能在同步代码块 ...

  10. NET-async-await是否会创建新线程

    title: .NET async/await是否会创建新线程 date: 2022-12-06 10:36:46 tags: - .NET 先上结论 CPU密集型操作,比如计算,如果不使用Task, ...