mybatis查詢結果返回爲空(NULL)但是查數據庫能查到

在自己搭的項目中出現了這個錯誤。

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating interface com.chun.zeroyuanlottery.mapper.ReviewLotteryGoodsMapper with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.chun.zeroyuanlottery.mapper.ReviewLotteryGoodsMapper.()

經過長時間的思考,現象如下:resultType是java庫的實體類(如integer等),能返回值,如果是自己寫的實體類,則屬性字段返回null,唯獨id(表字段和實體類同名)有返回值,還有個現象就是 代碼顯示錯誤。所以猜測是select的返回值字段與實體類不一致,也就是無法映射到實體類進行封裝。

解決辦法:
mybatis開啓駝峯命名。
前提:數據庫表設計按照規範“字段名中各單詞使用下劃線"_"劃分”;如user_id 則實體類的字段爲userId
1).yml文件中配置:

#mybatis配置
mybatis:
    # 映射文件所在路徑
    mapper-locations: classpath:mapper/*.xml
    # pojo類所在包路徑
    type-aliases-package: com.chun.zeroyuanlottery.model
    configuration:
        map-underscore-to-camel-case: true

2)指定配置文件下進行配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--開啓駝峯命名規則自動轉換-->
    <settings>
    <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

yml需指定配置文件:

mybatis:
    # 映射文件所在路徑
    mapper-locations: classpath:mapper/*.xml
    # pojo類所在包路徑
    type-aliases-package: com.chun.zeroyuanlottery.model
	configLocation: classpath:/mybatis-config.xml
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章