问题:
mybatis中Date参数传入报异常:java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String;
异常信息如下:
org.mybatis.spring.MyBatisSystemException: nested exception is
org.apache.ibatis.type.TypeException: Could not set parameters for mapping:
ParameterMapping{property='date', mode=IN, javaType=class java.lang.String, jdbcType=null,
numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #3 with JdbcType
null . Try setting a different JdbcType for this parameter or a different configuration
property. Cause: java.lang.ClassCastException: java.util.Date cannot be cast to
java.lang.String
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy80.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy84.selectByClientNameAndCompanyName(Unknown Source)
at com.eloan.biz.schedule.BaseFeeTransferBiz.batchBaseFeeTransfer(BaseFeeTransferBiz.java:138)
at com.eloan.application.controller.batch.ScheduleController.batchBaseFeeTransfer(ScheduleController.java:157)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
.....
解决方案:
方案一:检查mybatis的mapper文件传入参数设置的类型,将设置的类型的parameterType删除掉;
方案一:错误截图
方案一:正确截图
方案二:正确转换String和Date类型;代码和截图如下:
<select id="test" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from test
WHERE seq = #{seq,jdbcType=VARCHAR}
AND name = #{Name,jdbcType=VARCHAR}
AND <![CDATA[DATE_FORMAT(exec_date,'%Y-%m-%d') == DATE_FORMAT(#{date},'%Y-%m-%d')]]>
</select>
方案三:将String参数转换成Date类型,或将Date类型转化成String;
1.日期转字符串(格式化)
package com.test.dateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Test;
public class Date2String {
@Test
public void test() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
System.out.println(sdf.format(date));
}
}
2.字符串转日期(解析),注意:在字符串转日期操作时,需要注意给定的模式必须和给定的字符串格式匹配,否则会抛 出java.text.ParseException异常
package com.test.dateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.junit.Test;
public class String2Date {
@Test
public void test() throws ParseException {
String string = "2016-10-24 21:59:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.parse(string));
}
}
原因:
- 参数类型不匹配,传入date,mybatis接收的类型设置成String,导致类型不匹配;
- 类型转换错误,传入参数要转换成对应的类型;
相关知识点:
- date类型可以直接比较;
- String和date类型要做转换;
代码示例:
如上截图。