使用 Mybatis 的 TypeHandler 存取 Postgresql jsonb 类型

news/2024/7/9 22:32:21 标签: mybatis, postgresql, 数据库, jsonb

文章目录

    • 使用 TypeHandler 存取 Postgresql jsonb 类型
    • 常见错误
      • column "" is of type jsonb but expression is of type character varying

jsonb__2">使用 TypeHandler 存取 Postgresql jsonb 类型

首先在数据库表中定义 jsonb 类型:

create table tb_user_info
(
    id          varchar(36) not null,
    user_info   jsonb,
    user_list   jsonb,
    user_ids    varchar(36)[],
    create_time timestamp   not null default now(), -- 创建时间
    constraint pk_tb_user_info primary key (id)
);

使用 @TableName 标识实体类对应的表:

具体内容参考 MybatisPlus注解 章节

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import org.apache.ibatis.type.ArrayTypeHandler;
import org.apache.ibatis.type.JdbcType;

@TableName(value = "tb_user_info", autoResultMap = true)
public class AlarmRuleEntity {

    @TableField("id")
    private String id;

    @TableField("user_info")
    private String userInfo ;

    @TableField(value = "user_ids", jdbcType = JdbcType.ARRAY, typeHandler = ArrayTypeHandler.class)
    private String userIds ;

    @TableField(value = "user_list", typeHandler = JacksonTypeHandler.class)
    private List<UserInfo> userList;
}

@TableName 中的 autoResultMap = true 必须开启, @TableField 中的 jdbcType = JdbcType.ARRAY 用于指定数组类型,而typeHandler = ArrayTypeHandler.class 中的类则是自定义类型处理器。

UserInfo 如下:

/**
 * 用户类型
 */
public class UserInfo {
  /**
     * 用户id
     */
    private String userId;
    /**
     * 用户名
     */
    private String userName;

}

常见错误

jsonb_but_expression_is_of_type_character_varying_68">column “” is of type jsonb but expression is of type character varying

报错图片

解决办法:

在 JDBC URL参数中加入:stringtype=unspecified ,例如:

jdbc:postgresql://xxxxxxx:xxxx/db?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&stringtype=unspecified

官方对 stringtype 参数的解释:

stringtype : String
Specify the type to use when binding PreparedStatement parameters set via setString(). If stringtype is set to VARCHAR (the default), such parameters will be sent to the server as varchar parameters. If stringtype is set to unspecified, parameters will be sent to the server as untyped values, and the server will attempt to infer an appropriate type. This is useful if you have an existing application that uses setString() to set parameters that are actually some other type, such as integers, and you are unable to change the application to use an appropriate method such as setInt().

stringtype=unspecified 时,statement.setString() 方法的参数将以未知的类型发送给Postgresql 数据库,由数据库根据表中字段的类型进行推定和自动转换。


http://www.niftyadmin.cn/n/5223585.html

相关文章

解决ant-design-vue中Select组件v-model值为空字符串不显示placeholder的bug

方法一&#xff1a; 1.找到node_modules/ant-design-vue/es/vc-select/SingleSelector.js文件 搜索renderPlacehoder方法 将其修改为 const renderPlacehoder () > {const list props.values.filter(val > val.value ! );if (list[0]) {return null}... }2.在此文件中…

springcloud==ribbon

单独使用ribbon 建立两个服务端&#xff0c;分别是8080和8081 建立客户端 代码 package org.example.ribbon;import com.netflix.client.ClientFactory; import com.netflix.client.http.HttpRequest; import com.netflix.client.http.HttpResponse; import com.netflix.conf…

2023-11-28 LeetCode每日一题(设计前中后队列)

2023-11-28每日一题 一、题目编号 1760.设计前中后队列二、题目链接 点击跳转到题目位置 三、题目描述 请你设计一个队列&#xff0c;支持在前&#xff0c;中&#xff0c;后三个位置的 push 和 pop 操作。 请你完成 FrontMiddleBack 类&#xff1a; FrontMiddleBack() 初…

HbuilderX 项目打包文件过大问题优化

文章目录 HbuilderX 项目打包文件过大问题优化主要操作收效甚微&#xff0c;但又有那么点用的方法使用 gulp 压缩&#xff08;最后一步&#xff09;使用与配置 网上找的 gulp 优化压缩配置还未尝试可能有用的方法 尝试过程中看到的一些优质文章 HbuilderX 项目打包文件过大问题…

JSP迭代标签之 forEach循环标签 基本使用讲解

好 之前我们讲完了 我们的条件动作标签 那么 我们来继续说 迭代标签 所谓迭代就是 将某个主体循环多次 也可以循环 集合 对象 map 这个标签叫 forEach items 就是 我们要循环的数据 注意 这里 操作的也是域对象中的值 begin 开始说 例如 i 0;i<x;i begin 就是开始数 当前…

Android gradle 配置阿里镜像

要在Android Gradle中配置阿里镜像&#xff0c;可以按照以下步骤进行操作&#xff1a; 打开项目中的 build.gradle 文件。 在 build.gradle 文件中添加阿里镜像的地址&#xff0c;如下所示&#xff1a; buildscript {repositories {maven { url https://maven.aliyun.com/re…

fastadmin学习笔记-----动态下拉框

fastadmin 笔记记录。 把自己遇到的问题以及解决的问题记录以下&#xff0c;并分享出来&#xff0c;给大家参考&#xff0c;同时以免下次自己再踩坑。 这是一个下拉框&#xff0c;原本我的写法 这是编辑中的下拉框。 其中 foreach 中的 typeList 是后端传过来的数据。 原本…

LLM、ChatGPT与多模态必读论文150篇

为了写本 ChatGPT 笔记&#xff0c;我和10来位博士、业界大佬&#xff0c;在过去半年翻了大量中英文资料/paper&#xff0c;读完 ChatGPT 相关技术的150篇论文&#xff0c;当然还在不断深入。 由此而感慨&#xff1a; 读的论文越多&#xff0c;你会发现大部分人对ChatGPT的技…