idea中新建spring boot项目,引入jedis依赖
//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的方法可以写的丰满些,以方便使用,此处仅简单举例