解決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分頁失效內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
