使用 MyBatis-Plus 的分页插件来简化操作

文章介绍了如何使用MyBatis-Plus的分页插件简化数据库查询操作。首先,在application.yml配置文件中添加分页插件配置,然后在Mapper接口中使用Page对象作为参数进行分页查询。接着,在Mapper.xml文件中编写对应的SQL语句,再在Service接口和实现类中定义及实现分页查询方法,最后在Controller层调用这些方法完成分页查询功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用 MyBatis-Plus 的分页插件来简化操作。需要在配置文件中添加分页插件的配置,然后在 Mapper 接口中使用 Page 对象作为参数,就可以实现分页查询。具体的步骤如下:

  • 在 application.yml 文件中添加分页插件的配置,如下:
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true # 开启驼峰命名
  global-config:
    db-config:
      id-type: auto # 主键策略
  mapper-locations: classpath:mapper/*.xml # mapper 文件位置
  type-aliases-package: com.example.entity # 实体类包名
  plugins:
    - com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor # 分页插件
  • 在 AuditInfoMapper 接口中使用 Page 对象作为参数,如下:
public interface AuditInfoMapper extends BaseMapper<AuditInfo> {
  // 分页查询审核状态信息
  IPage<AuditInfo> selectPage(Page<AuditInfo> page, @Param("type") String type, @Param("id") Integer id);
}
  • 在 AuditInfoMapper.xml 文件中编写对应的 SQL 语句,如下:
<mapper namespace="com.example.mapper.AuditInfoMapper">
  <!-- 分页查询审核状态信息 -->
  <select id="selectPage" resultType="com.example.entity.AuditInfo">
    SELECT * FROM audit_info
    WHERE type = #{type} AND id = #{id}
  </select>
</mapper>
  • 在 AuditInfoService 接口中定义分页查询审核状态信息的方法,如下:
public interface AuditInfoService extends IService<AuditInfo> {
  // 分页查询审核状态信息
  IPage<AuditInfo> selectPage(Page<AuditInfo> page, String type, Integer id);
}
  • 在 AuditInfoServiceImpl 类中实现分页查询审核状态信息的方法,如下:
@Service
public class AuditInfoServiceImpl extends ServiceImpl<AuditInfoMapper, AuditInfo> implements AuditInfoService {
  @Override
  public IPage<AuditInfo> selectPage(Page<AuditInfo> page, String type, Integer id) {
    return baseMapper.selectPage(page, type, id);
  }
}
  • 在 Controller 层中调用 AuditInfoService 的分页查询审核状态信息的方法,如下:
@RestController
@RequestMapping("/audit")
public class AuditController {
  @Autowired
  private AuditInfoService auditInfoService;

  // 分页查询审核状态信息
  @GetMapping("/page")
  public IPage<AuditInfo> selectPage(@RequestParam("type") String type, @RequestParam("id") Integer id,
                                     @RequestParam(value = "current", defaultValue = "1") Integer current,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size) {
    Page<AuditInfo> page = new Page<>(current, size);
    return auditInfoService.selectPage(page, type, id);
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值