JavaSpring项目之连接,并使用redis
目录
1.如何在创建spring项目是正确的引入使用redis的依赖
2.StringRedisTemplate 使用string
2.1 StringRedisTemplate
2.2 @Autowired
3、使用execute方法去执行redis的原指令
上一篇我们讲了如何安装和下载Jedis,以及如何在Java的maven项目中使用Jedis去操作我们的redis,用法其实是很简单的,语法和redis的几乎已一模一样,可以说Jedis就是redis的再次副本也不为过;但是今天要分享的就有所不也一样了,但是只要掌握了几个重要的点,也是很简单的;
1.如何在创建spring项目是正确的引入使用redis的依赖
只需要在如下页面中选择正确的Spring Data Redis即可,不过spring boot的版本对redis的配置有点影响,后面会说到;
在Spring Boot应用程序中,application.properties
配置文件是用于配置应用程序属性的重要资源。这个文件提供了了一种简便的方式来配置Spring Boot应用程序的各种组件,如数据源、数据库、缓存、邮件服务等。但是目前更被广泛应用的是application.yml,我们只需要修改文件名类型为yml即可;以下是redis的配置信息,配置redis服务端的ip和端口号,就能够正常与redis服务器进行通信;
如果是spring boot 2.0+版本就使用这个配置内容 spring: redis:host: 127.0.0.1 #ipport: 8888 #端口号如果是spring boot 3.0+版本就使用这个配置内容 spring:data:redis:host: 127.0.0.1 #ip port: 8888 #端口号
至于为什么是127.0.0.1和8888,这里简单说一下,是因为我把redis的服务器通过xshell的ssh隧道映射到本地的8888端口,详细可以参考我的上一篇文章;
使用Java连接redis以及开放redis端口的问题-CSDN博客
配置到这里,就可以正常的写代码,然后与redis服务器进行通信了;记下来简单介绍一下如何使用StringRedisTemplate类去写相关的代码,以及如何使用五大数据类型,String,list,hash,set,zset,去存取数据,但是我只会简单讲一下String类型这个,因为其他都是一样的;
2.StringRedisTemplate 使用string
@Autowired
private StringRedisTemplate redisTemplate;
2.1 StringRedisTemplate
StringRedisTemplate
: 这是Spring Data Redis提供的一个核心类(模板类)。你可以把它想象成一个专门用于操作Redis数据库的“瑞士军刀”或“工具包”。
“String”的含义: 它特指在Redis中存储的键(Key) 和值(Value) 都是String
类型。这是最常用的序列化方式,因为Redis本身协议就是基于字符串的,非常直观和高效。实际上StringRedisTemplate是RedisTemplate的子类
功能: 这个类内部封装了与Redis服务器进行通信的所有复杂细节,并提供了各种简单易用的方法来操作Redis,比如:
opsForValue()
: 用于操作简单的字符串值。
opsForList()
: 用于操作列表(List)类型。
opsForHash()
: 用于操作哈希(Hash)类型。
opsForSet()
: 用于操作集合(Set)类型。
opsForZset()
: 用于操作有序集合(Zset)类型。
这里在简单介绍一Autowire的含义
2.2 @Autowired
这是Spring框架的核心注解之一,意思是 “自动装配” 或 “依赖注入”。
作用: 它告诉Spring容器(Spring Container):“请自动查找一个类型为StringRedisTemplate
的Bean(即一个由Spring管理的对象实例),然后把它赋值(注入)给这个redisTemplate
变量。”
过程: 当Spring启动时,它会根据你的配置(比如在配置类上使用了@EnableRedisRepositories
)自动创建一个StringRedisTemplate
的实例,并把它放在自己的“容器”里管理。当它看到@Autowired
注解时,就会把这个实例“注入”到你的类中,你无需手动使用new
关键字来创建它。
如果你是直接用变量名redisTemplate.,在ieda中你将看不到任何属性的方法,比如set或者get等,这是因为这个类内部封装了与Redis服务器进行通信的所有复杂细节,并提供了各种简单易用的方法来操作Redis,比如:opsForValue()
: 用于操作简单的字符串值。所以如果你要操作的数据类型是string,你就输入变量名.opsForValue(),你就会看到很多熟悉的办法,比如set,get,increment等等,只是看方法名,就大概能够查到是什么意思了;不过要建立在你了解redis原指令的基础上;
package com.example.demo.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class controllerForValue {@Autowiredprivate StringRedisTemplate redisTemplate;@GetMapping("/test01")public String test01(){System.out.println("set 和 get ");redisTemplate.opsForValue().set("key","111");String value=redisTemplate.opsForValue().get("key");System.out.println("value:"+value);return "ok";redisTemplate.opsForValue().}
}
比如以上的代码,不用我多说,大家大概都能猜到是什么意思,首先我添加了一个键值对,然后又把值取了出来,并输出到控制台上;
执行结果:
通过以上简单的介绍,相信大家应该可以在Java spring项目中使用redis了;
3、使用execute方法去执行redis的原指令
@GetMapping("/test02")public String test02(){System.out.println("execute");redisTemplate.execute((RedisConnection connection)->{connection.flushAll();return null;});return "";}
虽然StringRedisTemplate无法直接像Jedis去执行redis指令,只能通过opsFor等方法,但是提供了另外一个execute方法,也是一个回掉方法,让我们可以直接去执行像redis原指令一样名字的方法,就比如你直接用StringTemplate无法直接去调用flushall()去清楚掉所有redis数据,也没有办法通过以上五种数据类型的方法去调用,但是你可以使用execute方法是使用像redis原指令一样名字的方法。
为什么必须返回null,这是因为execute就规定回调方法就必须使用return语句,返回一点东西,而且execute也会将返回值作为自己的返回值返回;