分页查询和模糊查询前后端代码

前端代码

<template>
  <div class="home">
    <el-container style="height: 100%;">
      <!--左侧边栏-->
      <el-aside :width="sideWidth + 'px'" style="background-color: rgb(238, 241, 246);height: 100%; box-shadow:2px 0 6px rgb(0, 21, 41, 0.35)">
        <el-menu :default-openeds="['1', '3']" style="height: 100%;background-color: rgb(48, 65, 86);overflow-x: hidden"
                 default-active="2"
                 background-color="#304156FF"
                 text-color="#fff"
                 active-text-color="#ffd04b"
                 :collapse-transition="false"
                 :collapse="isCollapse"
                 class="el-menu-vertical-demo"
        >
          <div style="height: 60px;line-height: 60px;text-align: center">
            <img src="../assets/logo.png" alt="" style="width: 20px;position: relative;top: 5px;margin-right: 5px"></img>
            <b style="color: #fff" v-show="logoTextShow">后台管理系统</b>
          </div>
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-message"></i>
              <span slot="title">导航一</span>
            </template>
            <el-menu-item-group>
              <template slot="title">分组一</template>
              <el-menu-item index="1-1">选项1</el-menu-item>
              <el-menu-item index="1-2">选项2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="分组2">
              <el-menu-item index="1-3">选项3</el-menu-item>
            </el-menu-item-group>
            <el-submenu index="1-4">
              <template slot="title">选项4</template>
              <el-menu-item index="1-4-1">选项4-1</el-menu-item>
            </el-submenu>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span slot="title">导航二</span>
            </template>
            <el-menu-item-group>
              <template slot="title">分组一</template>
              <el-menu-item index="2-1">选项1</el-menu-item>
              <el-menu-item index="2-2">选项2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="分组2">
              <el-menu-item index="2-3">选项3</el-menu-item>
            </el-menu-item-group>
            <el-submenu index="2-4">
              <template slot="title">选项4</template>
              <el-menu-item index="2-4-1">选项4-1</el-menu-item>
            </el-submenu>
          </el-submenu>
          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-setting"></i>
              <span slot="title">导航三</span>
            </template>
            <el-menu-item-group>
              <template slot="title">分组一</template>
              <el-menu-item index="3-1">选项1</el-menu-item>
              <el-menu-item index="3-2">选项2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="分组2">
              <el-menu-item index="3-3">选项3</el-menu-item>
            </el-menu-item-group>
            <el-submenu index="3-4">
              <template slot="title">选项4</template>
              <el-menu-item index="3-4-1">选项4-1</el-menu-item>
            </el-submenu>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右边-->
      <el-container>
        <!--头部-->
        <el-header style="font-size: 12px; border-bottom: 1px solid #ccc; line-height: 60px;display: flex">
          <div style="flex: 1;font-size: 20px">
            <span :class="collapseBtnClass" style="cursor: pointer" @click="collapse"></span>
          </div>
          <el-dropdown style="width: 70px;cursor: pointer">
            <span>王小虎<i class="el-icon-arrow-down" style="margin-left: 5px"></i></span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>个人信息</el-dropdown-item>
              <el-dropdown-item>退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        <!--主体-->
        <el-main>

          <div class="mg-10">
            <el-input v-model="username" style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入名称"></el-input>
            <el-input v-model="email" style="width: 200px" suffix-icon="el-icon-message" placeholder="请输入邮箱" class="ml-5"></el-input>
            <el-input v-model="address" style="width: 200px" suffix-icon="el-icon-position" placeholder="请输入地址" class="ml-5"></el-input>
            <el-button class="ml-5" type="primary" @click="findPage()">搜索</el-button>
          </div>

          <div class="mg-10">
            <el-button type="primary">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
            <el-button type="danger">批量删除 <i class="el-icon-remove-outline"></i></el-button>
            <el-button type="primary">导入 <i class="el-icon-bottom"></i></el-button>
            <el-button type="primary">导出 <i class="el-icon-top"></i></el-button>
          </div>

          <el-table :data="tableData" border stripe :header-cell-class-name="headerBg">
            <el-table-column prop="id" label="ID" width="120">
            </el-table-column>
            <el-table-column prop="username" label="用户名" width="80">
            </el-table-column>
            <el-table-column prop="password" label="密码" width="120">
            </el-table-column>
            <el-table-column prop="nickname" label="昵称" width="120">
            </el-table-column>
            <el-table-column prop="email" label="邮箱" width="200">
            </el-table-column>
            <el-table-column prop="phone" label="电话" width="120">
            </el-table-column>
            <el-table-column prop="address" label="地址" width="250">
            </el-table-column>
            <el-table-column label="操作" width="210">
              <template slot-scope="scope">
                <el-button type="success">编辑 <i class="el-icon-edit"></i></el-button>
                <el-button type="danger">删除 <i class="el-icon-remove-outline"></i></el-button>
              </template>
            </el-table-column>
          </el-table>

          <!-- 分页
              分页数据和表格数据产生了联动
              前端传的值:
                :current-page="pageNum" 页码
                :page-size="pageSize" 每页个数
              后端传的值:
                :total="total" 总条数
          -->
          <div style="padding: 10px 0">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[ 2, 5, 10, 15]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
          </div>
        </el-main>

      </el-container>
    </el-container>
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'Home',
  data() {
    return {
      tableData: [],
      total: 0, // 分页总条数
      pageNum: 1, // 页数
      pageSize: 2, // 条数
      username: "", // 查询绑定username
      email: "", // 查询绑定email
      address: "", // 查询绑定address
      msg: 'Hello Word',
      collapseBtnClass: 'el-icon-s-fold', // 收缩图标控制 默认展开
      isCollapse: false, // 点击收缩图标控制 默认是展开 false
      sideWidth: 200, // 导航栏宽度 动态 初始值是200
      logoTextShow: true, // 默认是显示的标题 当收缩是为false
      headerBg: "headerBg"
    }
  },
  created() {
    this.findPage()
  },
  methods: {
    collapse (){ // 点击收缩按钮时会触发按钮
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) { // 收缩
        this.sideWidth = 64
        this.collapseBtnClass = 'el-icon-s-unfold' // 缩起图标控制
        this.logoTextShow = false // 收缩是不显示文字
      }else { // 展开
        this.sideWidth = 200
        this.collapseBtnClass = 'el-icon-s-fold' // 展开图标控制
        this.logoTextShow = true // 显示文字
      }
    },
    handleSizeChange(pageSize) {
      console.log(`每页 ${pageSize} 条`);
      // 传给我们上面绑定的pageSize
      this.pageSize = pageSize
      // 请求数据,请求数据之后它会自动更新 pageSize
      this.findPage()
    },
    handleCurrentChange(pageNum) {
      console.log(`当前页: ${pageNum}`);
      // 传给我们上面绑定的pageNum
      this.pageNum = pageNum
      // 请求数据,请求数据之后它会自动更新 tableData
      this.findPage()
    },
    findPage() {
      // 请求分页查询数据 fetch api 有以下几个参数,第一个参数是url
      // .then 返回一个res,之后对他进行一个json的处理,因为他返回的res是一个字符串,需要把字符串转成json
      // 通过res.json()函数 这个方法,把他转成json,再.then 这个res就是json类型的了
      // 拼接url路径使用es6的语法:`http://localhost:8081/user/findPage/${pageNum}`
      fetch("http://localhost:8081/user/findPage?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&username="+this.username).then(res => res.json()).then(res => {
        console.log(res)
        this.tableData = res.data
        this.total = res.total
        // 查询后清空查询条件
        this.username = ""
        this.email = ""
        this.address = ""
      })
    }
  }
}
</script>

