Spring 使用RedisTemplate操作Redis
首先添加依赖:
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
创建:SpringConfig
package the_mass.redis; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate; @Configuration //配置
@ComponentScan("the_mass.redis") //扫描那个包
public class SpringConfig { @Bean
RedisConnectionFactory redisConnectionFactory(){ //获取连接工厂
return new JedisConnectionFactory(); //返回
} @Bean
RedisTemplate redisTemplate(){ //模板
return new StringRedisTemplate(redisConnectionFactory()); //使用连接工厂返回
} }
创建:RedisService
package the_mass.redis; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.stereotype.Service; import java.nio.charset.StandardCharsets; @Service
public class RedisService { @Autowired
RedisConnectionFactory factory; //通过Redis连接的线程安全工厂 @Autowired
RedisOperations redisOperations; //通过公共接口RedisOperations public void testRedis() {
RedisConnection connection = factory.getConnection();
byte[] bytes = connection.get("hello".getBytes());
System.out.println(new String(bytes, StandardCharsets.UTF_8));
} public void testRedisTemplate() {
Object hello = redisOperations.opsForValue().get("hello");
System.out.println(hello);
} }
创建:JedisDemo
package the_mass.redis; import redis.clients.jedis.Jedis; public class JedisDemo {
public void execute() { Jedis jedis = new Jedis(); //创建客户端 Boolean hello = jedis.exists("hello");
System.out.println(hello); String s = jedis.get("hello");
System.out.println(s); jedis.set("hello", "wrold:23"); Long hello1 = jedis.exists("hello", "hello:123");
System.out.println(hello1); }
}
测试:
package the_mass.redis; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); RedisService redisService = context.getBean(RedisService.class);
redisService.testRedis();
redisService.testRedisTemplate();
}
}
注意:首先记得 设置值,不然会报空指针异常
Spring 使用RedisTemplate操作Redis的更多相关文章
- Java 使用Jedis和RedisTemplate操作Redis缓存(SpringBoot)
package com.example.redis.controller; import com.example.redis.entity.User; import com.example.redis ...
- spring data redis RedisTemplate操作redis相关用法
http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
RedisTemplate如何检查一个key是否存在? return getRedisTemplate().hasKey(key); 由一个问题,复习了一下redis 抄自: https://www. ...
- SpringBoot 使用RedisTemplate操作Redis
新版: import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.T ...
- spring-data-redis 中使用RedisTemplate操作Redis
Redis 数据结构简介 Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集合 ...
- RedisTemplate操作Redis
RedisTemplate Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序 ...
- springboot中,使用redisTemplate操作redis
知识点: springboot中整合redis springboot中redisTemplate的使用 redis存数据时,key出现乱码问题 一:springboot中整合redis (1)pom. ...
- RedisTemplate操作Redis(spring-data-redis)
参看博客:https://www.cnblogs.com/songanwei/p/9274348.html 使用文档:StringRedisTemplate+RedisTemplate使用说明
- SpringBoot使用RedisTemplate操作Redis时,key值出现 \xac\xed\x00\x05t\x00\tb
原因分析 原因与RedisTemplate源码中的默认序列化方式有关 defaultSerializer = new JdkSerializationRedisSerializer( classLoa ...
随机推荐
- [转帖]K8s集群安装--最新版 Kubernetes 1.14.1
K8s集群安装--最新版 Kubernetes 1.14.1 http://www.cnblogs.com/jieky/p/10679998.html 原作者写的比较简单 大略流程和跳转的多一些 改天 ...
- EML文件(MIME邮件)格式分析
电子邮件普遍遵循的邮件技术规范.MIME邮件由邮件头和邮件体两部分组成.邮件头包括:标题,送信人,收信人,创建日期,邮件体内容类型和邮件体编码方式等内容.邮件体包括:正文,超文本,内嵌数据和附件等内容 ...
- [BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT)
[BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT) 题面 小C有一个集合S,里面的元素都是小于质数M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数 ...
- js实现404页面倒计时跳转
<script type="text/javascript"> (function(){ var i=5,timer=null,number=document.getE ...
- zprofiler工具
转自:zprofiler三板斧解决cpu占用率过高问题 此工具为阿里自产的profiler工具,在其他文章中看到有用此工具进行性能问题定位的.在此转载文章学习一下. 上周五碰到了一个线上机器cpu占用 ...
- webpack output的path publicPath
path是用来定义入口文件保存的地址,而publicPath是定义非入口文件需要抽离保存的第三方文件的保存地址 vue-cli 中HtmlWebpackPlugin生成html,都会存在path路径上 ...
- Centos克隆虚拟机后配置网络
修改网卡相关信息,复制第二个网卡的mac地址. vim /etc/udev/rules.d/70-persistent-net.rules 修改网卡的信息 vim /etc/sysconfig/net ...
- Python 进程之间共享数据(全局变量)
进程之间共享数据(数值型): import multiprocessing def func(num): num.value=10.78 #子进程改变数值的值,主进程跟着改变 if __name__= ...
- 33. Search in Rotated Sorted Array (JAVA)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- linux常用的小命令
查看linux版本 uname -a 图上可知,linux内核版本为2.6.32 查看cpu核数 cat /proc/cpuinfo |grep "cores"| uniq 系统中 ...