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 ...
随机推荐
- [转帖]Oracle dba_objects和all_objects 最大的区别
Oracle dba_objects和all_objects 最大的区别 原创 Oracle 作者:maohaiqing0304 时间:2015-08-14 15:07:18 9281 0 链 ...
- 通过metaclass实现精简的ORM框架
摘抄于<python-3-廖雪峰> 使用ORM框架 class User(Model): #定义类的属性到列的映射: id = IntegerFiled('id') name = Stri ...
- Vim实用技巧(一)
vim 命令按键规定 标记 含义 x 按一次 x dw 按一次 d, w dap 按一次 d, a, p 同时按 和 n g<C-]> 按 g, 然后同时按 和 ] <C-=> ...
- npm学习(十三)之npm命令
npm:查看npm所有命令 自己写包可能用到的命令: npm adduser:注册 npm login:登录 npm whami:查看当前用户名 npm init:初始化包的信息 npm publis ...
- mac安装卸载brew
1.安装 访问https://brew.sh,copy图中的命令到命令行中,进行下载安装 2.卸载 官方版本的卸载: /usr/bin/ruby -e "$(curl -fsSL https ...
- CSS行内框(内联元素)
行内框在一行中水平布置.可以使用水平内边距.边框和外边距调整它们的间距.但是,垂直内边距.边框和外边距不影响行内框的高度.由一行形成的水平框称为行框(Line Box),行框的高度总是足以容纳它包含的 ...
- intellij idea 的快捷键方法
1.Ctrl+N按名字搜索类 相当于eclipse的ctrl+shift+R,输入类名可以定位到这个类文件,就像idea在其它的搜索部分的表现一样,搜索类名也能对你所要搜索的内容多个部分进行匹配,而且 ...
- python 查询Neo4j多节点的多层关系
需求:查询出满足3人及3案有关系的集合 # -*- coding: utf-8 -*- from py2neo import Graph import psycopg2 # 二维数组查找 def fi ...
- 关于Mysql group by 的记录
对于有group by 字段的select语句,group by 后面的字段如果没有出现在组函数里(max,min,sum,avg, count等),则一定要出现在select后面的字段里, 否则会报 ...
- 微信小程序(19)-- 从底部向上滑出的动画效果
从底部向上滑出的动画效果: 用到了小程序的触摸事件bindtouchmove,以及创建一个annimation对象,完成动画操作之后使用animation这个对象的export()方法导出动画数据. ...