Layui + PageHelper 分页查询

Layui + PageHelper 分页查询,第1张

Layui的分页查询

layui向后端发请求,url后面会自动带上 page=1&limit=10

传统的分页查询
select * from 表名 limit m, n 从第m+1行开始取n条数据
使用PageHelper

导入maven依赖:

<dependency>
  <groupId>com.github.pagehelpergroupId>
  <artifactId>pagehelperartifactId>
  <version>5.1.2version>
dependency>
<dependency>
  <groupId>com.github.jsqlparsergroupId>
  <artifactId>jsqlparserartifactId>
  <version>1.0version>
dependency>

MyBatis配置文件:

<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
    plugin>
plugins>

如果pagehelper的版本在5.0以下,要写成:

<plugins>
    <plugin interceptor="com.github.pagehelper.PageHelper">
        <property name="helperDialect" value="mysql" />
    plugin>
plugins>

controller层:

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAllUser")
    @ResponseBody
    public DataVo<User> findAllUser(Integer page, Integer limit) {
        //page和limit是从前端发过来的
        PageHelper.startPage(page, limit);
        List<User> userList = userService.findAllUser(); 

        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        DataVo<User> dataVo = new DataVo<User>();
        dataVo.setCode(0);
        dataVo.setCount((int) pageInfo.getTotal());
        dataVo.setMsg("success");
        dataVo.setData(pageInfo.getList());

        return dataVo;
    }
}

注意:PageHelper.startPage() 下面紧跟着需要分页查询的语句

使用pagehelper就不用在sql语句中写limit了,查询出所有数据即可,pagehelper会自动分页

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/719795.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存