java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String

问题:

           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类型要做转换;

代码示例:

     如上截图。

 

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值