一、在pom.xml文件中添加Jedis的依賴
<!--集成Redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
二、springboot配置文件application.properties中的redis配置
#Redis
redis.DB=6
redis.host=127.0.0.1
redis.port=6380
redis.password=
redis.timeout=10000
redis.max-active=200
redis.max-idle=10
redis.min-idle=10
redis.max-wait=10000
三、創建一個生成JedisPool的工廠,注意加上@Configuration註解,否則jedisPool爲空,
package com.niucheng.eblog.common.redis;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* 創建一個生成JedisPool的工廠
*/
@Configuration
public class JedisPoolFactory {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private int port;
@Value("${redis.password}")
private String password;
@Value("${redis.timeout}")
private int timeout;
@Value("${redis.max-active}")
private int maxActive;
@Value("${redis.max-idle}")
private int maxIdle;
@Value("${redis.min-idle}")
private int minIdle;
@Value("${redis.max-wait}")
private long maxWaitMillis;
@Bean
public JedisPool generateJedisPoolFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxActive);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxWaitMillis(maxWaitMillis);
//指定第6個庫
JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout, password, 6);
return jedisPool;
}
}
四:編寫工具類,加上@Component註解不能丟,否則jedisPool爲空,
package com.niucheng.eblog.common.redis;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
*編寫工具類,這裏的@Component註解不能丟,否則jedisPool爲空,
*/
@Component
public class RedisUtil {
@Autowired
private JedisPool jedisPool;
/**
* 獲取單個對象
*/
public <T> T get(String key, Class<T> clazz) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = jedis.get(key);
T t = stringToBean(str, clazz);
return t;
} finally {
returnToPool(jedis);
}
}
/**
* 得到對應鍵的字符串值
* @param key
* @return
*/
public String get(String key) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.get(key);
} finally {
returnToPool(jedis);
}
}
/**
* 選擇從哪個redis庫中,獲取對象信息
*/
public <T> T get(int dbNum, String key, Class<T> clazz) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(dbNum);
String str = jedis.get(key);
T t = stringToBean(str, clazz);
return t;
} finally {
returnToPool(jedis);
}
}
/**
* 設置對象
*/
public <T> boolean set(String key, T value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = beanToString(value);
if (str == null || str.length() <= 0) {
return false;
}
jedis.set(key, str);
return true;
} finally {
returnToPool(jedis);
}
}
/**
* 選擇redis庫,並寫入數據
*/
public <T> boolean set(int dbNum, String key, T value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = beanToString(value);
if (str == null || str.length() <= 0) {
return false;
}
jedis.select(dbNum);
jedis.set(key, str);
return true;
} finally {
returnToPool(jedis);
}
}
/**
* 向緩存中寫入值,並設置失效時間
**/
public <T> boolean setex(String key, T value, int seconds) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = beanToString(value);
if (str == null || str.length() <= 0) {
return false;
}
jedis.setex(key, seconds, str);
return true;
} finally {
returnToPool(jedis);
}
}
/**
* 選擇緩存庫,向緩存中寫入值,並設置失效時間
**/
public <T> boolean setex(int dbNum, String key, T value, int seconds) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = beanToString(value);
if (str == null || str.length() <= 0) {
return false;
}
jedis.select(dbNum);
jedis.setex(key, seconds, str);
return true;
} finally {
returnToPool(jedis);
}
}
/**
* 判斷key是否存在
*/
public <T> boolean exists(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.exists(key);
} finally {
returnToPool(jedis);
}
}
/**
* 刪除
*/
public boolean delete(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
long ret = jedis.del(key);
return ret > 0;
} finally {
returnToPool(jedis);
}
}
/**
* 刪除指定庫中單值
*/
public boolean delete(String key,int database) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(database);
long ret = jedis.del(key);
return ret > 0;
} finally {
returnToPool(jedis);
}
}
/**
* 刪除一組數據
*/
public boolean deletes(String[] keys) {
boolean deletes = true;
for (String key : keys) {
boolean delete = delete(key);
if (!delete) {
deletes = false;
}
}
return deletes;
}
/**
* 修改數據
*/
public boolean update(String key, String value) {
Jedis jedis = null;
if (jedis.exists(key)) {
jedis.set(key, value);
if (value.equals(jedis.get(key))) {
System.out.println("修改數據成功");
return true;
} else {
System.out.println("修改數據失敗");
return false;
}
} else {
System.out.println(key + "不存在");
System.out.println("若要新增數據請使用set()方法");
return false;
}
}
/**
* 關閉jedis
* @param jedis
*/
private void returnToPool(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
@SuppressWarnings("unchecked")
public static <T> T stringToBean(String str, Class<T> clazz) {
if (str == null || str.length() <= 0 || clazz == null) {
return null;
}
if (clazz == int.class || clazz == Integer.class) {
return (T) Integer.valueOf(str);
} else if (clazz == String.class) {
return (T) str;
} else if (clazz == long.class || clazz == Long.class) {
return (T) Long.valueOf(str);
} else {
return JSON.toJavaObject(JSON.parseObject(str), clazz);
}
}
public static <T> String beanToString(T value) {
if (value == null) {
return null;
}
Class<?> clazz = value.getClass();
if (clazz == int.class || clazz == Integer.class) {
return "" + value;
} else if (clazz == String.class) {
return (String) value;
} else if (clazz == long.class || clazz == Long.class) {
return "" + value;
} else {
return JSON.toJSONString(value);
}
}
}
五、在springboot啓動類加上@ComponentScan(basePackages = {"com.niucheng"}),一定要把所有的包掃描上
package com.niucheng.eblog;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = {"com.niucheng"})
// 添加對mapper包掃描
@MapperScan("com.niucheng.eblog.mapper")
@SpringBootApplication
public class EblogApplication {
public static void main(String[] args) {
SpringApplication.run(EblogApplication.class, args);
}
}
六、測試
輸出結果: