org.postgresql.util.PSQLException: Bad value for type long

项目用 springboot+mybatis +mybatisplus, 数据库是:postgresql执行查询时候返回错误。

org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'city_id' from result set. Cause: org.postgresql.util.PSQLException: Bad value for type long :
; Bad value for type long : ; nested exception is org.postgresql.util.PSQLException: Bad value for type long

类型long的值不正确

完整错误信息如下

java 复制代码
org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'city_id' from result set.  Cause: org.postgresql.util.PSQLException: Bad value for type long : 
; Bad value for type long : ; nested exception is org.postgresql.util.PSQLException: Bad value for type long : 
	at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:104)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:88)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
	at com.sun.proxy.$Proxy174.selectOne(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:108)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
	at com.sun.proxy.$Proxy204.getUser(Unknown Source)

java对象实体

mybatis mapper XML 文件

java 复制代码
    <resultMap id="BaseResultMap" type="org.vo.UserVO">
        <id column="user_id" property="userId"/>
        <result column="city_id" property="cityId"/>
        <result column="sex" property="sex"/>
        <result column="header" property="header"/>
        <result column="nick_name" property="nickName"/>
        <result column="telphone" property="telphone"/>
        <result column="email" property="email"/>
        <result column="address" property="address"/>
        <result column="birthday" property="birthday"/>
        <result column="real_name" property="realName"/>
        <result column="local_address" property="localAddress"/>
        <result column="create_time" property="createTime"/>
    </resultMap>

    


<select id="getUser" resultMap="BaseResultMap">
        SELECT *
        FROM c_user
        WHERE user_id = #{userId}
    </select>

在数据库中 city_id 是空的。

在mysql是正常的。切换到postgresql 就会提示这样的错。

我把数据为空的字段赋值以后就不报错误了。

解决方案:直接在定义BaseResultMap时候定义字段类型 ResultType="Long"

【注意】一定是大写的Long。Long:用于表示long类型的包装类。long 不能等于null,但是包装类是对象可以为null。

相关推荐
倒流时光三十年11 分钟前
第一阶段 05 · Java 客户端查询类详解(Query / SearchCriteria / Response 与复杂拼接)
java·开发语言·python
rannn_11111 分钟前
【力扣hot100】哈希表专题——从两数之和到最长连续序列
java·算法·leetcode·哈希
观远数据13 分钟前
云原生BI的战略价值:不是技术选择,而是业务弹性的决定因素
java·人工智能·云原生
Full Stack Developme15 分钟前
Tomcat 设计原理
java·tomcat
gongzhxu22 分钟前
JetBrains IDEA开发环境搭建
java·ide·intellij-idea
程序员三明治30 分钟前
【AI】RAG 生成阶段的最后一公里:Prompt 设计、幻觉抑制与引用对齐
java·人工智能·ai·大模型·llm·prompt·rag
Bug收容所33 分钟前
12305项目学习day5
java·spring boot·redis·mysql·spring·rocketmq
::呵呵哒::34 分钟前
java中SseEmitter
java·开发语言
lichuangcsdn35 分钟前
【Spring AI 学习(三)】实现简单的对话
java·人工智能·学习·spring·spring ai
长不胖的路人甲43 分钟前
Spring Bean 完整生命周期
java·spring·rpc