mybatis + mysql +spring mvc date字段报错java.lang.NumberFormatException

本文探讨了在数据库操作中遇到日期格式转换问题的解决方案,特别关注了使用MySQL时将日期字符串正确转化为日期类型的方法,以及如何避免在查询和更新过程中出现的NumberFormatException错误。通过提供具体的代码示例,本文旨在帮助开发者解决实际开发中常见的日期格式不匹配问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<select id="findMapByCondition" resultType="java.util.HashMap">
        SELECT
        pc_user_id               as userId,
            username                 as username,
            password                 as password,
            email                    as email,
            cellphone                as cellphone,
            nickname                 as nickname,
            realname                 as realname,
            photo                    as photo,
            signature                as signature,
            lvl                      as lvl,
            experience               as experience,
            credits                  as credits,
           <!--  DATE_FORMAT(login_time , '%Y-%m-%d %H:%i')   as loginTime, -->
            CAST(login_time AS CHAR) AS loginTime,
            email_authentication     as emailAuthentication,
            cellphone_authentication as cellphoneAuthentication,
            realname_authentication  as realnameAuthentication,
            status                   as status,
            image_name               as imageName,
            image_path               as imagePath,
            expand_info_id           as expandInfoId,
            school_id                as schoolId,
            school_name              as schoolName,
            area_id                  as areaId,
            specialty_id             as specialtyId,
            specialty_name           as specialtyName,
            year_of_admission        as yearOfAdmission,
            year_of_graduate         as yearOfGraduate
        FROM view_user
        ${whereCondition}

    </select>


前提:model和数据库字段属性全为date

若使用

 DATE_FORMAT(login_time , '%Y-%m-%d %H:%i')   as loginTime, 

查询出来没问题,但是修改时,若会报

Caused by: java.lang.NumberFormatException: For input string: "2013-10-29 10:10",

若改成 CAST(login_time AS CHAR) AS loginTime,则解决问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值