SSM整合关于MySQL数据库时间类型字段的问题,以及解决办法and注意事项!

首先使用数据口DateTime

在这里插入图片描述
因为要精确到秒,所以使用DateTime

在java实体类中使用Timestamp

private Timestamp Time;
Timestamp与数据库当中的DateTime是对应的数据类型

创建工具类TimeTool

public class TimeTool implements Converter<String, Date> {
    @Override
    public Date convert(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

类里的包

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

在spring-mvc.xml文件中配置一下信息

<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    <!-- 自定义参数绑定 -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!-- 转换器 -->
        <property name="converters">
            <list>
                <!-- 日期类型转换 -->
                <bean class="com.ssm.utils.CustomDateConverter"/>
            </list>
        </property>
    </bean>

注意事项

千万注意,应为数据库当中使用的是DateTime,它的格式是“yyyy-MM-dd HH:mm:ss”,所以在jsp页面当中,一定要精确到秒,否则会报错
错误信息如下:

Field error in object 'product' on field 'departureTime': rejected value [2019-08-14 07:35]; codes 
[typeMismatch.product.departureTime,typeMismatch.departureTime,typeMismatch.java.util.Date,ty
peMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: 
codes [product.departureTime,departureTime]; arguments []; default message [departureTime]]; 
default message [Failed to convert property value of type 'java.lang.String' to required type 
'java.sql.Timestamp' for property 'departureTime'; nested exception is 
org.springframework.core.convert.ConversionFailedException: Failed to convert from type 
[java.lang.String] to type [java.sql.Timestamp] for value '2019-08-14 07:35'; nested exception is 
java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]]
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值