mysql's josn type is mapped to java is String

MySQL has a json type, and java receives it through String ;

1. Database query display:

Insert picture description here

2. java code testing

    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 corresponding database entity class

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));
    }
}

Insert picture description here
JSON.parseObject(this.soldierNum, LinkedHashMap.class, Feature.OrderedField) ensures that the read data order is consistent with the database storage order

4.Postman returns data display:

Insert picture description here

5. It should not be finished, there will be opportunities to add later;

Recently, I have been sitting for a lot of back pain, and I got off work first, everyone pay attention to your body!

Guess you like

Origin blog.csdn.net/weixin_42096620/article/details/110498135