mybatis 返回Integer,Double,String等類型的數(shù)據(jù)操作
在使用mybatis的過程中會(huì)遇到只返回單獨(dú)數(shù)據(jù)類型的問題會(huì)用到resultType。
//返回Integer <select id="getSpeedByLinkId" parameterType="java.lang.String" resultType="Integer"> SELECT speed from dws_tfc_state_speed_link_last_rt where link_id = #{linkId} </select> //返回Double類型 <select id="getTravelTimeByLinkId" parameterType="java.lang.String" resultType="Double"> SELECT travel_time from dws_tfc_state_speed_link_last_rt where link_id = #{linkId} </select> //返回String 類型 <select id="getSpeedByLinkId" parameterType="java.lang.String" resultType="String"> SELECT speed from dws_tfc_state_speed_link_last_rt where link_id = #{linkId} </select>
補(bǔ)充知識(shí):mybatis下返回類型為int,結(jié)果為null時(shí)報(bào)tempted to return null from a method with a primitive return type (int).
背景了解:
從mysql數(shù)據(jù)庫中查詢數(shù)據(jù),用Int接收,因?yàn)閿?shù)據(jù)庫沒有數(shù)據(jù)所以返回null,于是運(yùn)行時(shí)報(bào)以下錯(cuò)誤,提取關(guān)鍵的信息“attempted to return null from a method with a primitive return type (int).”,翻譯成中文大概意思是“”嘗試從具有基本返回類型(Int)的方法返回null“返回int的方法想要返回null,不符合規(guī)矩。
報(bào)錯(cuò)信息:
xml中的SQL和報(bào)錯(cuò)信息如下:
<select id="getweekalert" resultType="int"> select SUM(alert_sum) as alert_sum from tb_checkresults </select>
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的函數(shù)ifnull
ifnull函數(shù)可以判斷返回值是否為‘null',不為null時(shí)直接返回,為null時(shí)返回我們指定的‘0'
<select id="getweekalert" resultType="int"> select IFNULL(SUM(alert_sum),0) as alert_sum from tb_checkresults </select>
(2) 將返回類型改為Integer
int是基本數(shù)據(jù)類型,默認(rèn)值是0:integer是int的封裝類,是一個(gè)類,默認(rèn)值是null
<select id="getweeekalert" resultType="Integer"> select SUM(alert_sum) as alert_sum from tb_checkresults </select>
找得到,看得懂,明確報(bào)錯(cuò)信息很重要。
解決問題有多種辦法,符合要求的才是最好的。
以上這篇mybatis 返回Integer,Double,String等類型的數(shù)據(jù)操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java web驗(yàn)證碼實(shí)現(xiàn)代碼分享
這篇文章主要為大家分享了java web驗(yàn)證碼的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06整理Java的MyBatis框架中一些重要的功能及基本使用示例
這篇文章主要介紹了Java的MyBatis框架中一些重要的功能及基本使用示例整理,MyBatis可以幫助Java程序進(jìn)行強(qiáng)大的數(shù)據(jù)庫操作,需要的朋友可以參考下2016-04-04Spring Boot整合FTPClient線程池的實(shí)現(xiàn)示例
這篇文章主要介紹了Spring Boot整合FTPClient線程池的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12springboot中rabbitmq實(shí)現(xiàn)消息可靠性機(jī)制詳解
這篇文章主要介紹了springboot中rabbitmq實(shí)現(xiàn)消息可靠性機(jī)制詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Java結(jié)合redis實(shí)現(xiàn)接口防重復(fù)提交
本文主要介紹了Java結(jié)合redis實(shí)現(xiàn)接口防重復(fù)提交,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09在RabbitMQ中實(shí)現(xiàn)Work queues工作隊(duì)列模式
這篇文章主要介紹了如何在RabbitMQ中實(shí)現(xiàn)Work queues模式,代碼詳細(xì),解釋清晰,可以幫助大家更好理解java,對(duì)這方面感興趣的朋友可以參考下2021-04-04