解決Mybatis的@Param()注解導(dǎo)致分頁失效的問題
@Param注解導(dǎo)致分頁失效—分頁攔截器
問題描述在使用mybatis分頁時,使用@Param注解傳入了兩個對象,分頁失效,查詢出的總是全部的數(shù)據(jù)。出現(xiàn)問題時,分頁策略為:分頁攔截器實現(xiàn)的分頁【錯誤寫法】
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>原因解析
【關(guān)鍵原因】
根源問題在于:在PaginationInterceptor中,分頁對象Page被解析為null,導(dǎo)致的分頁失效 由于@Param會將參數(shù)封裝到ParamMap中,而page對象在實體類entity中,導(dǎo)致convertParameter方法返回的page對象為null【mybatis原碼:@Param將參數(shù)封裝到ParamMap】
跟蹤源碼進(jìn)入:org.apache.ibatis.binding.MapperMethod.class
進(jìn)入executeForMany方法:
進(jìn)入convertArgsToSqlCommandParam方法,可以看到參數(shù)封裝到ParamMap中:
到此這篇關(guān)于解決Mybatis的@Param()注解導(dǎo)致分頁失效的問題的文章就介紹到這了,更多相關(guān)Mybatis分頁失效內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 關(guān)于Oracle中sqlldr的用法大全2. 淺談如何將Oracle導(dǎo)出到XML文件3. 使用SQL語句快速獲取SQL Server數(shù)據(jù)字典4. 探討SQL Server 2005.NET CLR編程5. Microsoft SQL Server 查詢處理器的內(nèi)部機(jī)制與結(jié)構(gòu)(1)6. Oracle數(shù)據(jù)庫中的字符處理技巧總結(jié)7. Mybatis 將table表名作為參數(shù)傳入操作8. SQL Server中的數(shù)據(jù)類型詳解9. MySQL InnoDB ReplicaSet(副本集)簡單介紹10. Mybatis Limit實現(xiàn)分頁功能
