1.pom文件
<!--Spring Boot -->
<!--支持 Web 应用开发,包含 Tomcat 和 spring-mvc。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置文件
#redis
spring.redis.hostName=127.0.0.1
spring.redis.port=6379
spring.redis.pool.maxActive=8
spring.redis.pool.maxWait=-1
spring.redis.pool.maxIdle=8
spring.redis.pool.minIdle=0
spring.redis.timeout=0
3.存取字符串
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void save(){
stringRedisTemplate.opsForValue().set("spingboot-redis","this is a test message");
}
执行后可在redis中查看
127.0.0.1:6379 > get springboot-redis
"this is a test message"
4.存取对象
class A 需要实现Serializable接口
package com.knife.test;
import java.io.Serializable;
public class A implements Serializable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "{\"name\":\""+name+"\",\"age\":"+age+"}";
}
public A(String name, int age) {
this.name = name;
this.age = age;
}
}
序列化class
package com.knife.test;
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 RedisObjectSerializer implements RedisSerializer<Object>{
private Converter<Object,byte[]> serializer = new SerializingConverter();
private Converter<byte[],Object> deserializer = new DeserializingConverter();
static final byte[] EMPTY_ARRAY = new byte[0];
public byte[] serialize(Object o) throws SerializationException {
if(o == null){
return EMPTY_ARRAY;
}
try {
return serializer.convert(o);
}catch (Exception e){
return EMPTY_ARRAY;
}
}
public Object deserialize(byte[] bytes) throws SerializationException {
if(isEmpty(bytes)){
return null;
}
try {
return deserializer.convert(bytes);
}catch (Exception e){
throw new SerializationException("Can not deserializer",e);
}
}
private boolean isEmpty(byte[] data){
return (data == null || data.length == 0);
}
}
redis配置class
package com.knife.test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory(){
return new JedisConnectionFactory();
}
/**
* 存入对象序列化信息
* @return
*/
@Bean
public RedisTemplate<String,Object> redisSerizlizerObj(){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new RedisObjectSerializer());
return redisTemplate;
}
}
测试
@Autowired
private RedisTemplate<String,A> rt;
@Test
public void saveobj(){
rt.opsForValue().set("classb", new A("wzj",29));
}
@Test
public void getobj(){
System.out.println(rt.opsForValue().get("classb"));
}
结果
127.0.0.1:6379>get classa
"\xac\xed\x00\x05sr\x00\x10com.knife.test.A\x19{\x8e\xc1`\xcc\xe4\xba\x02\x00\x02I\x00\x03ageL\x00\x04namet\x00\x12Ljava/lang/String;xp\x00\x00\x00\x1ct\x00\x03chq"

本文介绍如何在Spring Boot项目中整合Redis,包括pom文件配置、配置文件设置、字符串及对象的存取方法。通过具体代码示例展示了如何使用StringRedisTemplate进行字符串操作,以及如何自定义序列化方式存储复杂对象。
22万+

被折叠的 条评论
为什么被折叠?



