记录springboot 整合redis
一.pom.xml文件导入依赖包
通过jedis.get(key)和jedis.set(key,value)获取和插入redis缓存信息。
1.jedis依赖包
redis.clients
jedis
2.redis依赖包
redis.clients
jedis
二.application.properties配置redis信息
#redis
#Redis服务器地址
redis.host=127.0.0.1
#Redis端口号
redis.port=6379
#连接超时时间(毫秒)
redis.timeout=3
#密码 默认为空
redis.password=
#线程池最大连接数
redis.poolMaxTotal=10
#线程池最大空闲链接
redis.poolMaxIdle=10
#线程池最大等待时间
redis.poolMaxWait=3
三.配置Redis文件,注意RedisConfig类里面的属性名和application.properties属性名相对应。@Component作用:把普通pojo实例化到spring容器中。@ConfigurationProperties(prefix = “redis”)作用:读取application.properties配置文件,prefix = “redis”:获取redis下的属性名。RedisConfig类中set,get方法自己补上。。。
@Component
@ConfigurationProperties(prefix = “redis”)
public class RedisConfig {
private String host;
private int port;
private int timeout;
private int database;
private String password;
private int poolMaxTotal;
private int poolMaxIdle;
private int poolMaxWait;
}
四.通过jedisPool.getResource()获取jedis,编写返回jedisPool的方法类。新建RedisPoolFactory.class。 @Bean注解,将JedisPool实例化到spring容器中。注意redisConfig是毫秒数,*1000.
@Service
public class RedisPoolFactory {
@Autowired
RedisConfig redisConfig;
@Bean
public JedisPool JedisPoolFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
jedisPoolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
jedisPoolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000);
JedisPool jedisPool = new JedisPool(jedisPoolConfig,redisConfig.getHost(),redisConfig.getPort(),
redisConfig.getTimeout()*1000,redisConfig.getPassword(),redisConfig.getDatabase());
return jedisPool;
}
}
五.新建RedisService.class。获取set和get方法供controller调用。stringToBean()将spring转换成bean。beanToString()将bean转换成spring。
@Service
public class RedisService {
@Autowired
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);
}
}
public <T> boolean set(String key,T value){
Jedis jedis =null;
try {
jedis= jedisPool.getResource();
System.out.println(jedis);
String str = beanToString(value);
if(str ==null || str.length()<=0){
return false;
}
jedis.set(key,str);
return true;
} finally {
returnToPool(jedis);
}
}
public static <T> String beanToString(T value) {
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);
}
}
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);
}
}
private void returnToPool(Jedis jedis) {
if(jedis !=null){}
jedis.close();
}
}
六.新建RedisController类。
public class RedisController {
@Autowired
private RedisService redisService;
@RequestMapping("redisGet")
@ResponseBody
public ServerResponse<String> redisGet(){
String v1 = redisService.get("key2",String.class);
return ServerResponse.createBySuccess(v1);
}
@RequestMapping("redisSet")
@ResponseBody
public ServerResponse<String> redisSet(){
redisService.set("key2","你是谁啊");
String v1 = redisService.get("key2",String.class);
return ServerResponse.createBySuccess(v1);
}
}
出现的问题:启动项目发现空指针错误,检查发现是application.properties redis.password为空了,如果你的redis密码为空,把此行代码注释掉再次运行就OK了。