• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mybatis 返回Integer,Double,String等类型的数据操作

java 搞代码 4年前 (2022-01-05) 21次浏览 已收录 0个评论

这篇文章主要介绍了mybatis 返回Integer,Double,String等类型的数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在使用mybatis的过程中会遇到只返回单独数据类型的问题会用到resultType。

 //返回Integer  SELECT speed from dws_tfc_state_speed_link_last_rt where link_id = #{linkId}  //返回Double类型  SELECT travel_time from dws_tfc_state_speed_link_last_rt where link_id = #{linkId}  //返回String 类型  SELECT speed from dws_tfc_state_speed_link_last_rt where link_id = #{linkId} 

补充知识:mybatis下返回类型为int,结果为null时报tempted to return null from a method with a primitive return type (int).

背景了解:

从mysql数据库中查询数据,用Int接收,因为数据库没有数据所以返回null,于是运行时报以下错误,提取关键的信息“attempted to return null from a method with a primitive return type (int).”,翻译成中文大概意思是“”尝试从具有基本返回类型(Int)的方法返回null“返回int的方法想要返回null,不符合规矩。

报错信息:

xml中的SQL和报错信息如下:

  select<em style="color:transparent">来源[email protected]搞@^&代*@码)网</em> SUM(alert_sum) as alert_sum from tb_checkresults 

2019-06-27 17:39:40,742 ERROR (DirectJDKLog.java:182)- Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Mapper method ‘com.chinalife.datacheck.dao.CheckresultDao.getyestodayalert attempted to return null from a method with a primitive return type (int).] with root cause org.apache.ibatis.binding.BindingException: Mapper method ‘com.chinalife.datacheck.dao.Checkresul*tDao.getyestodayalert attempted to return null *********************省略以下那些没用的*********************

解决办法:

(1)利用mysql的函数ifnull

ifnull函数可以判断返回值是否为‘null’,不为null时直接返回,为null时返回我们指定的‘0′

  select IFNULL(SUM(alert_sum),0) as alert_sum from tb_checkresults 

2) 将返回类型改为Integer

int是基本数据类型,默认值是0:integer是int的封装类,是一个类,默认值是null

  select SUM(alert_sum) as alert_sum from tb_checkresults 

找得到,看得懂,明确报错信息很重要。

解决问题有多种办法,符合要求的才是最好的。

以上就是mybatis 返回Integer,Double,String等类型的数据操作的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mybatis 返回Integer,Double,String等类型的数据操作

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址