MyBatis-Plus query PostgreSQL database jsonb type keeps the original format


foreword

In this article, we save the jsonb type of the database: MyBatis-Plus implements the storage and query of the PostgreSQL database jsonb type

This article introduces the fuzzy query json/jsonb type: PostgreSQL query json/jsonb whether there is a certain fragment

Before fuzzy querying json/jsonb, we have to get the correct json/jsonb fragment. For example, we fuzzy query several connected key-value pairs. If the order of the keys we get is disordered, we want to fuzzy at once If you query json/jsonb data, you will not be able to query the correct data.

The next part of this article will explain how to implement the key-value pair format returned to the front-end json/jsonb data in MyBatis-Plus to be consistent with the format of the database.


database

This has a set of data of type jsonb:

insert image description here

problem background

The backend returns the entity object

  • specialControl is jsonb type

The following code implements the data return interface and can fuzzily query the jsonb type

@GetMapping
@ResponseBody
public Object get(@RequestParam(value = "id", required = false) String id,
                  @RequestParam(value = "name", required = false) String name,
                  @RequestParam(value = "specialControl", required = false) String specialControl) {
    
    
    MPJQueryWrapper<Strategy> mpjQueryWrapper = new MPJQueryWrapper<>();
    mpjQueryWrapper.selectAll(Strategy.class)
            .eq(StringUtil.isNotEmpty(id), "t.id", id)
            .like(StringUtil.isNotEmpty(name), "t.name", name)
            .orderByDesc("t.create_time");
    if (StringUtil.isNotEmpty(specialControl)) {
    
    
        mpjQueryWrapper.like("t.special_control::text", specialControl.replace(":", ": ").replace(",", ", "));
    }
    return strategyService.selectJoinList(Strategy.class, mpjQueryWrapper);
}

front end

Serialize Json object and output

console.log(JSON.stringify(this.form.specialControl))

The output data is as follows:

insert image description here

It can be seen that contentthe order of the key-value pairs in the database is disrupted and inconsistent with that of the database.

accomplish

Backend returns List<Map<String, Object>>

The database column name special_control, the front-end data is in camelcase format, which needs to be aliasedspecial_control as \"specialControl\"

At this time, the jsonb data in the database will be serialized and returned to the front end, so the order of key-value pairs in jsonb will not be disturbed

  • Serialization: The process of converting an object into a sequence of bytes is called serialization of an object
  • Deserialization: The process of restoring a sequence of bytes to an object is called deserialization of an object
@GetMapping
@ResponseBody
public List<Map<String, Object>> get(@RequestParam(value = "id", required = false) String id,
                                     @RequestParam(value = "name", required = false) String name,
                                     @RequestParam(value = "specialControl", required = false) String specialControl) {
    
    
    MPJQueryWrapper<Strategy> mpjQueryWrapper = new MPJQueryWrapper<>();
    mpjQueryWrapper.select("t.id as id, t.name as name, t.special_control as \"specialControl\"")
            .eq(StringUtil.isNotEmpty(id), "t.id", id)
            .like(StringUtil.isNotEmpty(name), "t.name", name)
            .orderByDesc("t.create_time");
    if (StringUtil.isNotEmpty(specialControl)) {
    
    
        mpjQueryWrapper.like("t.special_control::text", specialControl.replace(":", ": ").replace(",", ", "));
    }
    return strategyService.listMaps(mpjQueryWrapper);
}

front end

if (this.form.specialControl != null) {
    
    
  // 反序列化
  this.form.specialControl = JSON.parse(this.form.specialControl)
  // 序列化并输出
  console.log(JSON.stringify(this.form.specialControl))
} else {
    
    
  this.form.specialControl = []
}

The output data is as follows:

insert image description here

Even if the front-end JSON.parse() serializes the bytes, and then deserializes the output through JSON.stringify(), you can see that the order will not be disturbed, which is consistent with the database.

Then we can directly get this string of data and the database for fuzzy query matching.

Guess you like

Origin blog.csdn.net/xiaohuihui1400/article/details/131997530