redis 获得hash存储格式中所有key值

不BB直接上代码

老夫用
redisTemplate
stringRedisTemplate
来操控redis

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
public class WarnController {
//这里控制自己想要的关键字,因为获得所有,所以是“*”
	public static final String prefix = "*";
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	public ResponseEntity<List<WarnBean>> getWarnList() {//这里放你自己的bean
		Map<Object, Object> resultMap;
		Set<String> keys = stringRedisTemplate.keys(prefix);
		Iterator<String> it1 = keys.iterator();
		System.out.println("toString: " + it1.toString());
		while (it1.hasNext()) {
			resultMap =	redisTemplate.opsForHash().entries(it1.next());
			System.out.println("isEmpty :  " + resultMap.isEmpty());
			for (Entry<Object, Object> entry : resultMap.entrySet()) {
			//这里可以写自己的操作
				System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue() + ";");
			}
		}
		return null;
	}

}

因为我是直接把controller中的代码复制过来改的,有些瑕疵大家不要在意

猜你喜欢

转载自blog.csdn.net/pz641/article/details/102602605