同一个错误,就讲讲查询好了
controller里的映射代码段:
@RequestMapping("/queryuser")
public User queryUser(String id) throws Exception{
return userService.queryUserById(id);
}
serviceImpl的代码段
@Transactional(propagation = Propagation.SUPPORTS)
public User queryUserById(String id) throws Exception{
return userMapper.selectByPrimaryKey(id);
}
代码这么简单了……怎么会出错的?错误如下:
这是网页的反馈:
{
"timestamp": "2018-11-23T18:04:30.660+0000",
"status": 500,
"error": "Internal Server Error",
"message": "nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='age', mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #5 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer",
"path": "/mybatis/queryuser"
}
这是eclipse的错误
org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='age', mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #5 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer] with root cause
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
这里在说age属性的jdbcType是Integer,然后不能由String转Integer,但是那个xxmapper.xml是自动生成的,视频里老师也没改过,我用String的ID查出来User对象,不是应该直接显示成JSON吗?为什么需要转age属性?百度了一下,发生这个错误的原因各不相同,看到的朋友请赐教
哎,我傻了呀,解决了。我一开始思路不对,我以为是jdbcType出了什么问题,想想userMapper.xml是自动生成的不应该呀,后来以为是我application.properties里的下面这两句没生效,导致什么类加载器没用对产生了冲突
restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar
然后我就注掉了devtools的依赖,但是还是不对。后来一行行看下面的错误,发现还是service层的问题,最后才找到了……我的方法是return userMapper.selectByPrimaryKey(id);这个primaryKey数据库里建表时候是指定了,但是自动生成的实体类倒是干干净净的,它应该是没找到这个primaryKey,然后刚好匹配下来age是Integer类型,才跟我扯这个“java.lang.String cannot be cast to java.lang.Integer”和”Error setting non null for parameter #5 with JdbcType null . ”这点错误卡了我一个晚上真是太傻了……话说自动生成怎么不那么智能呢,主键不给实体类自动指定掉