解決Mybatis的@Param()注解導致分頁失效的問題
@Param注解導致分頁失效—分頁攔截器
問題描述在使用mybatis分頁時,使用@Param注解傳入了兩個對象,分頁失效,查詢出的總是全部的數據。出現問題時,分頁策略為:分頁攔截器實現的分頁【錯誤寫法】
service寫法:
public Page<Entity> getByNidAndEntity(Page<Entity> page,String nid,Entity entity){ entity.setPage(page); page.setList(dao.getByNidAndEntity(nid,entity)); return page;}
dao方法聲明:
List<Entity> getByNidAndEntity(@Param('nid') String nid,@Param('entity')Entity entity);
mapper.xml中的sql:
<select resultType='Entity'> select <include refid='entityColumns' /> from entity_table et left join other_table ot on et.id = ot.eid where ot.nid = #{nid} and et.name = #{entity.name} and et.remarks = #{entity.remarks}</select>原因解析
【關鍵原因】
根源問題在于:在PaginationInterceptor中,分頁對象Page被解析為null,導致的分頁失效 由于@Param會將參數封裝到ParamMap中,而page對象在實體類entity中,導致convertParameter方法返回的page對象為null【mybatis原碼:@Param將參數封裝到ParamMap】
跟蹤源碼進入:org.apache.ibatis.binding.MapperMethod.class
進入executeForMany方法:
進入convertArgsToSqlCommandParam方法,可以看到參數封裝到ParamMap中:
到此這篇關于解決Mybatis的@Param()注解導致分頁失效的問題的文章就介紹到這了,更多相關Mybatis分頁失效內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. DB2 Web 服務提供者的安全性(3)(1)2. mysql啟動時報錯 ERROR! Manager of pid-file quit without3. 如何手動刪除 SQL Server 2000 默認實例、命名實例或虛擬實例4. xp sp2 + sql server 2000 developer環境開1433端口5. 解決Mybatis中mapper的ID沖突問題6. SQL Server 2008通過端口1433連接到主機127.0.0.1的TCP/IP連接失敗7. 關于if exists的用法及說明8. DB2 Spatial Extender 性能調優(2)(1)9. MySQL性能優化之一條SQL在MySQL中執行的過程詳解10. Oracle數據遷移MySQL的三種簡單方法
