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. yum的$releaser与$basearch

    最近配置centos 的yum 源时,想要配置一个通配的yum源,注意到发行的网络yum源的url地址中通常有两个变量 https://vault.centos.org/$releaser/cloud ...

  2. 简单的sql注入1

    首先查看源码找找思路 发现源码里什么都没有 再使用bp拦截下数据 多次拦截后发现我们在 输入框里输入的等下就是id= 意思是我们这里就可以直接使用get注入了 好像类似于sql-labs上的?id= ...

  3. python将列表中的数字合并成一个数字

    前言 今天,写算法题,其中需要进行这一步操作 输入: [1,2,3,4,5] 输出: 12345 解决办法 我首先想到用 join() 函数,但我发现使用join函数要求列表中的元素都是字符串,所以需 ...

  4. day23 JDBC(Java Database Connection)连接 与 通配符与插入返回主键

    JDBC配置connector的jar包 1.项目下新建lib文件夹 2.将mysql-connector-java-版本号.jar复制到lib目录下 3.右键项目名,选择Properties选项 4 ...

  5. Springboot 整合 SpringCache 使用 Redis 作为缓存

    一直以来对缓存都是一知半解,从没有正经的接触并使用一次,今天腾出时间研究一下缓存技术,开发环境为OpenJDK17与SpringBoot2.7.5 SpringCache基础概念 接口介绍 首先看看S ...

  6. 【大数据面试】ClickHouse:介绍、特点、数据类型、引擎、操作、副本、分片

    1.介绍 开源的列式存储数据库(DBMS),由C++编写,用于在线分析处理查询(OLAP) 可以通过SQL查询实时生成分析数据报告 解释: DBMS:数据库管理系统 常见的列式存储数据库:Hbase. ...

  7. python中函数教程

    函数的基本概念 1.什么是函数? 函数相当于一种工具,就是把一串代码装到一起,我们下次需要用的这个功能的时候可以直接使用 函数相当于是工具(具有一定功能) 不用函数 修理工需要修理器件要用锤子 原地打 ...

  8. 动手实验查看MySQL索引的B+树的高度

    一:文中几个概念 h:统称索引的高度: h1:主键索引的高度: h2:辅助索引的高度: k:非叶子节点扇区个数. 二:索引结构 叶子节点其实是双向链表,而叶子节点内的行数据是单向链表,该图未体现. 磁 ...

  9. 日爬百万数据的域名限制、url的清洗和管理

    一.域名去重1.检测开头:link.startswith('http') txt = "Hello, welcome to my world." x = txt.startswit ...

  10. 看起来简单实际上却很牛的KMP算法:LeetCode572-另一棵树的子树

    题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 暴力解法 从 ...