postgreSql存储jsonb

postgreSQL 支持 json类型存储
使用异同如下:

1. 字段修改

1.1 修改前

字符串类型

 private String files;
 -- 数据库
 files varchar(511),
1.2 修改后

jsonArray 类型

@TableField(typeHandler = JsonArrayTypeHandler.class)
 private JSONArray files;
 -- 数据库
 files jsonb,

2. 查询修改

2.1 修改前

使用mybatisPlus 默认查询

Map map = new HashMap(CommonConstant.TWO);
map.put(CommonConstant.FBR_INFO_ID, fbrInfoId);
List<FBRHWInfoEntity> hwList = fbrhwInfoMapper.selectByMap(map);
2.2 修改后

需要自己写查询的mapper方法

List<FBRHWInfoEntity> hwList = fbrhwInfoMapper.getByFbrInfoId(fbrInfoId); 

mapper中 resultMap 的其余字段不用写

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.npi.module.fbr.mapper.FBRHWInfoMapper">
    <resultMap id="CurrentType" type="com.xxx.npi.module.fbr.po.FBRHWInfoEntity">
        <result column="files" javaType="com.alibaba.fastjson.JSONObject" property="files"
                typeHandler="com.xxx.platform.common.mybatis.JsonArrayTypeHandler"/>
    </resultMap>
    <select id="getByFbrInfoId" resultMap="CurrentType">
        select * from fbr_hw_info where fbr_info_id = #{param}
    </select>
</mapper>

3. 工具类

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

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

/**
 * @description: JsonArrayTypeHandler
 * @author: leiming5
 * @date: 2021-02-07 08:57
 */
@MappedTypes({JSONArray.class})
public class JsonArrayTypeHandler extends BaseTypeHandler<Object> {

    private static final PGobject jsonObject = new PGobject();

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
        jsonObject.setType("jsonb");
        jsonObject.setValue(o.toString());
        preparedStatement.setObject(i, jsonObject);
    }

    @Override
    public JSONArray getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        return JSONArray.parseArray(resultSet.getString(columnName));
    }

    @Override
    public JSONArray getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
        return JSONArray.parseArray(resultSet.getString(columnIndex));
    }

    @Override
    public JSONArray getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
        return JSONArray.parseArray(callableStatement.getString(columnIndex));
    }
}
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL 中,`JSONB` 和 `text` 是两种不同的字段类型,分别用于存储不同类型的数据。 1. 存储方式: - `JSONB` 类型对 JSON 数据进行解析、验证和优化存储。它使用二进制格式存储 JSON 数据,以提高查询和索引的性能。JSONB 类型支持所有 JSON 数据类型,包括对象、数组、字符串、数字、布尔值和 null。 - `text` 类型是用于存储任意文本数据的一种通用类型。它不会对数据进行解析或验证,只是简单地将文本数据存储为字符串。 2. 查询和操作: - `JSONB` 类型提供了专门针对 JSON 数据的查询和操作函数,例如 `jsonb_extract_path`、`jsonb_array_length` 等。这些函数可用于从 JSONB 字段中提取特定的值、搜索特定的键或值,以及对 JSON 数据进行修改和更新。 - `text` 类型在操作时需要使用通用的字符串函数,如 `LIKE`、`SUBSTRING` 等。这些函数无法直接处理 JSON 数据结构,需要自行编写解析逻辑。 3. 索引和性能: - `JSONB` 类型支持 GIN(Generalized Inverted Index)索引,可以加速 JSON 数据的查询。GIN 索引可以在 JSON 字段上进行全文搜索、键值搜索等操作,提高查询性能。 - `text` 类型可以使用通用的 B-tree 索引,但无法对 JSON 数据进行特定的索引优化。 综上所述,如果需要存储和操作 JSON 数据,并且需要高效的查询性能和索引支持,推荐使用 `JSONB` 类型。如果只是存储和展示普通的文本数据,可以使用 `text` 类型。 希望能解答你的问题!如有其他疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值