Mybatis Plus中保存String到UUID字段的问题
问题描述
SpringBoot 项目中,数据表字段类型为 uuid,Mybatis Plus 的 Mapper 进行 save 操作时,触发以下异常:
|
|
解决方法
数据库配置加上 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()方法的参数将以未知的类型发送给pg数据库,由数据库根据表中字段的类型进行推定和自动转换。也就是说我们可以以字符串形式给postgresql数据库中各种类型的数据进行赋值,当然也可以支持jsonb类型。