1. 程式人生 > >java鬼混筆記:springboot之redis儲存物件

java鬼混筆記:springboot之redis儲存物件

首先加入springboot-redis依賴:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

springboot-date-redis提供了一個StringRedisTemplate,儲存的物件是String-String,如
@Autowired
private StringRedisTemplate template;

@Test
public void testDemo() {

	template.opsForValue().set("name", "ywj");
	System.out.println(template.opsForValue().get("name").toString());
	Assert.assertEquals("ywj", template.opsForValue().get("name").toString());
}

也提供了一個RedisTemplate<K,V>,不過不能正常儲存物件。。。根據Google得知儲存物件的方法,
那就是自己搞一個RedisTemplate,根據springboot-data-redis提供的幾種序列方法自定義一個,參考StringRedisTemplate,

這個是StringRedisTemplate的原始碼:
public class StringRedisTemplate extends RedisTemplate<String, String> {

	/**
	 * Constructs a new <code>StringRedisTemplate</code> instance. {@link #setConnectionFactory(RedisConnectionFactory)}
	 * and {@link #afterPropertiesSet()} still need to be called.
	 */
	public StringRedisTemplate() {
		RedisSerializer<String> stringSerializer = new StringRedisSerializer();
		setKeySerializer(stringSerializer);
		setValueSerializer(stringSerializer);
		setHashKeySerializer(stringSerializer);
		setHashValueSerializer(stringSerializer);
} /** * Constructs a new <code>StringRedisTemplate</code> instance ready to be used. * * @param connectionFactory connection factory for creating new connections */ public StringRedisTemplate(RedisConnectionFactory connectionFactory) { this(); setConnectionFactory(connectionFactory); afterPropertiesSet(); } protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) { return new DefaultStringRedisConnection(connection); } }
所以接下來自己也搞一個。


RedisConfig.java

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.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {


RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();


template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();


return template;


}
}

測試:
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Thymeleaf0Application.class)
public class TTest {


	@Autowired
	private RedisTemplate<String, Object> template;

	@Test
	public void testDemo() {

		User u = new User();
		u.setUsername("abc");
		template.opsForValue().set("u", u);
		
		User u2 = (User) template.opsForValue().get("u");
		System.out.println(u2.getUsername()+".................");
		
		template.opsForValue().set("name", "ywj");
		System.out.println(template.opsForValue().get("name").toString());
		Assert.assertEquals("ywj", template.opsForValue().get("name").toString());
	}
}



OK。。。正常使用。

注意:自己建立的物件要 實現序列化介面java.io.Serializable,不然無法儲存。