今天第一天接觸iBatis,沒有系統學習過,遇到了一個簡單卻鬧心的錯誤:用iBatis查詢數據庫中某個表的多列結果作為一個對象返回時,會出現對象的部分屬性為null值得錯誤。例如,查詢用戶表中的用戶ID,用戶名,用戶密碼,並將查詢的結果復制給類User的對象:
XML中的SQL語句為:
1 select id,username,password from usertable where username=#userName# and password=#userPwd#
將查詢的結果輸出:
在網上查了很長時間的資料,各種說法都有,最終看到http://www.educity.cn/wenda/369385.html中的回答,嘗試更改SQL語句:
1 select id as id,username as userName,password as userPwd from usertable where username=#userName# and password=#userPwd#
也就是將查詢的結果都起了別名,而這些別名也不是隨便起的,別名對應於User類的屬性,於是終於成功查到結果: