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));
    }
}
相关推荐
倔强的石头1065 分钟前
让时序开发更可控:金仓时序 DB 的易用性实践与平台化路径
数据库·kingbase
数据知道14 分钟前
PostgreSQL实战:如何用 CTE(公用表表达式)解决复杂的查询逻辑
数据库·postgresql
1.14(java)20 分钟前
MySQL索引原理与B+树应用详解
数据库·b树·mysql
java干货21 分钟前
用 MySQL SELECT SLEEP() 优雅模拟网络超时与并发死锁
网络·数据库·mysql
哈哈不让取名字23 分钟前
使用Fabric自动化你的部署流程
jvm·数据库·python
洛_尘25 分钟前
MySQL 6:数据库约束
数据库·mysql
dawudayudaxue26 分钟前
sqlite在安卓下使用ndk的交叉编译
android·数据库·sqlite
YIN_尹26 分钟前
【MySQL】表的约束(下)
android·数据库·mysql
lkbhua莱克瓦2429 分钟前
Apache Maven全面解析
java·数据库·笔记·maven·apache
optimistic_chen30 分钟前
【Redis系列】哨兵模式
linux·数据库·redis·分布式·哨兵