① Field ‘id‘ doesn‘t have a default value ②Field error in object ‘xxx’ on field ‘XXX’: rejected valu

出现问题:

### The error occurred while setting parameters
### SQL: insert into product(productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus) values(?,?,?,?,?,?,?)
### Cause: java.sql.SQLException: Field 'id' doesn't have a default value
; ]; Field 'id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'id' doesn't have a default value
2020-12-15 16:03:59,733 76658  [io-8888-exec-10] DEBUG efaultHandlerExceptionResolver  - Resolving exception from handler [public java.lang.String candy.ssm.controller.ProductController.save(candy.ssm.domain.Product) throws java.lang.Exception]: org.springframework.dao.DataIntegrityViolationException: 
### Error updating database.  Cause: java.sql.SQLException: Field 'id' doesn't have a default value
### The error may involve candy.ssm.dao.IProductDao.save-Inline
### The error occurred while setting parameters
### SQL: insert into product(productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus) values(?,?,?,?,?,?,?)
### Cause: java.sql.SQLException: Field 'id' doesn't have a default value
; ]; Field 'id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'id' doesn't have a default value
2020-12-15 16:03:59,734 76659  [io-8888-exec-10] DEBUG .web.servlet.DispatcherServlet  - Could not complete request
org.springframework.dao.DataIntegrityViolationException: 
### Error updating database.  Cause: java.sql.SQLException: Field 'id' doesn't have a default value
### The error may involve candy.ssm.dao.IProductDao.save-Inline
### The error occurred while setting parameters
### SQL: insert into product(productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus) values(?,?,?,?,?,?,?)
### Cause: java.sql.SQLException: Field 'id' doesn't have a default value
; ]; Field 'id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'id' doesn't have a default value
	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:247)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
	at com.sun.proxy.$Proxy38.insert(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:279)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57)

分析发现

分析调试过程中出现过如下几个报错信息:

① Cause: java.sql.SQLException: Parameter index out of range (6 > number of parameter 5

分析:

一开始报错显示是sql语句缺少参数,多给出一个参数,后来发现自己并没有多给参数
在这里插入图片描述
解决:数据库的sql语句没有问题,

没有改动代码,就。。鼓捣

② 报错

Field error in object ‘xxx’ on field ‘XXX’: rejected value

Field error in object 'product' on field 'departureTime': rejected value [2020-12-15 14:28]; codes [typeMismatch.product.departureTime,typeMismatch.departureTime,typeMismatch.java.util.Date,typeMismatch]; 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.util.Date' for property 'departureTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-12-15 14:28'; nested exception is java.lang.IllegalArgumentException]

分析

显示前端页面做添加用户时,在加载Controller的方法之前出现以上异常,

解决

从异常信息中获悉,在前端日期控件输入的日期是String类型,String类型的日期无法转换成相应的Date日期格式,数据库的日期类型格式有date(年-月-日/yyyy-MM-dd)和datetime(年-月-日 时:分:秒/yyyy-MM-dd HH:mm:ss)

则:

其中涉及知识点:
SpringMVC绑定参数之类型转换三种方式:

  • 实体类中加日期格式化注解

    @DateTimeFormat(pattern=“yyyy-MM-dd HH:mm”)
    private Date creationTime;

    缺点:局部转换,只能对类中单个属性进行类型转换

  • 属性编辑器

  • 类型转换器Converter

在这里插入图片描述
并且加上日期格式转换工具类在utils包下,

/**
 * 日期转成工具类
 */
public class DateUtils {

    //日期转换成字符串
    public static String date2String(Date date, String patt) {
        SimpleDateFormat sdf = new SimpleDateFormat(patt);
        String format = sdf.format(date);
        return format;
    }

    //字符串转换成日期
    public static Date string2Date(String str, String patt) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(patt);
        Date parse = sdf.parse(str);
        return parse;
    }
}

对属性的get方法也需要进行修改

    public String getDepartureTimeStr() {
        if(departureTime!=null){
            departureTimeStr= DateUtils.date2String(departureTime,"yyyy-MM-dd HH:mm:ss");
        }
        return departureTimeStr;
    }

③ Field ‘id’ doesn’t have a default value 错误的解决办法

在这里插入图片描述
解决方法
分析及解决方法

把数据库中的id值设置为自增类型的int值,因为黑马的课程用的是oracle数据库,我这里改成mysql数据库没有让id自动生成uuid值,需要把id设置为唯一且自增的id,这样才能是唯一标识。

在这里插入图片描述

改成如下:

在这里插入图片描述
对于数据库参数问题

当把id改成自增的,则可以不用在sql语句给出参数,则不会出现参数越界的问题

posted @ 2020-12-15 16:24  your_棒棒糖  阅读(894)  评论(0)    收藏  举报