解決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ò)誤寫法】
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)鍵原因】
根源問(wèn)題在于:在PaginationInterceptor中,分頁(yè)對(duì)象Page被解析為null,導(dǎo)致的分頁(yè)失效 由于@Param會(huì)將參數(shù)封裝到ParamMap中,而page對(duì)象在實(shí)體類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數(shù)據(jù)庫(kù)安全的幾個(gè)問(wèn)題2. Windwos下MySQL 64位壓縮包的安裝方法學(xué)習(xí)記錄3. mysql-bin.000001文件的來(lái)源及處理方法4. 數(shù)據(jù)庫(kù)相關(guān)的幾個(gè)技能:ACCESS轉(zhuǎn)SQL5. mysql命令行客戶端結(jié)果分頁(yè)瀏覽6. mysql啟動(dòng)時(shí)報(bào)錯(cuò) ERROR! Manager of pid-file quit without7. ACCESS轉(zhuǎn)SQL數(shù)據(jù)庫(kù)相關(guān)的幾個(gè)技能9. MySQL 性能、監(jiān)控與災(zāi)難恢復(fù)10. MySQL 5.0 發(fā)布

網(wǎng)公網(wǎng)安備