解决Mybatis的@Param()注解导致分页失效的问题
作者:小妖云汐 时间:2022-05-01 13:24:53
@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>
原因解析
【关键原因】
根源问题在于:在PaginationInterceptor中,分页对象Page被解析为null,导致的分页失效
由于@Param会将参数封装到ParamMap中,而page对象在实体类entity中,导致convertParameter方法返回的page对象为null
【mybatis原码:@Param将参数封装到ParamMap】
跟踪源码进入:org.apache.ibatis.binding.MapperMethod.class
进入executeForMany方法:
进入convertArgsToSqlCommandParam方法,可以看到参数封装到ParamMap中:
解决办法
不使用@Param注解:在传递多个参数(或是多个javaBean)时,可以使用一个包含page属性的实体类进行封装
使用@Param注解:根据需求修改
BaseInterceptor
类中的convertParameter
方法,使得解析page
对象不为null即可
来源:https://blog.csdn.net/qq_42002006/article/details/114368309
标签:Mybatis,@Param(),注解,分页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
同时使用@LoadBalanced @RefreshScope注解负载均衡失效分析
2023-12-07 10:59:24
![](https://img.aspxhome.com/file/2023/7/62037_0s.png)
MyBatis使用动态SQL标签的小陷阱
2023-09-11 04:42:57
JAVA中数组从小到大排序的2种方法实例
2021-10-09 09:46:18
Java 数据结构深入理解ArrayList与顺序表
2023-02-15 14:24:07
![](https://img.aspxhome.com/file/2023/1/62501_0s.png)
Spring Bean生命周期之BeanDefinition的合并过程详解
2023-11-29 02:50:35
HashMap的get()方法的NullPointerException问题
2023-09-23 07:24:12
基于C语言string函数的详解
2023-06-28 05:33:25
Spring4如何自定义@Value功能详解
2021-12-16 06:31:12
Java如何根据不同系统动态获取换行符和盘分割符
2022-02-27 10:33:10
四种引用类型在JAVA Springboot中的使用详解
2023-11-24 03:34:38
![](https://img.aspxhome.com/file/2023/4/60554_0s.png)
Kafka 日志存储实现过程
2021-11-01 05:04:00
![](https://img.aspxhome.com/file/2023/7/62337_0s.png)
Java与C++实现相同的MD5加密算法简单实例
2023-08-31 09:43:02
![](https://img.aspxhome.com/file/2023/2/60892_0s.jpg)
Java转JSON串的几种方式
2023-08-24 07:38:47
SpringBoot基于Swagger2构建API文档过程解析
2022-12-06 20:42:58
![](https://img.aspxhome.com/file/2023/5/62205_0s.png)
Java实现提取QSV文件视频内容
2023-08-24 13:33:45
轻松掌握Java迭代器模式
2023-11-29 03:15:22
Java发送报文与接收报文的实例代码
2023-07-09 04:44:01
Java Scala实现数据库增删查改操作详解
2022-02-03 05:09:22
![](https://img.aspxhome.com/file/2023/0/61830_0s.png)
基于JavaMail的Java邮件发送
2022-10-30 18:56:56
![](https://img.aspxhome.com/file/2023/1/60931_0s.jpg)
Android切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题的解决方法
2023-07-21 10:51:45