Mybatis参数是一个对象,对象中含有Date[ ]数组,如何按照索引获取值

很多表单查询都会按照时间区间查询,这个时候前端是固定传入长度为2的时间数组参数。后端如果分成2个参数去接受,就显得很麻烦,直接用数组接受会方便很多,但是有坑。

一、在做时间区间查询的时候,前端固定传一个长度为2的时间数组参数。

二、后端实体类中,用这样的形式接收。

三、如果像这样直接按照索引获取,会报错

 <if test="checkTimeArray != null and checkTimeArray.length == 2">
     AND  DATE_FORMAT(a.check_time,'%Y-%m-%d') BETWEEN DATE_FORMAT(#{checkTimeArray[0]},'%Y-%m-%d') AND DATE_FORMAT(#{checkTimeArray[1]},'%Y-%m-%d')
 </if>
### Error querying database.  Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'checkTimeArray[0]'. It was either not specified and/or could not be found for the javaType ([Ljava.util.Date;) : jdbcType (null) combination.
### Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'checkTimeArray[0]'. It was either not specified and/or could not be found for the javaType ([Ljava.util.Date;) : jdbcType (null) combination.

四、只需要在参数后面加上javaType和jdbcType就好了。如

#{checkTimeArray[0],javaType=java.util.Date,jdbcType=TIMESTAMP}

<if test="checkTimeArray != null and checkTimeArray.length == 2">
     AND  DATE_FORMAT(a.check_time,'%Y-%m-%d') BETWEEN DATE_FORMAT(#{checkTimeArray[0],javaType=java.util.Date,jdbcType=TIMESTAMP},'%Y-%m-%d') AND DATE_FORMAT(#{checkTimeArray[1],javaType=java.util.Date,jdbcType=TIMESTAMP},'%Y-%m-%d')
</if>

猜你喜欢

转载自blog.csdn.net/qq_39648029/article/details/108594806
今日推荐