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. Python——最美丽的编程语言

    之前总听别人说Python有多厉害,有多好,我是不以为然的.但是当我开始接触Python的时候,我就知道它被誉为最美编程语言不是没有道理的.简短的代码,清晰明了的逻辑,方便快捷的编程工具让它注定会大放 ...

  2. 最短路问题---Dijkstra算法学习

    Dijkstra又称单源最短路算法,就从一个节点到其他各点的最短路,解决的是有向图的最短路问题 此算法的特点是:从起始点为中心点向外层层扩展,直到扩展到中终点为止. 该算法的条件是所给图的所有边的权值 ...

  3. Java开学测试

    这次开学测试要求做一个信息系统,该系统完成学生成绩录入,修改,计算学分积点和查询学生成绩的简单功能. 下面是我写的代码 //信1805-3班 20183641 赵树琪 package test; im ...

  4. springboot过滤器的实现

    springboot过滤器的实现 如下所示: import javax.servlet.*; import javax.servlet.annotation.WebFilter;import java ...

  5. 明明有class为什么还是报ClassNotFoundException?

    描述 我们修改接口时,习惯发布一个快照版本用于测试.我们的一个服务也是发布了快照版本,然后一个jar程序要依赖这个服务,修改pom文件打包部署后,通过 java -jar 命令执行这个jar程序,然后 ...

  6. 史上最详 Thymeleaf 使用教程

    前言 操作前建议先参考我的另一篇博客:玩转 SpringBoot 2 快速整合 | Thymeleaf 篇 查看如何在SpringBoot 中使用 Thymeleaf.还有一点需要注意的是:模版页面中 ...

  7. UGUI_关卡选项界面

    1.Image组件—“Source Image”,Set Native Size. 2.Image组件—“Image Type”——Sliced 编辑要放大缩小的图片,Sprite Editor,采用 ...

  8. 关于Oracle12c中无scott用户的问题

    我目前预习是通过视频,学到此处视频里的老师要登录scott用户,而我无法登陆,显示用户不存在,虽然在Oracle文件中也可以找到scott.sql文件,但经过网上教程创建用户后我觉得很麻烦而且没有成功 ...

  9. HBase shell scan 模糊查询

    0.进入hbase shell ./hbase shell help help "get" #查看单独的某个命令的帮助 1. 一般命令 status 查看状态 version 查看 ...

  10. eclipse查看.class文件

    要使用jd-gui.exe反编译程序 步骤:window-preferences-general-editors-file associations, 如下图 上面的框选中,*.class witho ...