視頻不完整……另外插入可以,但是查詢修改刪除報(bào)錯(cuò),有人遇到一樣的錯(cuò)誤嗎?
同一個(gè)錯(cuò)誤,就講講查詢好了
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);
}
代碼這么簡(jiǎn)單了……怎么會(huì)出錯(cuò)的?錯(cuò)誤如下:
這是網(wǎng)頁的反饋:
{
? ? "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的錯(cuò)誤
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轉(zhuǎn)Integer,但是那個(gè)xxmapper.xml是自動(dòng)生成的,視頻里老師也沒改過,我用String的ID查出來User對(duì)象,不是應(yīng)該直接顯示成JSON嗎?為什么需要轉(zhuǎn)age屬性?百度了一下,發(fā)生這個(gè)錯(cuò)誤的原因各不相同,看到的朋友請(qǐng)賜教
2018-11-24
哎,我傻了呀,解決了。我一開始思路不對(duì),我以為是jdbcType出了什么問題,想想userMapper.xml是自動(dòng)生成的不應(yīng)該呀,后來以為是我application.properties里的下面這兩句沒生效,導(dǎo)致什么類加載器沒用對(duì)產(chǎn)生了沖突
restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar
然后我就注掉了devtools的依賴,但是還是不對(duì)。后來一行行看下面的錯(cuò)誤,發(fā)現(xiàn)還是service層的問題,最后才找到了……我的方法是return userMapper.selectByPrimaryKey(id);這個(gè)primaryKey數(shù)據(jù)庫里建表時(shí)候是指定了,但是自動(dòng)生成的實(shí)體類倒是干干凈凈的,它應(yīng)該是沒找到這個(gè)primaryKey,然后剛好匹配下來age是Integer類型,才跟我扯這個(gè)“java.lang.String cannot be cast to java.lang.Integer”和”Error setting non null for parameter #5 with JdbcType null . ”這點(diǎn)錯(cuò)誤卡了我一個(gè)晚上真是太傻了……話說自動(dòng)生成怎么不那么智能呢,主鍵不給實(shí)體類自動(dòng)指定掉