服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - mybatis 如何通过resultMap 返回long

mybatis 如何通过resultMap 返回long

2021-10-19 10:26qq_16055765 Java教程

这篇文章主要介绍了mybatis 如何通过resultMap 返回long的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

mybatis resultMap 返回long

?
1
2
3
<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).

?
1
2
3
4
5
6
7
8
9
10
11
<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中没有此类型所以抛异常,改为

?
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 根据日期来进行计算平局值与 -->
   <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来进行判断!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_16055765/article/details/79665579

延伸 · 阅读

精彩推荐