SpringBoot基本操作Redis

默认刚导入的redistemplate是不能存入对象的

得去加入配置类才可以

@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;

	}
}

并且由上面代码所示,对象必须实现序列化

同时自动注入的类也要改泛型

	@Autowired
	private RedisTemplate<String, Object> redistemplate;

存取hash类型

	public Map<String, Object> setRedis(String key){
		Map<String, Object> map = new HashMap<>();
		
		redistemplate.opsForHash().put("jedishash", "sola", "wow");
		
		map.put("data", redistemplate.opsForHash().get("jedishash", "sola"));
		
		return map;
	}

map里存list

	public Map<String, Object> setRedis(String key){
		Map<String, Object> map = new HashMap<>();
		
		Map<String, Object> maptest = null;
		
		List<Map<String, Object>> list = new LinkedList<>();
		
		for(int i = 1 ; i <=10 ; i++){
			
			maptest = new HashMap<>();
			
			maptest.put("name", i);
			
			maptest.put("age", i);
			
			list.add(maptest);
			
		}
		
		redistemplate.opsForHash().put("jedismaplist", "one", list);
		
		map.put("data", redistemplate.opsForHash().get("jedismaplist", "one"));
		
		return map;
	}

存取String类型

	public Map<String, Object> setRedis(String key){
		Map<String, Object> map = new HashMap<>();
		
		redistemplate.opsForValue().set("jedisString", "jedisStringValue");
		
		String string = redistemplate.opsForValue().get("jedisString");
		
		map.put("jedisString", string);
		
		return map;
	}

存储list类型(linkedlist)

public Map<String, Object> setRedis(String key){
		Map<String, Object> map = new HashMap<>();
		
		police policemode = null;
		
		int poilceno = 1;
		
		int pollceage = 1;
		
		for(;poilceno <= 10; poilceno++){
			
			policemode = new police();
			
			policemode.setNo(poilceno+"");
			
			policemode.setAge(pollceage+"");
			
			pollceage++;
			
			redistemplate.opsForList().rightPush("jedislist",policemode );
			
		}
		//查看所有
		map.put("data", redistemplate.opsForList().range("jedislist", 0, -1));
		
		return map;
	}

直接装整个集合也可以

public Map<String, Object> setRedis(String key){
		Map<String, Object> map = new HashMap<>();
		
		Map<String, Object> maptest = null;
		
		List<Map<String, Object>> list = new LinkedList<>();
		
		for(int i = 1 ; i <=10 ; i++){
			
			maptest = new HashMap<>();
			
			maptest.put("name", i);
			
			maptest.put("age", i);
			
			list.add(maptest);
			
		}
		
		redistemplate.opsForList().rightPushAll("jedislist", list);
		
		map.put("data",redistemplate.opsForList().range("jedislist", 0, -1) );
		
		return map;
	}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot 2.7中,可以通过以下步骤来整合Redis: 1. 添加Redis依赖:在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接信息:在application.properties或application.yml文件中配置Redis的连接信息,例如: ```yaml spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= ``` 3. 创建Redis配置类:创建一个Redis配置类,用于配置Redis连接工厂和Redis模板。可以使用以下示例代码: ```java @Configuration public class RedisConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(); connectionFactory.setHostName("127.0.0.1"); connectionFactory.setPort(6379); return connectionFactory; } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } } ``` 4. 使用Redis:在需要使用Redis的地方,可以通过注入RedisTemplate来操作Redis。例如,以下代码演示了如何向Redis中存储和获取数据: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; public void setValue(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object getValue(String key) { return redisTemplate.opsForValue().get(key); } ``` 以上是SpringBoot 2.7中整合Redis的基本步骤。你可以根据自己的需求进行进一步的配置和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值