mybatis自定义sql实现使用Ipage和page进行分页

转发
https://blog.csdn.net/m0_56231256/article/details/121595199

其实就是两个步骤。
1.mapper方法中需要传入一个分页的对象 Ipage
2.mapper方法返回也是一个分页对象。

  1. 例如。entity 类是 User对象。
class User{
   int id;
   String name;
   // setter and getter method
}
  1. mapper定义的方法是
public Ipage<User> getUserPageInfo(page<T> page);

3.service类中进行调用即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-Plus 中,我们可以通过自定义 XML 文件来实现复杂的 SQL 操作,包括分页查询。下面是一个示例,演示如何使用自定义 XML 文件实现 Mybatis-Plus 分页查询: ```xml <!-- Mybatis 自定义 XML 文件 --> <mapper namespace="com.example.mapper.UserMapper"> <select id="selectUserList" resultType="com.example.entity.User"> select * from user <where> <if test="name != null and name != ''"> and name like concat('%', #{name}, '%') </if> </where> </select> <!-- 分页查询 --> <select id="selectUserPage" resultMap="userResultMap"> select * from user <where> <if test="name != null and name != ''"> and name like concat('%', #{name}, '%') </if> </where> <if test="page != null"> limit #{page.current}, #{page.size} </if> </select> </mapper> ``` 其中,`selectUserList` 是一个普通的查询语句,`selectUserPage` 则是一个带有分页参数的查询语句。在分页查询中,我们需要使用 `limit` 关键字来控制返回数据的数量,同时,我们也需要将分页信息传递给 SQL 语句。这里,我们通过 Mybatis 的参数处理机制来实现,在 XML 文件中使用 `${paramName}` 取出参数值。 接下来,我们需要在 Java 代码中调用该查询语句,从而实现分页查询。示例代码如下: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Override public IPage<User> getUserPage(Page<User> page, String name) { return baseMapper.selectUserPage(page, name); } } ``` 在 Service 实现类中,我们通过 `baseMapper` 来调用自定义 SQL 语句,其中 `selectUserPage` 方法返回一个 `IPage<User>` 对象,表示查询结果。最后,我们将该对象返回给控制器,完成分页查询。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值