Postgresql's jsonb format always says it cannot be parsed when doing mapping

Postgresql's jsonb format always says it cannot be parsed when doing mapping

When using jsonb, first write an entity class about jsonb in Java, and then write a parsed JsonbTypeHandler.

1. Write the jsonb entity class of attr, and write the number getter and setter.

2. Write a processor JsonbTypeHandler that parses into json format

package com.ruoyi.web.config.pg;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.postgresql.util.PGobject;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JsonbTypeHandler<T extends Object> extends BaseTypeHandler<T> {
    
    
    private static final PGobject jsonObject = new PGobject();
    private Class<T> clazz;

    public JsonbTypeHandler(Class<T> clazz) {
    
    
        if (clazz == null){
    
    
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.clazz = clazz;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    
    
        jsonObject.setType("jsonb");
        jsonObject.setValue(this.toJson(parameter));
        ps.setObject(i, jsonObject);
    }

    @Override
    public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
    
    
        return this.toObject(rs.getString(columnName), clazz);
    }

    @Override
    public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    
    
        return this.toObject(rs.getString(columnIndex), clazz);
    }

    @Override
    public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    
    
        return this.toObject(cs.getString(columnIndex), clazz);
    }

    private String toJson(T object) {
    
    
        try {
    
    
            return JSON.toJSONString(object);
        } catch (Exception e) {
    
    
            throw new RuntimeException(e);
        }
    }

    private T toObject(String content, Class<?> clazz) {
    
    
        if (content != null && !content.isEmpty()) {
    
    
            try {
    
    
                return (T) JSON.parseObject(content,clazz);
            } catch (Exception e) {
    
    
                throw new RuntimeException(e);
            }
        } else {
    
    
            return null;
        }
    }
}

3. Add the parameter in the jsonb format of the result in the mapper

<result property="Attr"    column="attr"    jdbcType="OTHER" typeHandler="com.xxx.xxx.xxxxx.xx.JsonbTypeHandler"/>

4. When inserting and modifying jsonb, write in mybatis

 <if test="Attr != null">#{Attr, jdbcType=OTHER, typeHandler=com.xxx.xxx.xxxxx.xx.JsonbTypeHandler},</if>

5. If all these are done well, there is basically no problem in the background. If there is still a problem, it is generally a problem with the data sent from the front end. ,

Then if the front-end transmits data, let the front-end generate json format data for us, instead of writing json data by ourselves, which cannot be recognized as json, but a string. So it will not be parsed when it is passed to the background.

The front end also corresponds to the Attr entity class and attributes of the back end.

<el-form-item label="xxx" prop="Attr">
          <el-input v-model="form.Attr.a" placeholder="xxx" />
          <el-input v-model="form.Attr.b" placeholder="xxx" />
        </el-form-item>

The format written in this way will be unified into json format when sending the request.

Guess you like

Origin blog.csdn.net/qq_44627822/article/details/128398979