redis笔记2(千峰教育)
作者:互联网
四 redis连接数据库
1.下载依赖jedis
2.连接数据库, 和redis一样的操作命令, 关闭资源
// 连接Redis
Jedis jedis = new Jedis("192.168.43.30", 6379);
// 操作Redis, jedis的操作命令和redis完全一样
jedis.set("name","coco");
String name = jedis.get("name");
System.out.println(name);
// 释放资源
jedis.close();
3. 存对象.(对象要求k,v都必须是byte[])
1.定义的对象必须实现Serializable
@Data @AllArgsConstructor public class User implements Serializable { private int id; private String name; private Date brithday; }
2.将k,v都转成byte[]
添加依赖, 可以利用SerializationUtils
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.12.RELEASE</version> </dependency>
String key = "user"; User value = new User(1,"tony",new Date()); // 2,将key value转成byte[] byte[] key_b = key.getBytes(); byte[] value_b = SerializationUtils.serialize(value); // 3.存储 String set = jedis.set(key_b, value_b); // System.out.println(set); // 4.获取 byte[] bytes = jedis.get(key_b); User user = (User)SerializationUtils.deserialize(bytes); System.out.println(user);
标签:value,name,redis,笔记,jedis,User,key,byte,千峰 来源: https://www.cnblogs.com/dangdanghepingping/p/14285779.html