postgreSql存储jsonb

postgreSQL 支持 json类型存储

使用异同如下:

1. 字段修改

1.1 修改前

字符串类型

java 复制代码
 private String files;
 -- 数据库
 files varchar(511),
1.2 修改后

jsonArray 类型

java 复制代码
@TableField(typeHandler = JsonArrayTypeHandler.class)
 private JSONArray files;
 -- 数据库
 files jsonb,

2. 查询修改

2.1 修改前

使用mybatisPlus 默认查询

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

需要自己写查询的mapper方法

java 复制代码
List<FBRHWInfoEntity> hwList = fbrhwInfoMapper.getByFbrInfoId(fbrInfoId); 

mapper中 resultMap 的其余字段不用写

xml 复制代码
<?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. 工具类

java 复制代码
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));
    }
}
相关推荐
每天都在想吃啥3 分钟前
day31 SQLITE
数据库·sqlite
m0_748254093 小时前
2025最新华为云国际版注册图文流程-不用绑定海外信用卡注册
服务器·数据库·华为云
大新屋3 小时前
MongoDB 分片集群修改管理员密码
数据库·mongodb
ejinxian3 小时前
MySQL/Kafka数据集成同步,增量同步及全量同步
数据库·mysql·kafka
未来之窗软件服务3 小时前
数据库优化提速(一)之进销存库存管理—仙盟创梦IDE
数据库·sql·数据库调优
Mapmost4 小时前
信创浪潮下的GIS技术变革:从自主可控到生态繁荣
数据库
foundbug9994 小时前
Node.js导入MongoDB具体操作
数据库·mongodb·node.js
天天进步20154 小时前
Node.js中的Prisma应用:现代数据库开发的最佳实践
数据库·node.js·数据库开发
hui函数5 小时前
Flask高效数据库操作指南
数据库·python·flask