解決Mybatis的@Param()注解導(dǎo)致分頁(yè)失效的問(wèn)題
@Param注解導(dǎo)致分頁(yè)失效—分頁(yè)攔截器
問(wèn)題描述在使用mybatis分頁(yè)時(shí),使用@Param注解傳入了兩個(gè)對(duì)象,分頁(yè)失效,查詢出的總是全部的數(shù)據(jù)。出現(xiàn)問(wèn)題時(shí),分頁(yè)策略為:分頁(yè)攔截器實(shí)現(xiàn)的分頁(yè)【錯(cuò)誤寫(xiě)法】
service寫(xiě)法:
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)鍵原因】
根源問(wèn)題在于:在PaginationInterceptor中,分頁(yè)對(duì)象Page被解析為null,導(dǎo)致的分頁(yè)失效 由于@Param會(huì)將參數(shù)封裝到ParamMap中,而page對(duì)象在實(shí)體類(lèi)entity中,導(dǎo)致convertParameter方法返回的page對(duì)象為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)致分頁(yè)失效的問(wèn)題的文章就介紹到這了,更多相關(guān)Mybatis分頁(yè)失效內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. access不能打開(kāi)注冊(cè)表關(guān)鍵字錯(cuò)誤處理方法(80004005錯(cuò)誤)2. MariaDB性能調(diào)優(yōu)工具mytop的使用詳解3. SQL案例學(xué)習(xí)之字符串的合并與拆分方法總結(jié)4. SQL Server一個(gè)字符串拆分多行顯示或者多行數(shù)據(jù)合并成一個(gè)字符串5. MariaDB的安裝與配置教程6. Window7安裝MariaDB數(shù)據(jù)庫(kù)及系統(tǒng)初始化操作分析7. DB2大事記8. Centos7 下mysql重新啟動(dòng)MariaDB篇9. MariaDB數(shù)據(jù)庫(kù)的外鍵約束實(shí)例詳解10. 用SQL語(yǔ)句查找Access中某表是否存在的小技巧
