首先添加依赖:

<!-- 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();
}
}

注意:首先记得 设置值,不然会报空指针异常

最新文章

  1. Redis Sentinel机制与用法说明【转】
  2. jsp连接SQL Server数据库的方式
  3. Asp.net Mvc模块化开发之分区扩展框架
  4. poj 1324 Holedox Moving
  5. ccpc 2016 省赛
  6. linx 实用操作命令二
  7. VPW协议解析
  8. Canvas使用笔记
  9. SQL 判断字段中指定字符出现的次数
  10. XFire中Services.xml 配置的一些细节
  11. EasyUI篇のico
  12. scrapy_对传到items的值预处理
  13. 解决Chrome浏览器访问https提示“您的连接不是私密连接”的问题
  14. C 标准库头文件
  15. bootstrap模态框手动关闭遮盖层不消失
  16. Java中关于Map的使用(HashMap、ConcurrentHashMap)
  17. sh - 脚本学习 启动/停止/重启/部署jetty crontab
  18. jq ajax post body raw传json
  19. CSS day49
  20. java中使用相对路径读取文件的写法总结 ,以及getResourceAsStream() (转)

热门文章

  1. [转帖]PostgreSQL ident和peer基于操作系统用户的认证
  2. XLS导出的服务器端配置方式
  3. 极*Java速成教程 - (5)
  4. mysql 修改表字段默认值
  5. 分布式事务解决方案汇总:2PC、3PC、消息中间件、TCC、状态机+重试+幂等(转)
  6. linux查找进程id和杀死进程以及查看内存??
  7. html5动画之等待加载动画
  8. application详解
  9. 解决arcgis10.5直连postgresql报错
  10. 使用pyenv对python版本管理