springboot 分页查询

1.查询总条数和数据集合

package cn.xx.basic.utils;

import lombok.Data;

import java.util.List;
@Data
public class PageResult<T> {
    private Long total;
    private List<T> data;

    public PageResult(Long total, List<T> data) {
        this.total = total;
        this.data = data;
    }
    public PageResult() {
    }
}

2.controller层

@PostMapping
    public PageResult<Department> findAll(@RequestBody DepartmentQuery query){

        return departmentService.queryPage(query);
    }

3.service

package cn.xx.basic.service;

import cn.x.basic.query.Basequery;
import cn.x.basic.utils.PageResult;

import java.util.List;

public interface IBaseService<T> {
    List<T> loadAll ();
    T loadOne(Long id);
    void remove(Long id);
    void add(T t);
    void update(T t);
    //分页查询
    PageResult<T> queryPage(Basequery query);

}

4.serviceImpl

package cn.x.basic.service.impl;

import cn.x.basic.mapper.BaseMapper;
import cn.x.basic.query.Basequery;
import cn.x.basic.service.IBaseService;
import cn.x.basic.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    public PageResult<T> queryPage(Basequery query) {
    //注意:在service分页返回的是数据为PageResult<T>,而mapper层返回的是List<T>
        List<T> list = null;
        //1.查询总条数
        Long count = baseMapper.queryCount(query);
        if(count !=0 || count !=null){
            //2.查询页面分页的数据
            list = baseMapper.queryPage(query);
        }
        return new PageResult<>(count,list);
    }

}

5.mapper

package cn.x.basic.mapper;

import cn.x.basic.query.Basequery;

import java.util.List;

public interface BaseMapper<T> {
    List<T> loadAll();
    T loadOne(Long id);
    void remove(Long id);
    void add(T t);
    void update(T t);

    //分页高级查询
    Long queryCount(Basequery Query);//查询总条数
    //分页的数据
    List<T> queryPage(Basequery Query);
}

6.mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.itsource.org.mapper.DepartmentMapper">
    <!--List<Department> loadAll ();-->
    <select id="loadAll" resultType="Department">
        SELECT * FROM t_department
    </select>
    <!--Department loadOne(Long id);-->
    <select id="loadOne" resultType="Department">
        SELECT * FROM t_department WHERE id = #{id}
    </select>
    <!--void remove (Long id);-->
    <delete id="remove">
        DELETE FROM t_department WHERE id = #{id}
    </delete>
    <!--void add(Department department);-->
    <insert id="add" >
        INSERT INTO t_department(sn, name, dirPath, state,manager_id)
        VALUES (#{sn},#{name},#{dirPath},#{state},#{manager.id})
    </insert>
    <!--void update(Department department);-->
    <update id="update">
        UPDATE t_department SET sn=#{sn},name=#{name},dirPath=#{dirPath},state=#{state},manager_id=#{manager.id}
        WHERE id = #{id}
    </update>

    <!--//分页,高级查询
    Long queryCount();//查询总条数-->
    <select id="queryCount" resultType="long">

        SELECT COUNT(*) FROM t_department d
        <include refid="wheresql"/>
    </select>

    <resultMap id="resultDept" type="Department">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="sn" property="sn"></result>
        <result column="dirPath" property="dirPath"></result>
        <result column="state" property="state"></result>
        <!--关联属性的封装-->
        <association property="manager" javaType="Employee">
            <id column="eid" property="id"></id>
            <result column="ename" property="username"></result>
        </association>
    </resultMap>
    <!--//分页的数据
    List<Department> queryPage(DepartmentQuery Query);-->
    <select id="queryPage" resultMap="resultDept">
        SELECT  d.* ,e.username ename,e.id eid FROM t_department d LEFT JOIN t_employee e on d.manager_id=e.id
        <include refid="wheresql"/>
        limit #{begin},#{pageSize}
    </select>
    <sql id="wheresql">
        <where>
            <if test="keywords !=null and keywords != ''">
                AND d.name LIKE concat("%",#{keywords},"%")
            </if>
        </where>
    </sql>

    
</mapper>

7.前端vue发送请求
记得写vue模板

<template>
    
</template>

<script>
    export default {
        data() {
            return {
                departments:[],
                listLoading: true,
                total:0,
                currPage:1,
                pageSize:5,
            }
        },
        methods:{
            //加载列表数据
            loadListData(){
                //组装请求的数据
                let param = {
                    currentPage:this.currPage,
                    pageSize:this.pageSize,
                    keywords:this.filters.name
                }
                //发送axios 请求
                this.$http.post("/dept",param).then((res)=>{
                    //给上面的departments赋值
                    console.debug(res);
                    this.departments = res.data.data;
                    this.total =res.data.total;
                    //关闭加载效果
                    this.listLoading=false;
                })
            }
        },
        mounted(){
            this.loadListData();
        }
    }
</script>

<style scoped>

</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值