溫馨提示×

溫馨提示×

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

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

Mybatis 返回 One 和 List 的區(qū)分方式

發(fā)布時間:2021-07-07 16:31:10 來源:億速云 閱讀:179 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要介紹“Mybatis 返回 One 和 List 的區(qū)分方式”,在日常操作中,相信很多人在Mybatis 返回 One 和 List 的區(qū)分方式問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Mybatis 返回 One 和 List 的區(qū)分方式”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

返回一個對象

官網(wǎng)示例:<T> T selectOne(String statement)

結(jié)果: 可能為 null。

注: 使用返回一個對象時,若數(shù)據(jù)庫查詢結(jié)果超過一條 record,Mybatis 則拋出非檢查類異常 TooManyResultsException eg. : org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2 證明查詢出兩條

返回一個 List

官網(wǎng)示例:<E> List<E> selectList(String statement)

結(jié)果: 如果沒有返回值,那么就是一個空的 list,不會出現(xiàn) null 的情況,所以使用時不用進(jìn)行判空,直接循環(huán)處理即可。

官網(wǎng)對接口定義的解釋:

The difference between selectOne and selectList is only in that selectOne must return exactly one object or null (none). If any more than one, an exception will be thrown. If you don't' know how many objects are expected, use selectList. If you want to check for the existence of an object, you're better off returning a count (0 or 1). The selectMap is a special case in that it is designed to convert a list of results into a Map based on one of the properties in the resulting objects. Because not all statements require a parameter, these methods are overloaded with versions that do not require the parameter object. The value returned by the insert, update and delete methods indicate the number of rows affected by the statement.

<T> T selectOne(String statement)
<E> List<E> selectList(String statement)
<K,V> Map<K,V> selectMap(String statement, String mapKey)
int insert(String statement)
int update(String statement)
int delete(String statement)

到此,關(guān)于“Mybatis 返回 One 和 List 的區(qū)分方式”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

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

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

AI