首先添加依赖:

<!-- 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的更多相关文章

  1. Java 使用Jedis和RedisTemplate操作Redis缓存(SpringBoot)

    package com.example.redis.controller; import com.example.redis.entity.User; import com.example.redis ...

  2. spring data redis RedisTemplate操作redis相关用法

    http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...

  3. Spring中使用RedisTemplate操作Redis(spring-data-redis)

    RedisTemplate如何检查一个key是否存在? return getRedisTemplate().hasKey(key); 由一个问题,复习了一下redis 抄自: https://www. ...

  4. SpringBoot 使用RedisTemplate操作Redis

    新版: import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.T ...

  5. spring-data-redis 中使用RedisTemplate操作Redis

    Redis 数据结构简介 Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集合 ...

  6. RedisTemplate操作Redis

    RedisTemplate Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序 ...

  7. springboot中,使用redisTemplate操作redis

    知识点: springboot中整合redis springboot中redisTemplate的使用 redis存数据时,key出现乱码问题 一:springboot中整合redis (1)pom. ...

  8. RedisTemplate操作Redis(spring-data-redis)

    参看博客:https://www.cnblogs.com/songanwei/p/9274348.html 使用文档:StringRedisTemplate+RedisTemplate使用说明

  9. SpringBoot使用RedisTemplate操作Redis时,key值出现 \xac\xed\x00\x05t\x00\tb

    原因分析 原因与RedisTemplate源码中的默认序列化方式有关 defaultSerializer = new JdkSerializationRedisSerializer( classLoa ...

随机推荐

  1. [转帖]探秘华为(一):华为和H3C(华三)的爱恨情仇史!

    探秘华为(一):华为和H3C(华三)的爱恨情仇史! https://baijiahao.baidu.com/s?id=1620703498823290828&wfr=spider&fo ...

  2. JSP技术学习总结

    1.JSP的执行过程 首先用户向服务器发出请求,服务器在接收请求后去寻找响应的jsp页面,然后服务器将jsp页面翻译成.java文件,然后进行编译得到.class字节码文件,服务器执行class文件将 ...

  3. HashMap中确定数组位置为什么要用hash进行扰动

    HashMap数据存储的过程先根据key获得hash值,通过 (n - 1) & hash 判断当前元素存放的位置(这里的 n 指的是数组的长度),如果当前位置存在元素的话,就判断该元素与要存 ...

  4. Java数据结构之算法时间度

    1.度量一个程序(算法)执行时间的两种方法 1)事后统计的方法 这种方法可行, 但是有两个问题:一是要想对设计的算法的运行性能进行评测,需要实际运行该程序:二是所得时间的统计量依赖于计算机的硬件.软件 ...

  5. kotlin学习(6)运算符重载和其他约定

    约定 在Kotlin中,可以调用自己代码中定义的函数,来实现语言结构.这戏功能与特定的函数命名相关,例如,在你的类中定义了一个名为plus的特殊方法,那么按照约定,就可以在该类的实例上使用 + 运算符 ...

  6. ES6初步学习

    from:http://www.jianshu.com/p/287e0bb867ae 刚开始用vue或者react,很多时候我们都会把ES6这个大兄弟加入我们的技术栈中.但是ES6那么多那么多特性,我 ...

  7. Vue 数据持久化

    方法一:使用 localStorage 存储数据 window.localStorage.setItem(key,value) 方法二:使用 vuex-persistedstate插件 vuex 存在 ...

  8. Python爬虫之selenium高级功能

    Python爬虫之selenium高级功能 原文地址 表单操作 元素拖拽 页面切换 弹窗处理 表单操作 表单里面会有文本框.密码框.下拉框.登陆框等. 这些涉及与页面的交互,比如输入.删除.点击等. ...

  9. Linux-date函数

    rhel7 date函数 显示本地时间?设定当前系统的时间,以一定格式显示当前时间,如X-X-X /X:X:X 使用man date命令查看关于date的使用方法 SYNOPSIS           ...

  10. 算法学习 howto

    入门: The Most Important Algorithms http://www.risc.jku.at/people/ckoutsch/stuff/e_algorithms.html Alg ...