久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

解決Mybatis的@Param()注解導(dǎo)致分頁(yè)失效的問(wèn)題

瀏覽:8日期:2023-10-19 14:21:57

@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()注解導(dǎo)致分頁(yè)失效的問(wèn)題

【mybatis原碼:@Param將參數(shù)封裝到ParamMap】

跟蹤源碼進(jìn)入:org.apache.ibatis.binding.MapperMethod.class

解決Mybatis的@Param()注解導(dǎo)致分頁(yè)失效的問(wèn)題

進(jìn)入executeForMany方法:

解決Mybatis的@Param()注解導(dǎo)致分頁(yè)失效的問(wèn)題

進(jìn)入convertArgsToSqlCommandParam方法,可以看到參數(shù)封裝到ParamMap中:

解決Mybatis的@Param()注解導(dǎo)致分頁(yè)失效的問(wèn)題

解決辦法 不使用@Param注解:在傳遞多個(gè)參數(shù)(或是多個(gè)javaBean)時(shí),可以使用一個(gè)包含page屬性的實(shí)體類(lèi)進(jìn)行封裝 使用@Param注解:根據(jù)需求修改BaseInterceptor類(lèi)中的convertParameter方法,使得解析page對(duì)象不為null即可

到此這篇關(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)文章:
主站蜘蛛池模板: 东源县| 屯留县| 绥滨县| 景洪市| 西丰县| 通化市| 花莲市| 甘肃省| 宜宾市| 沭阳县| 泾川县| 龙陵县| 蒙阴县| 澳门| 兰州市| 清远市| 陵川县| 竹溪县| 阳曲县| 团风县| 会泽县| 敖汉旗| 安溪县| 增城市| 永平县| 且末县| 扎兰屯市| 利津县| 永州市| 夏河县| 彭水| 札达县| 德阳市| 博罗县| 桐城市| 灵石县| 宜川县| 潢川县| 抚松县| 英德市| 巨鹿县|