在資料庫中查詢多個欄位的時候,不能用物件接受
阿新 • • 發佈:2018-12-20
今天在做查詢從資料庫中查詢兩個欄位的問題的時候不知道是應該用物件接受,還是集合接受,開始的時候用物件接受
service層介面:
User userUpdateByAccount(@Param("account") String account, @Param("phone") String phone);
service實現層:
@Override
public User userUpdateByAccount(String account,String phone) {
return mapper.userUpdateByAccount(account,phone);
}
dao層介面:
User userUpdateByAccount(@Param("account") String account, @Param("phone") String phone);
mapper.xml
<select id="userUpdateByAccount" resultType="user">
SELECT account,phone from user where account=#{account} or phone=#{phone} (在mapper.xml檔案中如果用or最少查詢0條,最多查詢出來兩條,但是如果用and結果最少0條,最多1條)
</select>
結果測試查詢出來報異常:
TooManyResultsException異常,原因是因為查詢多條物件,只用一個物件結果,結果太多就會有異常發生。
所以應該用物件集合來接收
具體修改:
service層介面:
List<User> userUpdateByAccount(@Param("account") String account, @Param("phone") String phone);
service實現層:
@Override
public List<User> userUpdateByAccount(String account,String phone) {
return mapper.userUpdateByAccount(account,phone);
}
dao層介面:
List<User> userUpdateByAccount(@Param("account") String account, @Param("phone") String phone);
用物件集合接受就不會出現異常
開發中遇到的問題,積累一下