1. 程式人生 > >《springboot和mybatis專案使用列舉轉換器出現No enum constant及解決方法》

《springboot和mybatis專案使用列舉轉換器出現No enum constant及解決方法》


mybatis列舉轉換器參見此篇部落格
我們知道寫好了列舉轉換器以後,可以在xxxMapper.xml中配置handler,如下:

 <result column="type" property="personEnum" typeHandler="com.db.enumhandler.PersonTypeHandler"/>

也可以在mybatis-config.xml中進行配置,如下:

<typeHandlers>
    	<typeHandler handler="com.db.enumhandler.PersonTypeHandler"/>
    </typeHandlers>

在springboot專案中,第一種方式試了,OK沒問題,但是使用第二種方式的時候出現如下錯誤:


Error attempting to get column 'type' from result set.  Cause: java.lang.IllegalArgumentException: No enum constant com.db.enums.PersonEnum.1

Caused by: java.lang.IllegalArgumentException: No enum constant com.db.enums.PersonEnum.1

說明springboot沒有找到列舉轉換器,查找了半天,終於找到原因。

springboot專案中需要在application.properties增加如下程式碼:

mybatis.typeHandlersPackage=com.db.enumhandler

值為列舉處理器所在的包名。