mybatisPlus使用分页插件
1-使用xml写sql,如何使用mybatisPlus的分页插件进行分页。
@Configuration
@MapperScan("com.chenxixi.mapper")
public class MybatisPlusPageConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
//interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
return interceptor;
}
}
/**
* 分页查询 + 模糊查询
*
* @param pageNum
* @param pageSize
* @return
*/
@GetMapping("/pageTestXml")
public Result pageTestXml(@RequestParam Integer pageNum,
@RequestParam Integer pageSize,
@RequestParam(defaultValue = "") String adminName) {
IPage<Admin> userPageInfo = adminService.selectTestPage(pageNum, pageSize,adminName);
return Result.success(userPageInfo);
}
public interface IAdminService extends IService<Admin> {
IPage<Admin> selectTestPage(Integer pageNum, Integer pageSize, String adminName);
}
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService { @Override
public IPage<Admin> selectTestPage(Integer pageNum, Integer pageSize, String adminName) {
// (配置分页插件)创建Page对象,设置当前页,每页条数
// 传递给mapper层
Page<Admin> page = new Page<>(pageNum, pageSize);
// 调用mapper层方法,连着分页插件和 '关键字' 一起传入
IPage<Admin> pageList = this.baseMapper.selectTestPage(page, adminName);
return pageList;
}
}
@Mapper
public interface AdminMapper extends BaseMapper<Admin> {
//Page<Admin> page 传递来的分页插件
IPage<Admin> selectTestPage(Page<Admin> page,@Param("adminName") String adminName);
}
<select id="selectTestPage" resultType="com.chenxixi.entity.Admin">
select *
from admin
WHERE adminName LIKE CONCAT('%', #{adminName}, '%')
</select>