当前位置: 首页 > article >正文

使用 Mybatis 的 TypeHandler 存取 Postgresql jsonb 类型

文章目录

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

使用 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;

}

常见错误

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.kler.cn/news/149010.html

相关文章:

  • 【数学】旋转矩阵
  • xv6 磁盘中断流程和启动时调度流程
  • Java中实现精度准确的浮点数运算
  • SpringBoot——模板引擎及原理
  • RK3568平台开发系列讲解(Linux系统篇)通过OF函数获取设备树中断信息实验
  • 2023年国赛试题:配置inux1 为 CA 服务器
  • Linux dd命令详解:如何从标准输入或文件中读取、转换并输出数据(附实例教程和注意事项)
  • 7.5 Windows驱动开发:监控Register注册表回调
  • Day09
  • using meta-SQL 使用元SQL
  • awk,sed都可以用号表示查找结果,给查找结果加括号反引号
  • 『 Linux 』进程优先级
  • Pytorch:view()、permute()和contiguous()
  • Unity学习笔记11
  • 【Android Jetpack】Navigation的使用
  • 智能优化算法应用:基于花授粉算法无线传感器网络(WSN)覆盖优化 - 附代码
  • 《尚品甄选》:后台系统——权限管理之分类和品牌管理,使用EasyExcel导入导出数据(debug一遍)
  • 记录vscode常用插件集合(extensions)
  • yml转properties工具
  • 【深度学习】参数优化和训练技巧
  • 森林无人机高效解决巡查难题,林区防火掀新篇
  • 链路追踪详解(二):链路追踪技术的演进
  • 万字解析设计模式之观察者模式、中介者模式、访问者模式
  • 开发知识点-CSS样式
  • [栈迁移+ret滑梯]gyctf_2020_borrowstack
  • 使用opencv实现更换证件照背景颜色
  • 使用oxylabs代理国外ip请求openai接口报错记录
  • 服务器主机安全如何保障
  • 【数据结构 —— 二叉树的链式结构实现】
  • 数据分享 I 全国各市城镇化率,shapeflie格式,附数据可视化