时间:2021-10-18 10:05:33 | 栏目:JAVA代码 | 点击:次
@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 id="getByNidAndEntity" 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>
【关键原因】

【mybatis原码:@Param将参数封装到ParamMap】
跟踪源码进入:org.apache.ibatis.binding.MapperMethod.class

进入executeForMany方法:

进入convertArgsToSqlCommandParam方法,可以看到参数封装到ParamMap中:

BaseInterceptor类中的convertParameter方法,使得解析page对象不为null即可