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。

相关推荐
2601_962106644 小时前
【Java Set综合实战】商品管理系统 + 图书收藏系统|Product、Book自定义类全套代码
java
RuoyiOffice5 小时前
SpringBoot+Vue3 凭证生命周期设计:审核、过账、冲销与反过账怎样保持账目一致
java·spring boot·事务·springboot3·财务系统·会计凭证·ruoyi office
2601_962106645 小时前
HashMap入门|工号-姓名键值存储、两种遍历方式、根据key/value查
java
杨凯凡6 小时前
Spring Framework 7 API 版本管理实战:请求头路由、兼容迁移与废弃策略
java·rest api·springframework·spring boot 4·api版本管理
T_Wizard6 小时前
同一个问题写了 4 遍:官方 SDK / HTTP / Spring AI / LangChain4j,到底该选哪个
java·ai编程
hai_android6 小时前
AudioTrack 深度解析:从原理到实战
android·java·kotlin
斯维赤7 小时前
限流、熔断、降级,你真的分得清吗?
java·后端·架构
aiks_yuan9 小时前
Java CPU 100% 怎么排查:JDK 11 起 jstack 自带线程 CPU 时间,不用再 top -H 换算十六进制(JDK 8、11、17 实测)
java
Flynn Liang11 小时前
AiServices 源码级拆解:一个接口如何变成 RAG Agent
java·langchain·源码分析·rag·langchain4j·aiservices
Flynn Liang11 小时前
LangChain4j 1.23 架构全景:Java 大模型应用的 6 层抽象
java·langchain·大模型·rag·ai agent·langchain4j