<style>
.home {
  height: 100%;
}
.el-menu-item,.el-menu-item-group, .el-submenu__title{
  background-color: rgb(48, 65, 86);
}
</style>

后端代码

    Controller层

@Autowired
SysUserMapper userMapper;
/**
 * @description: 分页查询 因为是查询多条数据所以是list
 * @param: pageNum、pageSize
 * @return: java.util.Map<java.lang.String,java.lang.Object>
 * @author ls
 * @date: 2022/4/18
 * 接口路径 /user/findPage?pageNum=1 & pageSize=10
 * @RequestParam 它接收 ?pageNum=1 & pageSize=10 这样的数据 它会把url参数里的pageNum和pageSize中的值(也就是1和10)映射到参数列表中去,这就是他的这个数据接收
 * (pageNum - 1) * pageSize = limit 第一个参数
 * pageSize 第二个参数
 */
@GetMapping("/findPage")
public Map<String , Object> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
    // 第一个参数 pageNum
    pageNum = (pageNum - 1) * pageSize;
    List<SysUser> data = userMapper.findPage(pageNum, pageSize);
    // 因为不知道总条数 所以查询出总条数
    Integer total = userMapper.selectTotal();
    // 封装一个map
    Map<String , Object> res = new HashMap<>();
    // 往map里面存放数据
    res.put("data", data);
    res.put("total", total);
    return res;
}

    Mapper层 

/** 
 * @description: 分页查询 因为是查询多条数据所以是list
 * @param: pageNum、pageSize
 * @return: java.util.List<com.boot.springboot.entity.SysUser> 
 * @author ls
 * @date: 2022/4/18 
 */ 
@Select("select * from sys_user limit #{pageNum}, #{pageSize}")
List<SysUser> findPage(@Param("pageNum") Integer pageNum,@Param("pageSize") Integer pageSize);

/** 
 * @description: 因为不知道总条数 所以查询出总条数 
 * @param:  
 * @return: java.lang.Integer 
 * @author ls
 * @date: 2022/4/18 
 */ 
@Select("select count(*) from sys_user")
Integer selectTotal();

     这里省略了service和它的实现类

 

posted @ 2022-04-19 20:56  回头是岸不可能回头  阅读(446)  评论(0编辑  收藏  举报