正文:在网上看到很多教程帖,感觉说的很乱,不完整,后来又去把狂神的redis课又复看了一遍,记下下面的笔记,强推狂神,讲的是真的好
1 首先下载redis win版(这个教程太多了 我就不说了)(linux版在我另一篇博客也有)
启动方法:
redis-server.exe redis.windows.conf
2 下载管理redis的客户端(可装可不装)
3 springboot的maven依赖导入:
org.springframework.boot spring-boot-starter-data-redis
4 配置文件(properities)
spring.redis.host=127.0.0.1 spring.redis.port=6379
5 然后就可以直接测试源码的redisTemplate了
package com.ldu.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ldu.demo.model.entity.pojo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test(){
redisTemplate.opsForValue().set("key3", "呵呵");
System.out.println(redisTemplate.opsForValue().get("key3"));
}
}
ps:这里还没有用到重写redisTemplate和工具类,这里用到源码的也是可以直接用的,因为这里没有用到复杂对象等,没有考虑到序列化的问题
补充:
6 这时如果你想存储实体类对象 且实体类没有序列化的话,如果用源码的redisTemplate是报错的。
解决办法:
1 实体类继承Serializable,实现序列化
2 重写redisTemplate 重新注入bean容器当中 实现序列化 而不是用底层jdk序列化
package com.ldu.demo.model.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class pojo implements Serializable {
private String name;
private String value;
}
package com.ldu.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ldu.demo.model.entity.pojo;
import com.ldu.demo.utils.RedisUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
//在这里加上泛型,可以直接找到咱重写的那个地方
//其实不加也可以 因为底层源码里写的是如果bean容器没有redisTemplate的话才会加载默认的,所以只要配置类注入了重写bean spring就会加载我们新写的了
@Test
public void test(){
//redisTemplate.opsForValue().set("key3", "呵呵");
// redisTemplate.opsForValue().set("key2","guanzhu");
//System.out.println(redisTemplate.opsForValue().get("key2"));
redisTemplate.opsForValue().set("pojo",new pojo("gx","ss"));
redisTemplate.opsForValue().get("pojo");
}
}
还有一个办法就是手动转化为json序列化字符串,也可以直接存储
String jsonUser=new ObjectMapper().writevalueAsString() 序列化对象为json
7 工具类实现:
package com.ldu.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ldu.demo.model.entity.pojo;
import com.ldu.demo.utils.RedisUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RedisUtils redisUtils;
@Test
public void test(){
//redisTemplate.opsForValue().set("key3", "呵呵");
// redisTemplate.opsForValue().set("key2","guanzhu");
//System.out.println(redisTemplate.opsForValue().get("key2"));
//String jsonUser=new ObjectMapper().writevalueAsString() 序列化对象为json
//redisTemplate.opsForValue().set("pojo",new pojo("gx","ss"));
//redisTemplate.opsForValue().get("pojo");
redisUtils.set("gg","帅");
System.out.println(redisUtils.get("gg"));
}
}
附自定义redisTemplate和工具类:
redisTemplate
package com.ldu.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import java.net.UnknownHostException;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
// 将template 泛型设置为
RedisTemplate template = new RedisTemplate();
// 连接工厂,不必修改
template.setConnectionFactory(redisConnectionFactory);
// key、hash的key 采用 String序列化方式
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
// value、hash的value 采用 Jackson 序列化方式
template.setValueSerializer(RedisSerializer.json());
template.setHashValueSerializer(RedisSerializer.json());
template.afterPropertiesSet();
return template;
}
}
工具类:
package com.ldu.demo.utils;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@Component
public final class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
// =============================common============================
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete((Collection) CollectionUtils.arrayToList(key));
}
}
}
// ============================String=============================
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
// ================================Map=================================
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
public Map 


