mybatis association表关联与rowbounds共同使用时的异常及其解决方案
生活随笔
收集整理的這篇文章主要介紹了
mybatis association表关联与rowbounds共同使用时的异常及其解决方案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
按照mybatis手冊中所說的,association有兩種實現方式,嵌套查詢和嵌套結果映射。如手冊中所述,select方式會帶來N+1次查詢的問題,考慮到效率問題的話建議使用嵌套結果映射。但是在結合使用rowbounds進行分頁的時候嵌套結果映射會報Mapped Statements with nested result mapping cannot be safely constrained by rowbounds異常。經過測試發現是rowbounds和resultmap-association之間有沖突,魚與熊掌不可兼得的話,我想最好還 是選擇放棄rowbounds。畢竟可以在sql語句里面加入變量來實現分頁。
解決方案: 新建一個RowBoundCapsule類,將原來的查詢參數和limit、offset封裝到一起,并采用如下的方式改寫mapper文件: <select id="selectByOwner" parameterType="int" resultMap="topicresultmap" resultSetType="FORWARD_ONLY">select t.tid as tid, t.uid as tuid, t.content as content, t.commentcount as commentcount, t.pptime as pptime,
u.uid as uid, u.email as email, u.nickname as nickname, u.login as login, u.pass as pass, u.pic as pic
from topic as t LEFT JOIN user as u on t.uid = u.uid
where t.uid = #{o} limit #{offset},#{limit}
</select>
#{o}代表原來的參數。這樣就可以把分頁的任務交給數據庫來完成了。
?
=====================
或者不要rowbounds了,直接傳offset和pageSize到dao中,然后在sql后加limit #{offset},#{limit}
?總結
以上是生活随笔為你收集整理的mybatis association表关联与rowbounds共同使用时的异常及其解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 变量的存储类别以及作用空间
- 下一篇: so 问题来了,你现在值多少钱?