Mybatis插入postgresql数据库中类型为UUID的字段

项目中使用postgresql数据库,当表的主键是UUID类型时,出现了一些问题,由于某些原因,无法修改数据库中字段类型,只能自己想办法解决:

问题一,使用mybatis-generator自动生成时,UUID字段类型由于无法映射成对应的Java类型,会默认映射成Object对象;

解决方案(PS:表中的主键名是“uuid”,字段类型是UUID):

	<table tableName="centrex_operator" domainObjectName="CentrexOperator" 
		enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" 
		enableSelectByExample="false" selectByExampleQueryId="false">
			<!-- column是表中的字段名 
			jdbcType是将表中的该字段指定为某种类型 
			property是对应的Java Bean中的属性名 
			javaType是对应的属性类型-->
		  <columnOverride column="uuid" jdbcType="VARCHAR" property="uuid" javaType="java.lang.String" />
		</table>
通过以上方法可将类型为UUID的字段映射成对应的Java属性类型为字符串String。


问题二,对于表中的UUID字段,新增或删除记录,都无法将该字段插入进去,会报PSQLException: 字段“uuid”的类型为uuid,但表达式的类型为character varying ,如下图所示:



解决方案:

将与表中类型为UUID的对应的Java属性转换一下,如下图中的红色矩形框所示:


原本是'#{uuid,jdbcType=VARCHAR}',改成‘#{uuid}::uuid’即可,其余的类似,经过测试,增删改查中,只要在将表中类型为UUID的字段对应的Java属性,用#{}取值时,在后面加上'::uuid'就可以了,查了一下,字段是uuid类型的,不能直接使用,需要通过col::uuid转换。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis Plus 默认支持 PostgreSQL 数据库,无需额外适配。在使用时只需要设置对应的数据源、配置文件和实体类映射即可。以下是一个简单的示例: 1. 添加依赖 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.23</version> </dependency> ``` 2. 配置数据源 ```properties spring.datasource.url=jdbc:postgresql://localhost:5432/mydb spring.datasource.username=postgres spring.datasource.password=123456 spring.datasource.driver-class-name=org.postgresql.Driver ``` 3. 配置 MyBatis Plus 在 MyBatis Plus 的配置文件,需要设置一些 PostgreSQL 特有的参数,例如数组类型的映射、标识符的转义等。 ```properties # 配置数组类型的映射 mybatis.type-handlers-package=com.baomidou.mybatisplus.extension.handlers # 配置标识符的转义 mybatis.configuration.map-underscore-to-camel-case=true mybatis.configuration.jdbc-type-for-null=null mybatis.configuration.local-cache-scope=STATEMENT mybatis.configuration.cache-enabled=false mybatis.configuration.default-fetch-size=100 mybatis.configuration.default-statement-timeout=30 mybatis.configuration.default-executor-type=reuse mybatis.configuration.default-auto-mapping=JDBC mybatis.configuration.default-statement-type=PREPARED mybatis.configuration.default-use-column-label=true mybatis.configuration.default-use-generated-keys=false mybatis.configuration.default-statement-result-type=HASH_MAP mybatis.configuration.default-scripting-language=VelocitySQL mybatis.configuration.default-enum-type-handler=org.apache.ibatis.type.EnumOrdinalTypeHandler mybatis.configuration.default-map-type-handler=org.apache.ibatis.type.MapTypeHandler mybatis.configuration.default-date-type-handler=org.apache.ibatis.type.DateTypeHandler mybatis.configuration.default-calendar-type-handler=org.apache.ibatis.type.CalendarTypeHandler mybatis.configuration.default-numeric-type-handler=org.apache.ibatis.type.BigDecimalTypeHandler mybatis.configuration.default-lob-handler=com.baomidou.mybatisplus.extension.handlers.OracleLobHandler mybatis.configuration.default-boolean-type-handler=org.apache.ibatis.type.BooleanTypeHandler mybatis.configuration.default-byte-type-handler=org.apache.ibatis.type.ByteTypeHandler mybatis.configuration.default-short-type-handler=org.apache.ibatis.type.ShortTypeHandler mybatis.configuration.default-integer-type-handler=org.apache.ibatis.type.IntegerTypeHandler mybatis.configuration.default-long-type-handler=org.apache.ibatis.type.LongTypeHandler mybatis.configuration.default-float-type-handler=org.apache.ibatis.type.FloatTypeHandler mybatis.configuration.default-double-type-handler=org.apache.ibatis.type.DoubleTypeHandler mybatis.configuration.default-string-type-handler=org.apache.ibatis.type.StringTypeHandler mybatis.configuration.default-byte-array-type-handler=org.apache.ibatis.type.ByteArrayTypeHandler mybatis.configuration.default-object-type-handler=org.apache.ibatis.type.ObjectTypeHandler mybatis.configuration.default-clob-type-handler=org.apache.ibatis.type.ClobTypeHandler mybatis.configuration.default-reader-type-handler=org.apache.ibatis.type.ReaderTypeHandler mybatis.configuration.default-input-stream-type-handler=org.apache.ibatis.type.InputStreamTypeHandler mybatis.configuration.default-blob-type-handler=org.apache.ibatis.type.BlobTypeHandler mybatis.configuration.default-uuid-type-handler=org.apache.ibatis.type.EnumOrdinalTypeHandler mybatis.configuration.default-time-zone=GMT+8 ``` 4. 配置实体类映射 在实体类的属性上使用注解或配置文件来映射数据库字段。例如: ```java @Data public class User { @TableId private Long id; @TableField("user_name") private String userName; private Integer age; } ``` ```xml <!-- 配置实体类映射 --> <bean id="userMapper" class="com.baomidou.mybatisplus.core.mapper.BaseMapper"> <property name="entityClass" value="com.example.User"/> <property name="mapperClass" value="com.example.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> ``` 以上就是在 MyBatis Plus 适配 PostgreSQL 数据库的基本步骤。如果有更复杂的需求,可以参考官方文档或在社区寻求帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值