mybatis与MP的分页查询

mybatis的分页

编辑ItemController

@RestController
@RequestMapping("/item")
public class ItemController {
    
    @Autowired
    private ItemService itemService;
    
    /**
     * 业务分析:
     *  根据分页要求,动态查询商品全部记录信息.
     *  url:http://localhost:8091/item/query?page=1&rows=40
     *  参数说明:  page 当前页
     *            rows  每页的行数
     *  返回值: EasyUITable JSON串.
     *  
     *  1.手写分页
     *  2.MP动态分页
     */
    @RequestMapping("/query")
    public EasyUITable findItemByPage(Integer page,Integer rows) {
        
        return itemService.findItemByPage(page,rows);
    }
}

编辑ItemService

@Service
public class ItemServiceImpl implements ItemService {
    
    @Autowired
    private ItemMapper itemMapper;/**
     * 1.以结果为导向
     * 
     * 数组取值:含头不含尾
     * 分页查询:
     *  SELECT * FROM tb_item LIMIT 起始位置,查询行数
        第一页: SELECT * FROM tb_item LIMIT 0,20    共21个记录    0-19下标
        第二页: SELECT * FROM tb_item LIMIT 20,20           20-39下标        
        第三页: SELECT * FROM tb_item LIMIT 40,20           40-59下标
        第N页:  SELECT * FROM tb_item LIMIT (page-1)*rows,rows
     */
    @Override
    public EasyUITable findItemByPage(Integer page, Integer rows) {
        
        //1.total 表示商品查询的记录总数  不需要where条件
        int total = itemMapper.selectCount(null);
        //2.rowsItem 每页展现的记录  需要使用分页操作
        int start = (page-1)* rows;
        List<Item> itemList = itemMapper.findItemByPage(start,rows);
        
        return new EasyUITable(total, itemList);
    }
    
}

编辑ItemMapper

public interface ItemMapper extends BaseMapper<Item>{
​
    List<Item> findItemByPage(Integer start, Integer rows);
    
}
<mapper namespace="com.jt.mapper.ItemMapper">
    
    <select id="findItemByPage" resultType="Item">
        SELECT * FROM tb_item ORDER BY updated DESC LIMIT #{start},#{rows}
    </select>
    
</mapper>

MP方式实现分页查询

编辑MP的拦截器

//标识配置类 
@Configuration   //相当于.xml的配置文件
public class MybatisPlusConfig {
    
    /**
     * 将方法的返回值的结果,交给Spring容器管理
     * Spring内部封装了一个Map集合管理实例化的对象.
     * Map<key,value>
     *      key: bean的ID    方法名称paginationInterceptor
     *      value: 实例化后的对象    new PaginationInterceptor()
     * 
     * 交给spring容器管理之后,如果需要直接注入即可.
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        
        //进行分页操作时,会被拦截器对象拦截,之后开始MP的分页
        return new PaginationInterceptor();
    }
    
}

编辑ItemController

@RequestMapping("/query")
    public EasyUITable findItemByPage(Integer page,Integer rows) {
        
        return itemService.findItemByPage(page,rows);
    }

编辑ItemService

@Override
    public EasyUITable findItemByPage(Integer page, Integer rows) {
        //使用MP的方式进行分片 满足mp的规则  传递查询的参数
        Page<Item> iPage = new Page<>(page, rows);
        //条件:根据updated进行排序
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("updated");
        //将分页查询的结果封装个Page对象
        Page<Item> itemPage = itemMapper.selectPage(iPage, queryWrapper);
        //从分页对象中动态取值.
        int total = (int) itemPage.getTotal();          //获取记录总数
        List<Item> itemList = itemPage.getRecords();    //获取当前分页后的记录
        return new EasyUITable(total, itemList);
    }
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值