mysql的josn类型映射到java是String
程序员文章站
2022-09-29 21:59:16
mysql有json类型,java通过String接收;1.数据库查询显示:2. java代码测试 public static void main(String[] args) { // LinkedHashMap保证遍历的时候是存储顺序; Map soldierNum = new LinkedHashMap<>(); soldierNum.put("18-40岁", 77);...
mysql有json类型,java通过String接收;
1.数据库查询显示:
2. java代码测试
public static void main(String[] args) {
// LinkedHashMap保证遍历的时候是存储顺序;
Map<String, Integer> soldierNum = new LinkedHashMap<>();
soldierNum.put("18-40岁", 77);
soldierNum.put("41-65岁", 130);
soldierNum.put("65岁以上", 146);
String string = soldierNum.toString();
System.out.println(string);
// 遍历集合
Set<Map.Entry<String, Integer>> entries = soldierNum.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
System.out.println("key= " + entry.getKey() + " and value= "
+ entry.getValue());
}
// map -> json
String s = JSON.toJSONString(soldierNum);
System.out.println(s);
}
3.java对应数据库实体类
public class ExcelElement implements Serializable {
private Long id;
// TODO 记得自动将Sting -> map
@Excel(name = "军人数量")
private String soldierNum;
private String orgRelationship;
private Map<String, Object> soldierNumMap = new LinkedHashMap<>();
private Map<String, Object> orgRelationshipMap = new LinkedHashMap<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSoldierNum() {
return soldierNum;
}
public void setSoldierNum(String soldierNum) {
this.soldierNum = soldierNum;
// mybatis映射java对象的时候调用set方法,所以在这里我直接解析了json -> map
this.setSoldierNumMap(JSON.parseObject(this.soldierNum, LinkedHashMap.class, Feature.OrderedField));
}
}
JSON.parseObject(this.soldierNum, LinkedHashMap.class, Feature.OrderedField)保证了读取数据顺序和数据库存储顺序一致
4.postman返回数据展示:
5.应该没写完,以后有机会补充;
最近坐多了腰疼,先下班了,大家注意身体哈!
本文地址:https://blog.csdn.net/weixin_42096620/article/details/110498135
上一篇: 大二 我是如何轻松月赚8000的
下一篇: 浅谈草根博客如何获得稳定的关键词排名?