mybatis 如何通過resultMap 返回long

mybatis resultMap 返回long

<resultMap id="ResultOfLong" type="java.lang.Long">  
        <result column="budget" property="budget" javaType="long"/>  
    </resultMap> 

mybatis long 類型返回為null報異常

Mapper method ‘com.mpn.dao.TWeatherMapper.avgWeatherTemperature attempted to return null from a method with a primitive return type (long).

<select id="avgWeatherTemperature" resultType="java.lang.Long" parameterType="java.lang.String" >
        SELECT IFNULL(AVG(WeatherTemperature),0) AS OrderAverage FROM t_weather WHERE WeatherDate= #{date,jdbcType=VARCHAR};
  </select>
  
   <select id="sumWeatherRainfall" resultType="java.lang.Long" parameterType="java.lang.String" >
        SELECT SUM(WeatherTemperature) AS OrderAverage FROM t_weather WHERE WeatherDate= #{date,jdbcType=VARCHAR};
  </select>
  
   <select id="avghumidity" resultType="java.lang.Long" parameterType="java.lang.String" >
        SELECT IFNULL(AVG(WeatherHumidity),0) AS OrderAverage FROM t_weather WHERE WeatherDate= #{date,jdbcType=VARCHAR};
  </select>

由於返回為空long中沒有此類型所以拋異常,改為

<!-- 根據日期來進行計算平局值與 -->
   <select id="avgWeatherTemperature" resultType="java.lang.Long" parameterType="java.lang.String" >
        SELECT IFNULL(AVG(WeatherTemperature),0) AS OrderAverage FROM t_weather WHERE WeatherDate= #{date,jdbcType=VARCHAR};
  </select>
  
   <select id="sumWeatherRainfall" resultType="java.lang.Long" parameterType="java.lang.String" >
        SELECT IFNULL(SUM(WeatherTemperature),0) AS OrderAverage FROM t_weather WHERE WeatherDate= #{date,jdbcType=VARCHAR};
  </select>
  
   <select id="avghumidity" resultType="java.lang.Long" parameterType="java.lang.String" >
        SELECT IFNULL(AVG(WeatherHumidity),0) AS OrderAverage FROM t_weather WHERE WeatherDate= #{date,jdbcType=VARCHAR};
  </select>

用IFNULL來進行判斷!

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: