前台
我們需要發送請求和帶入參數
有了這兩個屬性過后需要在data中添加
這個屬性是和方法平級的
整個頁面

<template> <section> <!--工具條--> <el-col :span="24" class="toolbar" style="padding-bottom: 0px;"> <el-form :inline="true" :model="filters"> <el-form-item> <el-input v-model="filters.keywords" placeholder="關鍵字"></el-input> </el-form-item> <el-form-item> <el-button type="primary" v-on:click="getEmployees">查詢</el-button> </el-form-item> <el-form-item> <el-button type="primary" @click="handleAdd">新增</el-button> </el-form-item> </el-form> </el-col> <!--列表--> <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;"> <el-table-column type="selection" width="55"> </el-table-column> <el-table-column prop="id" width="60" label="ID"> </el-table-column> <el-table-column prop="username" label="姓名" width="120" sortable> </el-table-column> <el-table-column prop="sn" label="員工編號" width="80" sortable> </el-table-column> <el-table-column prop="password" label="密碼" width="280" sortable> </el-table-column> <el-table-column prop="headimg" label="頭像" width="180" sortable> </el-table-column> <el-table-column prop="phone" label="電話" width="180" sortable> </el-table-column> <el-table-column prop="department" label="部門" width="180" sortable> </el-table-column> <el-table-column label="操作" width="150"> <template scope="scope"> <el-button size="small" @click="handleEdit(scope.$index, scope.row)">編輯</el-button> <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">刪除</el-button> </template> </el-table-column> </el-table> <!--工具條 page-size為每頁顯示的條數--> <el-col :span="24" class="toolbar"> <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量刪除</el-button> <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;"> </el-pagination> </el-col> <!--編輯界面--> <el-dialog title="編輯" v-model="editFormVisible" :close-on-click-modal="false"> <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm"> <el-form-item label="姓名" prop="username"> <el-input v-model="editForm.username" auto-complete="off"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click.native="editFormVisible = false">取消</el-button> <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button> </div> </el-dialog> <!--新增界面--> <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false"> <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm"> <el-form-item label="姓名" prop="username"> <el-input v-model="addForm.username" auto-complete="off"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click.native="addFormVisible = false">取消</el-button> <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button> </div> </el-dialog> </section> </template> <script> import util from '../../common/js/util' //import NProgress from 'nprogress' import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api'; export default { data() { return { filters: { keywords: '', }, employees: [], total: 0, page: 1, pageSize:5, listLoading: false, sels: [],//列表選中列 editFormVisible: false,//編輯界面是否顯示 editLoading: false, editFormRules: { name: [ { required: true, message: '請輸入姓名', trigger: 'blur' } ] }, //編輯界面數據 editForm: { username: '', }, addFormVisible: false,//新增界面是否顯示 addLoading: false, addFormRules: { name: [ { required: true, message: '請輸入姓名', trigger: 'blur' } ] }, //新增界面數據 addForm: { username: '', } } }, methods: { //性別顯示轉換 formatSex: function (row, column) { return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知'; }, handleCurrentChange(val) { this.page = val; this.getEmployees(); }, //獲取用戶列表 getEmployees() { let para = { page: this.page, pageSize:this.pageSize, keywords: this.filters.keywords }; this.listLoading = true; //NProgress.start(); //getUserListPage(para) this.$http.patch("/employee/query",para).then((res) => { this.total=res.data.total; this.employees = res.data.rows; this.listLoading=false; //NProgress.done(); }); }, //刪除 handleDel: function (index, row) { this.$confirm('確認刪除該記錄嗎?', '提示', { type: 'warning' }).then(() => { this.listLoading = true; //NProgress.start(); let para = { id: row.id }; //console.log(row.id); this.$http.delete('/employee/delete/'+para.id).then((res) => { this.listLoading = false; //NProgress.done(); this.$message({ message: '刪除成功', type: 'success' }); this.getEmployees(); }); }).catch(() => { this.$message({ message: '刪除失敗', type: 'error' }); }); }, //顯示編輯界面 handleEdit: function (index, row) { this.editFormVisible = true; this.editForm = Object.assign({}, row); }, //顯示新增界面 handleAdd: function () { this.addFormVisible = true; this.addForm = { username:'' }; }, //編輯 editSubmit: function () { this.$refs.editForm.validate((valid) => { if (valid) { this.$confirm('確認提交嗎?', '提示', {}).then(() => { this.editLoading = true; //NProgress.start(); let para = Object.assign({}, this.editForm); // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd'); this.$http.put("/employee/update",para).then((res) => { this.editLoading = false; //NProgress.done(); this.$message({ message: '提交成功', type: 'success' }); this.$refs['editForm'].resetFields(); this.editFormVisible = false; this.getEmployees(); }); }); } }); }, //新增 addSubmit: function () { this.$refs.addForm.validate((valid) => { if (valid) { this.$confirm('確認提交嗎?', '提示', {}).then(() => { this.addLoading = true; //NProgress.start(); let para = Object.assign({}, this.addForm); console.log(para); // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd'); this.$http.post("/employee/add",para).then((res) => { this.addLoading = false; //NProgress.done(); this.$message({ message: '提交成功', type: 'success' }); this.$refs['addForm'].resetFields(); this.addFormVisible = false; this.getEmployees(); }); }); } }); }, selsChange: function (sels) { this.sels = sels; }, //批量刪除 batchRemove: function () { var ids = this.sels.map(item => item.id).toString(); this.$confirm('確認刪除選中記錄嗎?', '提示', { type: 'warning' }).then(() => { this.listLoading = true; //NProgress.start(); let para = { ids: ids }; batchRemoveUser(para).then((res) => { this.listLoading = false; //NProgress.done(); this.$message({ message: '刪除成功', type: 'success' }); this.getEmployees(); }); }).catch(() => { }); } }, mounted() { this.getEmployees(); } } function pagination(index,size,list){ return list.slice((index-1)*size,index*size); } </script> <style scoped> </style>
后台web
首先需要一個返回值
PageList

package cn.jiedada; import java.util.List; public class PageList<T> { private Long total; private List<T> rows; public Long getTotal() { return total; } public void setTotal(Long total) { this.total = total; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } public PageList(Long total, List<T> rows) { this.total = total; this.rows = rows; } public PageList() { } }
和一個接收參數的類
EmployeeQuery

public class EmployeeQuery extends BasicQuery { }
BasicQuery

package cn.jiedada.query; public class BasicQuery { private Long page; //默認為每頁10張數據 private Long pageSize=10L; //高級查詢的關鍵字 private String keywords; public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } public Long getPageSize() { return pageSize; } public void setPageSize(Long pageSize) { this.pageSize = pageSize; } public Long getPage() { return page; } public void setPage(Long page) { this.page = page; } //設置每頁開始的頁數,limit start,通過get可以直接獲得#{start} public Long getStart(){ return (this.page-1)*pageSize; } }
web

@RequestMapping(value = "/query",method = RequestMethod.PATCH) @ResponseBody //@RequestBody才能把前台的json轉為后台的數據 public PageList<Employee> query(@RequestBody EmployeeQuery employeeQuery){ return employeeService.query(employeeQuery); }
后台service
因為每個頁面都需要使用分頁和高級查詢所以我們在父類中寫入
IBasicService

package cn.jiedada.service; import cn.jiedada.PageList; import cn.jiedada.query.BasicQuery; import java.io.Serializable; import java.util.List; public interface IBasicService<T> { void save(T t); void delete(Serializable id); void update(T t); List<T> findAll(); T findOne(Serializable id); PageList<T> query(BasicQuery basicQuery); }
BasicServiceImpl

package cn.jiedada.service.impl; import cn.jiedada.PageList; import cn.jiedada.mapper.BasicMapper; import cn.jiedada.query.BasicQuery; import cn.jiedada.service.IBasicService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; import java.util.List; @Transactional(propagation = Propagation.SUPPORTS,readOnly = true) public class BasicServiceImpl<T> implements IBasicService<T> { @Autowired private BasicMapper<T> basicMapper; @Override @Transactional public void save(T t) { basicMapper.save(t); } @Override @Transactional public void delete(Serializable id) { basicMapper.delete(id); } @Override @Transactional public void update(T t) { basicMapper.update(t); } @Override public List<T> findAll() { return basicMapper.findAll(); } @Override public T findOne(Serializable id) { return basicMapper.findOne(id); } @Override public PageList<T> query(BasicQuery basicQuery) { // 通過Mapper查詢總數 Long total = basicMapper.querryCount(basicQuery); //查詢當前頁數據 List<T> rows = basicMapper.queryData(basicQuery); //通過構造方法返回數據中的所有值 return new PageList<T>(total,rows); } }
后台mapper
BasicMapper

package cn.jiedada.mapper; import cn.jiedada.query.BasicQuery; import java.io.Serializable; import java.util.List; public interface BasicMapper<T> { void save(T t); void delete(Serializable id); void update(T t); List<T> findAll(); T findOne(Serializable id); //查詢總頁數 Long querryCount(BasicQuery basicQuery); //查詢分頁1的所有數據 List<T> queryData(BasicQuery basicQuery); }
后台mapper.xml
通過繼承和實現EmployeeMapper和EmployeeService也有我們的東西了,只需要實現Mapper中的方法我們就可以完成了
在其中添加

<!--查詢總頁數 記住需要添加where條件 --> <select id="querryCount" parameterType="employeeQuery" resultType="long"> select count(id) from t_employee <where> <include refid="sqlWhere"></include> </where> </select> <!--查詢pageList--> <select id="queryData" parameterType="employeeQuery" resultType="employee"> select * from t_employee <where> <include refid="sqlWhere"></include> </where> LIMIT #{start},#{pageSize} </select> <!--高級查詢,判斷是否傳入來了keywords--> <sql id="sqlWhere"> <if test="keywords!=null and keywords!=''"> and username like concat('%',#{keywords},'%') </if> </sql>