溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Double與String類型的數(shù)據(jù)如何利用mybatis 實(shí)現(xiàn)返回

發(fā)布時(shí)間:2020-11-25 14:37:19 來(lái)源:億速云 閱讀:1063 作者:Leah 欄目:開發(fā)技術(shù)

Double與String類型的數(shù)據(jù)如何利用mybatis 實(shí)現(xiàn)返回?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

在使用mybatis的過(guò)程中會(huì)遇到只返回單獨(dú)數(shù)據(jù)類型的問(wèn)題會(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ù)庫(kù)中查詢數(shù)據(jù),用Int接收,因?yàn)閿?shù)據(jù)庫(kù)沒(méi)有數(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 *********************省略以下那些沒(méi)用的*********************

解決辦法:

(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>

看完上述內(nèi)容,你們掌握Double與String類型的數(shù)據(jù)如何利用mybatis 實(shí)現(xiàn)返回的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI