Spring Boot整合Redis-CRUD
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的更多相关文章
- SpringBoot入门系列(七)Spring Boot整合Redis缓存
前面介绍了Spring Boot 中的整合Mybatis并实现增删改查,.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/ ...
- Spring Boot 整合 Redis 和 JavaMailSender 实现邮箱注册功能
Spring Boot 整合 Redis 和 JavaMailSender 实现邮箱注册功能 开篇 现在的网站基本都有邮件注册功能,毕竟可以通过邮件定期的给用户发送一些 垃圾邮件 精选推荐
- (转)spring boot整合redis
一篇写的更清晰的文章,包括redis序列化:http://makaidong.com/ncjava/330749_5285125.html 1.项目目录结构 2.引入所需jar包 <!-- Sp ...
- Spring Boot2 系列教程(二十六)Spring Boot 整合 Redis
在 Redis 出现之前,我们的缓存框架各种各样,有了 Redis ,缓存方案基本上都统一了,关于 Redis,松哥之前有一个系列教程,尚不了解 Redis 的小伙伴可以参考这个教程: Redis 教 ...
- Spring Boot 整合 Redis 实现缓存操作
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』 本文提纲 ...
- spring boot整合redis,以及设置缓存过期时间
spring-boot 整合 redis 注:redis服务器要先开启 pom文件: <dependency> <groupId>org.springframework.boo ...
- spring boot 2.x 系列 —— spring boot 整合 redis
文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...
- Spring Boot2 系列教程(二十九)Spring Boot 整合 Redis
经过 Spring Boot 的整合封装与自动化配置,在 Spring Boot 中整合Redis 已经变得非常容易了,开发者只需要引入 Spring Data Redis 依赖,然后简单配下 red ...
- Spring Boot 整合Redis 实现缓存
本文提纲 一.缓存的应用场景 二.更新缓存的策略 三.运行 springboot-mybatis-redis 工程案例 四.springboot-mybatis-redis 工程代码配置详解 ...
- spring boot 整合 redis
自己开发环境需要安装 redis 服务,百度一下很多,下面主要说明Springboot 集成 redis 讲解 我的版本 java8 + redis3.0 + springboot 1.5.9. Sp ...
随机推荐
- c++学习笔记(入门)
1 struct和class的区别 struct成员变量(成员函数)的访问属性缺省的情况下默认为public. class成员变量(成员函数)的访问属性缺省的情况下默认为private. 2 初始化列 ...
- from 表单非空验证以及多表单提交
开发中我们常用到$('#formid').serialize()方法进行表单序列化提交,但也相应催生了表单的非空严重以及多表单提交. form html: <form id="form ...
- Semaphore信号量源码解析(基于jdk11)
目录 1.Semaphore信号量源码解析(基于jdk11) 1.1 Semaphore概述 1.2 Semaphore的原理 1.2.1 基本结构(jdk11) 1.2.2 可中断获取信号量 1.2 ...
- 【Java】各种数据类型的元素数量
容易混,就算写多了也容易混... 数据类型 元素个数写法 备注 Stack s s.size() s的元素个数 二维数组m[][] m.length m的行数 二维数组m[][] m[0].lengt ...
- xshell+Xftp下载
1.XShell免费官方网站下载地址:https://www.netsarang.com/zh/all-downloads/ 但是官网的地址贼慢,用以下方法下载 2.可以用这个下(写着中文官网):ht ...
- APP异常测试点汇总
在测试APP时异常测试是非常必要的. 安装卸载中的异常测试 一.安装 安装过程中设备重启 安装过程中息屏 安装过程中断网 安装过程中切换网络 安装过程中收到短信提醒 安装过程中收到来电提醒 安装过程中 ...
- week_8
Andrew Ng 机器学习笔记 ---By Orangestar Week_7_Unsupervised Learning While supervised learning algorithms ...
- css images图片铺满 不变型 以及头像裁剪 属性
一,图片的引入 background:url(img_flwr.gif); background-repeat:no-repeat; //平铺 二,图片的大小不不变形 background-size: ...
- 8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
转载自 第一步.确认Git公钥/密钥是否生成: 1. 首先查看本地是否生成git密钥,一般在C盘home目录下:[C:你自己的home目录\.ssh] 第二步:添加Git密钥: 右键->Git ...
- Python实验报告(第13章)
实验13:Pygame游戏编程 一.实验目的和要求 学会Pygame的基本应用 二.Pygame的优点及应用 使用Python进行游戏开发的首选模块就是Pygame,专为电子游戏设计(包括图像.声音) ...