SpringDataRedis入门到实战

2024-04-10 12:58

本文主要是介绍SpringDataRedis入门到实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

5.Spring Data Redis入门实例
准备工作
(1)构建Maven工程 SpringDataRedisDemo
(2)引入Spring相关依赖、引入JUnit依赖 (内容参加其它工程)
(3)引入Jedis和SpringDataRedis依赖
pom.xml


4.0.0
com.ldc.org
SpringDataRedisDemo
0.0.1-SNAPSHOT

<!-- 集中定义依赖版本号 -->
<properties><junit.version>4.12</junit.version><spring.version>4.2.4.RELEASE</spring.version>
</properties><dependencies><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version></dependency><!-- 缓存 --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.1</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.7.2.RELEASE</version></dependency></dependencies>
(4)在src/main/resources下创建properties文件夹,建立redis-config.properties

redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
(5)在src/main/resources下创建spring文件夹 ,创建applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?>

<context:property-placeholder location=“classpath*:properties/*.properties” />

maxIdle :最大空闲数 maxWaitMillis:连接时的最大等待毫秒数 testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的; 6.值类型操作 package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations=“classpath:spring/applicationContext-redis.xml”)
public class TestValue {

@Autowired
private RedisTemplate redisTemplate;@Test
public void setValue() {redisTemplate.boundValueOps("name").set("ldc");
}@Test
public void getValue() {String string=(String) redisTemplate.boundValueOps("name").get();System.out.println(string);
}@Test
public void deleteValue() {redisTemplate.delete("name");
}

}
7. Set类型操作
package test;

import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations=“classpath:spring/applicationContext-redis.xml”)
public class TestSet {

@Autowired
private RedisTemplate redisTemplate;@Test
public void setValue() {//存进去和顺序无关redisTemplate.boundSetOps("nameSet").add("曹操");redisTemplate.boundSetOps("nameSet").add("刘备");redisTemplate.boundSetOps("nameSet").add("孙权");
}@Test
public void getValue() {Set set=redisTemplate.boundSetOps("nameSet").members();System.out.println(set);
}@Test
public void removeValue() {//单独的移除其中一个元素redisTemplate.boundSetOps("nameSet").remove("孙权");
}@Test
public void delete() {//移除全部redisTemplate.delete("nameSet");
}

}
8.List类型操作
package test;

import java.util.List;
import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations=“classpath:spring/applicationContext-redis.xml”)
public class TestList {

@Autowired
private RedisTemplate redisTemplate;/*** 右压栈*/
@Test
public void setValue1() {redisTemplate.boundListOps("nameList1").rightPush("刘备");redisTemplate.boundListOps("nameList1").rightPush("关羽");redisTemplate.boundListOps("nameList1").rightPush("张飞");
}/*** 显示右压栈的值*/
@Test
public void getValue1() {List list=redisTemplate.boundListOps("nameList1").range(0, 10);System.out.println(list);
}/*** 左压栈*/
@Test
public void setValue2() {redisTemplate.boundListOps("nameList2").leftPush("刘备");redisTemplate.boundListOps("nameList2").leftPush("关羽");redisTemplate.boundListOps("nameList2").leftPush("张飞");
}/*** 显示左压栈的值*/
@Test
public void getValue2() {List list=redisTemplate.boundListOps("nameList2").range(0, 10);System.out.println(list);
}/*** 按照索引查询*/
@Test
public void searchByIndex() {String string=(String) redisTemplate.boundListOps("nameList2").index(1);System.out.println(string);
}/*** 删除其中一个元素*/
@Test
public void removeValue() {//单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表redisTemplate.boundSetOps("nameList1").remove(1,"关羽");
}/*** 删除整个List集合*/
@Test
public void delete() {//单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表redisTemplate.delete("nameList1");
}

}
9.Hash类型操作
package test;

import java.util.List;
import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations=“classpath:spring/applicationContext-redis.xml”)
public class TestHash {

@Autowired
private RedisTemplate redisTemplate;/*** 存值*/
@Test
public void setValue() {redisTemplate.boundHashOps("nameHash").put("a", "唐僧");redisTemplate.boundHashOps("nameHash").put("b", "悟空");redisTemplate.boundHashOps("nameHash").put("c", "八戒");redisTemplate.boundHashOps("nameHash").put("d", "沙僧");
}/*** 取所有key*/
@Test
public void getKey() {Set keys=redisTemplate.boundHashOps("nameHash").keys();System.out.println(keys);
}/*** 取所有value*/
@Test
public void getValue() {List list=redisTemplate.boundHashOps("nameHash").values();System.out.println(list);
}/*** 根据key取值*/
@Test
public void searchValueByKey() {String string=(String) redisTemplate.boundHashOps("nameHash").get("b");System.out.println(string);
}/*** 移除某个小key的值*/
@Test
public void removeValueByKey() {redisTemplate.boundHashOps("nameHash").delete("c");
}/*** 删除其中一个元素*/
@Test
public void removeValue() {//单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表redisTemplate.boundSetOps("nameList1").remove(1,"关羽");
}/*** 删除整个List集合*/
@Test
public void delete() {//单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表redisTemplate.delete("nameList1");
}

}
龙华大道1号http://www.kinghill.cn/LongHuaDaDao1Hao/index.html

这篇关于SpringDataRedis入门到实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/891127

相关文章

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

从原理到实战解析Java Stream 的并行流性能优化

《从原理到实战解析JavaStream的并行流性能优化》本文给大家介绍JavaStream的并行流性能优化:从原理到实战的全攻略,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的... 目录一、并行流的核心原理与适用场景二、性能优化的核心策略1. 合理设置并行度:打破默认阈值2. 避免装箱

Maven中生命周期深度解析与实战指南

《Maven中生命周期深度解析与实战指南》这篇文章主要为大家详细介绍了Maven生命周期实战指南,包含核心概念、阶段详解、SpringBoot特化场景及企业级实践建议,希望对大家有一定的帮助... 目录一、Maven 生命周期哲学二、default生命周期核心阶段详解(高频使用)三、clean生命周期核心阶

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Java Scanner类解析与实战教程

《JavaScanner类解析与实战教程》JavaScanner类(java.util包)是文本输入解析工具,支持基本类型和字符串读取,基于Readable接口与正则分隔符实现,适用于控制台、文件输... 目录一、核心设计与工作原理1.底层依赖2.解析机制A.核心逻辑基于分隔符(delimiter)和模式匹