SpringBoot+Mybatis使用Enum枚舉類型總是報(bào)錯(cuò)No enum constant XX問題
SpringBoot+Mybatis使用Enum枚舉類型總是報(bào)錯(cuò)No enum constant XX
環(huán)境SpringBoot+Mybatis
比如:
數(shù)據(jù)庫中User表存放status字段值為1,想要通過Mybatis轉(zhuǎn)換后為正在使用
當(dāng)然,可以使用if else 但是狀態(tài)值很多時(shí),就變得很復(fù)雜,且不利于維護(hù),故需要用到枚舉類
數(shù)據(jù)庫查詢時(shí)獲得status值為1,通過Mybatis依照枚舉類進(jìn)行轉(zhuǎn)換獲取到對(duì)應(yīng)的狀態(tài)
之前使用時(shí)總是報(bào)錯(cuò)
Wed Jan 02 10:59:18 CST 2019
There was an unexpected error (type=Internal Server Error, status=500).
nested exception is org.apache.ibatis.executor.result.ResultMapException:
Error attempting to get column 'qu_type' from result set.
Cause: java.lang.IllegalArgumentException: No enum constant com.test.model.survey.QuType.1
此時(shí)的實(shí)體類代碼如下:
public class Question {
private String id;
// 類型
private QuType quType;
。。get set
}枚舉類代碼:
public enum QuType {
YESNO("是否","yesno", 0);
private String quName;
private String quEnName;
private int index;
QuType(String quName,String quEnName,int index){
this.cnName=quName;
this.quEnName=quEnName;
this.index=index;
}
..get set
}Mybatis文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.mapper.QuestionMapper">
<select id="getQuestionById" resultType="Question">
SELECT * FROM question WHERE id = #{Id}
</select>
</mapper>此時(shí)就會(huì)報(bào)錯(cuò):
No enum constant com.test.model.survey.QuType.1
原因是無法使用Mybatis默認(rèn)的轉(zhuǎn)換器EnumTypeHandler 進(jìn)行轉(zhuǎn)換,
解決方法
只需要修改mybatis文件,添加ResultMap配置,對(duì)需要枚舉轉(zhuǎn)換的字段配置特定的轉(zhuǎn)換類EnumOrdinalTypeHandler
如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.mapper.QuestionMapper">
<resultMap id="questionMap" type="com.test.model.survey.Question" >
<id column="id" property="id"/>
<result column="qu_type" property="quType" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
</resultMap>
<select id="getQuestionById" resultMap="questionMap">
SELECT * FROM question WHERE id = #{Id}
</select>
</mapper>注意?。?/p>
1:查詢中resultType修改為ResultMap,否則會(huì)報(bào) can not find class XXX
2:此處Result屬性中,column對(duì)應(yīng)的是數(shù)據(jù)庫中字段,property是實(shí)體類中屬性,項(xiàng)目中使用了數(shù)據(jù)庫中_+小寫轉(zhuǎn)換為大寫駝峰寫法的配置
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Data JPA 關(guān)鍵字Exists的用法說明
這篇文章主要介紹了Spring Data JPA 關(guān)鍵字Exists的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringMVC請(qǐng)求、響應(yīng)和攔截器的使用實(shí)例詳解
攔截器(Interceptor) 它是一個(gè)Spring組件,并由Spring容器管理,并不依賴Tomcat等容器,是可以單獨(dú)使用的,這篇文章給大家介紹SpringMVC請(qǐng)求、響應(yīng)和攔截器的使用,感興趣的朋友一起看看吧2024-03-03
Eclipse配置tomcat發(fā)布路徑的問題wtpwebapps解決辦法
這篇文章主要介紹了Eclipse配置tomcat發(fā)布路徑的問題wtpwebapps解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
聊一聊SpringBoot服務(wù)監(jiān)控機(jī)制
這篇文章主要介紹了聊一聊SpringBoot服務(wù)監(jiān)控機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
tomcat connection-timeout連接超時(shí)源碼解析
這篇文章主要為大家介紹了tomcat connection-timeout連接超時(shí)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

