idea中新建spring boot项目,引入jedis依赖

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>

application.properties中自定义redis配置

#自定义redis配置
myredis.host=localhost
myredis.port=6379
myredis.timeout=10
myredis.pool-max-total=1000
myredis.pool-max-idle=500
myredis.pool-max-wait=500

自定义配置类MyRedisConfig.java读取redis配置文件信息,生成JedisPool类型的bean

package com.example.jedisredis.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; @Configuration
@ConfigurationProperties(prefix = "myredis")
public class MyRedisConfig { private String host;
private Integer port;
private Integer timeout;
private Integer poolMaxTotal;
private Integer poolMaxIdle;
private Integer poolMaxWait; @Bean
public JedisPool jedisPoolFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(poolMaxTotal);
jedisPoolConfig.setMaxIdle(poolMaxIdle);
jedisPoolConfig.setMaxWaitMillis(poolMaxWait * 1000);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout*1000);
return jedisPool;
} @Override
public String toString() {
return "MyRedisConfig{" +
"host='" + host + '\'' +
", port=" + port +
", timeout=" + timeout +
", poolMaxTotal=" + poolMaxTotal +
", poolMaxIdle=" + poolMaxIdle +
", poolMaxWait=" + poolMaxWait +
'}';
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public Integer getPort() {
return port;
} public void setPort(Integer port) {
this.port = port;
} public Integer getTimeout() {
return timeout;
} public void setTimeout(Integer timeout) {
this.timeout = timeout;
} public Integer getPoolMaxTotal() {
return poolMaxTotal;
} public void setPoolMaxTotal(Integer poolMaxTotal) {
this.poolMaxTotal = poolMaxTotal;
} public Integer getPoolMaxIdle() {
return poolMaxIdle;
} public void setPoolMaxIdle(Integer poolMaxIdle) {
this.poolMaxIdle = poolMaxIdle;
} public Integer getPoolMaxWait() {
return poolMaxWait;
} public void setPoolMaxWait(Integer poolMaxWait) {
this.poolMaxWait = poolMaxWait;
}
}

自定义工具类MyReidsUtil.java,注入MyReidsConfig.java中的JedisPool,通过JedisPool生成Jedis,从而构建操作redis的方法

package com.example.jedisredis.util;

import com.example.jedisredis.config.MyRedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; @Component
public class MyRedisUtil { @Autowired
JedisPool jedisPool; public String get(String key){
Jedis jedis = jedisPool.getResource();
String value = jedis.get(key);
return value;
} public void set(String key, String value){
Jedis jedis = jedisPool.getResource();
jedis.set(key, value);
}
}

测试

package com.example.jedisredis.controller;

import com.example.jedisredis.util.MyRedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/user")
public class UserController { @Autowired
MyRedisUtil myRedisUtil; @RequestMapping("/setname")
@ResponseBody
public String setName(){
myRedisUtil.set("name", "yanguobin");
return "setName";
} @RequestMapping("/getname")
@ResponseBody
public String getName(){
String name = myRedisUtil.get("name");
System.out.println("name:" + name);
return "getName";
}
}

最后,MyRedisUtil.java中的各种自定义的操作redis的方法可以写的丰满些,以方便使用,此处仅简单举例

spring boot通过Jedis来操作redis的更多相关文章

  1. Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程

    Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...

  2. 阿里P7级教你如何在Spring Boot应用程序中使用Redis

    在Spring Boot应用程序中使用Redis缓存的步骤: 1.要获得Redis连接,我们可以使用Lettuce或Jedis客户端库,Spring Boot 2.0启动程序spring-boot-s ...

  3. Spring Boot(二):数据库操作

    本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...

  4. Jedis API操作redis数据库

    1.配置文件 classpath路径下,新建redis.properties配置文件 配置文件内容 # Redis settings redis.host=127.0.0.1 redis.port=6 ...

  5. Spring Boot 1.5.4集成Redis

    本文示例源码,请看这里: 如何安装与配置Redis,请看这里 首先添加起步依赖: <dependency> <groupId>org.springframework.boot& ...

  6. 【Spring Boot&&Spring Cloud系列】Spring Boot中使用NoSql数据库Redis

    github地址:https://github.com/AndyFlower/Spring-Boot-Learn/tree/master/spring-boot-nosql-redis 一.加入依赖到 ...

  7. Spring Boot (五): Redis缓存使用姿势盘点

    1. Redis 简介 Redis 是目前业界使用最广泛的内存数据存储.相比 Memcached,Redis 支持更丰富的数据结构,例如 hashes, lists, sets 等,同时支持数据持久化 ...

  8. spring boot下JedisCluster方式连接Redis集群的配置

    最近在使用springboot做项目,使用redis做缓存.在外网开发的时候redis服务器没有使用集群配置,所有就是用了RedisTemplate的方式进行连接redis服务器.但是项目代码挪到内网 ...

  9. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

随机推荐

  1. poj 3159 Candies(dijstra优化非vector写法)

    题目链接:http://poj.org/problem?id=3159 题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的 ...

  2. Kafka笔记—可靠性、幂等性和事务

    这几天很忙,但是我现在给我的要求是一周至少要出一篇文章,所以先拿这篇笔记来做开胃菜,源码分析估计明后两天应该能写一篇.给自己加油~,即使没什么人看. 可靠性 如何保证消息不丢失 Kafka只对&quo ...

  3. 201871010134-周英杰《面向对象程序设计(java)》第一周学习总结

    项目 内容 java https://www.cnblogs.com/nwnu-daizh/ 任课老师博客下 https://www.cnblogs.com/nwnu-daizh/p/11435127 ...

  4. HTTPS页面使用CNZZ统计代码,Chrome显示警告怎么办?

    很多站长会遇到一个问题,网站加入CNZZ的JS统计代码后,Chrome浏览器出现警告:阻止跨站解析器阻断脚本通过document.write调用(A parser-blocking, cross si ...

  5. cssrelative

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. java架构之路-(源码)mybatis执行流程源码解析

    这次我们来说说Mybatis的源码,这里只说执行的流程,内部细节太多了,这里只能授之以渔了.还是最近的那段代码,我们来回顾一下. package mybatis; import mybatis.bea ...

  7. 【学习笔记】第六章 python核心技术与实践--深入浅出字符串

    [第五章]思考题答案,仅供参考: 思考题1:第一种方法更快,原因就是{}不需要去调用相关的函数: 思考题2:用列表作为key在这里是不被允许的,因为列表是一个动态变化的数据结构,字典当中的key要求是 ...

  8. 第二场周赛(递归递推个人Rank赛)——题解

    很高兴给大家出题,本次难度低于上一场,新生的六个题都可以直接裸递归式或者裸递推式解决,对于老生的汉诺塔3,需要找出一般式,后两题分别为裸ST算法(或线段树)/线性DP. 正确的难度顺序为 种花 角谷定 ...

  9. 一个基于vue的仪表盘demo

    最近写了一个基于vue的仪表盘,其中 主要是和 transform 相关的 css 用的比较多.给大家分享一下,喜欢的话点个赞呗?嘿嘿 截图如下: 实际效果查看地址:https://jhcan333. ...

  10. 会计的疑惑--BigDecimal的秘密

    为了提供公司的财务信息化,公司A上线了一套自主研发的财务系统,上班第一天,财务C姐就发现了情况不对:几项支出都对,但支出总和一直为0,赶紧向大老板报告.大老板勃然大怒,责令技术部门今天必须解决,小B负 ...