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的值不正确

完整错误信息如下


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 文件

    <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。

文章来源地址https://uudwc.com/A/zkpxa

原文地址:https://blog.csdn.net/wochunyang/article/details/133132828

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

h
上一篇 2023年09月24日 16:38
SQL 基础知识梳理(一)- 数据库与 SQL
下一篇 2023年09月24日 16:39