1.整合redis
1.添加依赖
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
2.在application.yml中添加配置
spring:
redis:
###数据库索引(默认0
database: 0
host: 127.0.0.1
port: 6379
password:
jedis:
pool:
###最大连接数(负数没有限制
max-active: 8
###连接池最大阻塞等待时间(负数没有限制
max-wait: -1
###最大空闲连接
max-idle: 5
###最小空闲连接
min-idle: 0
###连接超时时间
timeout: 10000
本地host大多是127.0.0.1可以进入redis操作的时候左边显示
最左边 host:port> a
3.添加类RedisConfig
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.StringRedisSerializer;
import java.io.Serializable;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Serializable, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Serializable, Object> template = new RedisTemplate<Serializable, Object>();
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
// redis存取对象的关键配置
template.setKeySerializer(new StringRedisSerializer());
// ObjectRedisSerializer类为java对象的序列化和反序列化工具类
template.setValueSerializer(new ObjectRedisSerializer());
return template;
}
}
4.创建类ObjectRedisSerializer
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
public class ObjectRedisSerializer implements RedisSerializer<Object> {
/**
* 定义序列化和反序列化转化类
*/
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
/**
* 定义转换空字节数组
*/
private static final byte[] EMPTY_ARRAY = new byte[0];
@Override
public byte[] serialize(Object obj) throws SerializationException {
byte[] byteArray = null;
if (null == obj) {
System.err.println("----------------------------->:Redis待序列化的对象为空.");
byteArray = EMPTY_ARRAY;
} else {
try {
byteArray = serializer.convert(obj);
} catch (Exception e) {
System.err.println("----------------------------->Redis序列化对象失败,异常:"+e.getMessage());
byteArray = EMPTY_ARRAY;
}
}
return byteArray;
}
@Override
public Object deserialize(byte[] datas) throws SerializationException {
Object obj = null;
if((null == datas)|| (datas.length == 0)){
System.out.println("---------------------------------->Redis待反序列化的对象为空.");
}else{
try {
obj = deserializer.convert(datas);
} catch (Exception e) {
System.out.println("------------------------------------->Redis反序列化对象失败,异常:"+e.getMessage());
}
}
return obj;
}
}
5.添加测试TestController
import com.example.test.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.Serializable;
@RestController
public class TestController {
@Autowired
private RedisTemplate<Serializable, Object> redisTemplate;
@RequestMapping("/set")
public String setPOJO(){
User user = new User();
user.setAge("18");
user.setGender("nv");
user.setNickname("cherish");
user.setPassword("12345");
user.setUsername("admin");
redisTemplate.opsForValue().set("user2", user);
return "lihaolihao";
}
@RequestMapping("/get")
public Object getPOJO(){
return redisTemplate.opsForValue().get("user2");
}
}
尝试set和get操作。