JSON解析器:封装好的JSON工具类,节省代码编写
JSON转为Java对象
Java对象转换JSON
步骤:
转换方式:
writeValue(参数1,obj对象)
参数1有很多重载形式:
代码:
Person p = new Person();
p.setName("dyg");
p.setAge(23);
p.setGender("男");
// 2.创建Jackson的核心对象 ObjectMapper
ObjectMapper mapper = new ObjectMapper();
// 3.转换
String json = mapper.writeValueAsString(p);
System.out.println(json);
// 写入文件
mapper.writeValue(new File("./a.txt"),p);
// 关联到writer中
mapper.writeValue(new FileWriter("./b.txt"),p);
注解:
@JsonIgnore:排除属性(加在对象类的属性上)
@JsonIgnore
private Date birthday;
@JsonFotmat:属性值格式化
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;
复杂的java对象如果转json
步骤:
readValue(json字符串数据,Class)
// 1.
String json = "{\"gender\":true,\"name\":\"zs\",\"age\":23}";
// 2.
ObjectMapper mapper = new ObjectMapper();
// 3.
Person person = mapper.readValue(json, Person.class);
System.out.println(person);
redis.conf:配置文件
运行命令:
# 运行服务器端
$ redis-server
# 连接redis
$ redis-cli -p 6379 -h 127.0.0.1
# 存值
$ set name "Parzival"
# 取值
$ get name
redis是一个内存数据库,当redis服务器重启数据会丢失,我们可以将redis内存中的数据持久化保存到内存中。
持久化机制:
RDB:默认方式,不需要配置默认使用这种机制
在一定的间隔时间中,检测key变化情况,然后去持久化数据
较为推荐,对性能影响比较低
使用方式:
编辑redis.conf配置文件
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
save 900 1
# after 300 sec (5 min) if at least 10 keys changed
save 300 10
# after 60 sec if at least 10000 keys changed
save 60 10000
save 10 5
重新启动服务器,并指定配置文件
$ redis-server redis.conf
AOF:日志记录方式,可以记录每一条命令的操作。可以每一次命令操作后,持久化数据
使用步骤:
打开配置文件,找到appendonly ,默认值为no ,改为yes
# appendfsync always : 每一次操作都进行持久化
appendfsync everysec : 每间一秒操作都进行持久化(默认)
# appendfsync no : 不进行持久化
重新启动服务器,并指定配置文件