引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
修改配置文件
//单机模式配置 spring.redis.host=172.16.7.21 //ip地址 spring.redis.port=6379 //端口号 spring.redis.database=1 //几号数据库 spring.redis.jedis.pool.max-active=100 //最大活跃数 spring.redis.jedis.pool.max-wait=1000 //超过最大值的等待时间 spring.redis.jedis.pool.max-idle=10 //休息时间到达后即销毁 //集群模式配置 spring.redis.cluster.nodes=172.16.7.21:7006,172.16.7.21:7001,172.16.7.21:7002,172.16.7.21:7003,172.16.7.21:7004,172.16.7.21:7005
使用(springboot整合redis时封装了两个工具类:springRedisTemplate和RedisTemplate)
StringRedisTemplate(springRedisTemplate是RedisTemplate的子类,只能存放字符串内容)
注入
@Autowired //注入StringRedisTemplate private StringRedisTemplate stringRedisTemplate;
hash操作
HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash(); //得到操作hash的类对象 hash.put("user", "name", "张三"); //存放单个数据 Map<String, String> hashMap = new HashMap<>(); hashMap.put("age","18"); hashMap.put("sex","男"); hash.putAll("user",hashMap); //存放多个数据 hash.get("user", "name"); //获取key为user的name的值 hash.entries("user"); //获取user的所有的键值对 hash.keys("user"); //获取key为user的所有的key的值 hash.values("user"); //获取key为user的所有的value的值
String操作
ValueOperations<String, String> val = stringRedisTemplate.opsForValue(); //得到操作字符串的类对象 val.set("k1","v1"); //存放数据 val.set("k2","200",2,TimeUnit.MINUTES); //存放数据设置过期时间为两分钟 val.setIfAbsent("k3","v3",2,TimeUnit.MINUTES); //如果没有key为k3的数据则存入,并设置两分钟过期时间 val.get("k1"); //获取key为k1的value val.increment("k2",20) //为key为k2的value递增20
key操作
stringRedisTemplate.keys("*"); //查看所有的key名 stringRedisTemplate.delete("k1"); //删除指定的key stringRedisTemplate.hasKey("k1"); //判断是否存在key为k1的数据 stringRedisTemplate.expire("k2", 100, TimeUnit.SECONDS); //为指定的key设置过期时间
RedisTemplate(属于StringRedisTemplate的父类,它的泛型默认都是Object。可以存储任意类型的key和value)
标签:hash,Springboot,val,spring,redis,整合,user,key From: https://blog.csdn.net/Baizeh/article/details/140671926编写config配置
@Configuration public class RedisConfig { @Bean public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String,Object> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setConnectionFactory(factory); // key序列化方式 template.setKeySerializer(redisSerializer); // value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); // value hashmap序列号 filed value template.setHashKeySerializer(redisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } }
使用
@Autowired private RedisTemplate redisTemplate; //注入 ValueOperations val = redisTemplate.opsForValue(); //获取类对象 val.set("k11","v11"); //存放数据 val.set("k12",new User("aaa",19)); //存放对象数据 val.get("k12"); //获取指定key的value HashOperations hash = redisTemplate.opsForHash(); //获取操作hash类对象 hash.put("b","a","哈哈哈"); //存放key为b的hashkey为a的val为哈哈哈的键值对