说明

最基本的 CRUD

后端

  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import com.baomidou.mybatisplus.toolkit.IdWorker; import com.github.houbb.privilege.admin.common.dto.BasePageInfo; import com.github.houbb.privilege.admin.common.dto.BaseResp; import com.github.houbb.privilege.admin.common.dto.common.CommonPageReq; import com.github.houbb.privilege.admin.common.util.RespUtil; import com.github.houbb.privilege.admin.dal.entity.User; import com.github.houbb.privilege.admin.service.service.UserService; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ResponseBody; /** * <p> * 用户表 前端控制器 * </p> * * @author binbin.hou * @since 2020-09-18 */ @Controller @RequestMapping("/user") public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } /** * 首页 * @return 首页地址 * @since 0.0.5 */ @RequestMapping("/index") public String index() { return "user/index"; } @RequestMapping("/add") @ResponseBody public BaseResp add(@RequestBody final User user) { user.setUserId(IdWorker.get32UUID()); userService.insert(user); return RespUtil.success(); } @RequestMapping("/edit") @ResponseBody public BaseResp edit(final User user) { userService.updateById(user); return RespUtil.success(); } @RequestMapping("/remove/{id}") @ResponseBody public BaseResp remove(@PathVariable final Integer id) { userService.deleteById(id); return RespUtil.success(); } @RequestMapping("/list") @ResponseBody public BaseResp list(@RequestBody CommonPageReq pageReq) { BasePageInfo<User> pageInfo = userService.pageQueryList(pageReq); return RespUtil.of(pageInfo); } }

前端

例子

缺陷:关于 rules 的校验暂时都不生效。

  [html]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>用户首页</title> <!-- 引入样式 --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"/> <style> .el-footer { color: #333; text-align: center; line-height: 60px; } .el-aside { background-color: #D3DCE6; color: #333; text-align: center; line-height: 200px; width: 200px; } .el-main { color: #333; text-align: center; padding-top: 100px; height: 520px; } #app, html, body, .el-container { height: 100%; } #app { margin-left: 300px; margin-right: 300px; } .el-pagination { padding-top: 100px; } </style> </head> <body> <div id="app"> <el-container direction="vertical"> <el-header> <a href="#"> <el-image style="width: 100px; height: 58px; display: inline-block;" src="/img/logo.PNG" fit="cover"> </el-image> </a> <el-menu default-active="1" class="el-menu-demo" mode="horizontal" style="float: right;"> <el-menu-item index="1"> <a href="/" target="_blank">首页</a> </el-menu-item> <el-submenu index="2"> <template slot="title">权限管理</template> <el-menu-item index="2-1"> <i class="el-icon-user-solid"></i> 用户管理 </el-menu-item> <el-menu-item index="2-2"> <i class="el-icon-magic-stick"></i> 角色管理 </el-menu-item> <el-menu-item index="2-3"> <i class="el-icon-lock"></i> 权限管理 </el-menu-item> </el-submenu> <el-submenu index="3"> <template slot="title">审计日志</template> <el-menu-item index="3-1"><i class="el-icon-user-solid"></i>用户操作</el-menu-item> <el-menu-item index="3-2">角色操作</el-menu-item> <el-menu-item index="3-3">权限操作</el-menu-item> </el-submenu> <el-submenu index="4"> <template slot="title">更多</template> <el-menu-item index="4-1">关于我们</el-menu-item> <el-menu-item index="4-2">变更日志</el-menu-item> <el-menu-item index="4-3">版本信息</el-menu-item> </el-submenu> </el-menu> </el-header> <el-main> <el-form :model="queryForm" ref="queryForm" :inline="true" label-width="80px"> <el-form-item label="用户标识"> <el-input v-model="queryForm.userId"></el-input> </el-form-item> <el-form-item label="用户名称"> <el-input v-model="queryForm.userName"></el-input> </el-form-item> <el-form-item label="用户描述"> <el-input v-model="queryForm.remark"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="doQuery('queryForm')" icon="el-icon-search">查询</el-button> <el-button type="warning" @click="doClear('queryForm')" icon="el-icon-refresh-right">重置</el-button> <el-button type="success" @click="dialogAddVisible = true" icon="el-icon-plus">新增</el-button> </el-form-item> </el-form> <el-divider></el-divider> <el-table ref="singleTable" :data="tableData" border stripe style="width: 100%"> <el-table-column type="index" label="序号" width="60"> </el-table-column> <el-table-column property="id" label="用户主键" width="100"> </el-table-column> <el-table-column property="userId" label="用户标识"> </el-table-column> <el-table-column property="userName" label="用户名称"> </el-table-column> <el-table-column property="remark" label="备注"> </el-table-column> <el-table-column property="createTime" label="创建时间"> </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button size="mini" type="primary" @click="handleRole(scope.row)"><i class="el-icon-magic-stick"></i></el-button> <el-button size="mini" @click="handleEdit(scope.row)"><i class="el-icon-edit"></i></el-button> <el-button size="mini" type="danger" @click="handleDelete(scope.row)"><i class="el-icon-delete"></i></el-button> </template> </el-table-column> </el-table> <el-pagination background layout="total, prev, pager, next" :data="page" :total="page.total" :current-page="page.pageNum" :page-size="page.pageSize" @current-change="handleCurrentChange" > </el-pagination> </el-main> <el-divider></el-divider> <el-footer><el-link :underline="false" href="https://github.com/houbb/privilege-admin" type="primary">privilege-admin </el-link> 强力支持 </el-footer> <el-dialog title="添加用户" :visible.sync="dialogAddVisible"> <el-form :model="addForm" :rules="addRules" ref="addForm"> <el-form-item label="用户名称"> <el-input v-model="addForm.userName" prop="userName" autocomplete="off"></el-input> </el-form-item> <el-form-item label="用户备注"> <el-input v-model="addForm.remark" prop="remark" autocomplete="off"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogAddVisible = false">取 消</el-button> <el-button type="primary" @click="saveAddForm('addForm')">确 定</el-button> </div> </el-dialog> <el-dialog title="修改用户" :visible.sync="dialogEditVisible" @close="closeDialogEditVisible"> <el-form :model="editForm" :rules="editRules" ref="editForm"> <el-form-item label="用户主键" style="display: none;"> <el-input v-model="editForm.id" prop="id" disabled></el-input> </el-form-item> <el-form-item label="用户标识"> <el-input v-model="editForm.userId" prop="userId" autocomplete="off" disabled></el-input> </el-form-item> <el-form-item label="用户名称"> <el-input v-model="editForm.userName" prop="userName" autocomplete="off"></el-input> </el-form-item> <el-form-item label="用户备注"> <el-input v-model="editForm.remark" prop="remark" autocomplete="off"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogEditVisible = false">取 消</el-button> <el-button type="primary" @click="saveEditForm('editForm')">确 定</el-button> </div> </el-dialog> </el-container> </div> <script src="https://unpkg.com/vue/dist/vue.js"></script> <!-- 引入组件库 --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <!--axios--> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script> <script> var vm =new Vue({ el: '#app', data: { tableData: [], page: { total: 0, pageNum: 1, pageSize: 10 }, dialogAddVisible: false, addRules: { userId: [ {required: true, message: '请输入用户标识', trigger: 'blur'}, {min: 1, max: 32, message: '长度在 1 到 32 个字符', trigger: 'blur'} ], userName: [ {required: true, message: '请输入用户名称', trigger: 'blur'}, {min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'blur'} ], remark: [ {min: 0, max: 128, message: '长度在 1 到 128 个字符', trigger: 'blur'} ], }, addForm: { userName: '', remark: '' }, queryForm: { userId: '', userName: '', remark: '' }, dialogEditVisible: false, editForm: { id: '', userId: '', userName: '', remark: '', createTime: '' }, editRules: {} }, mounted () { // 接在完成之后,调用初始化方法 this.init() }, methods: { init() { // 加载数据 this.doQuery(); }, doQuery() { var req = { id: this.queryForm.userId, name: this.queryForm.userName, pageNum: this.page.pageNum, pageSize: this.page.pageSize } console.log("请求:" + JSON.stringify(req)); //axios 中的 this 并不指向 vue var _this = this; axios.post('/user/list', req).then(function (response) { if(response.data.respCode === '0000') { _this.tableData = response.data.list; _this.page.total = response.data.total; } else { ELEMENT.Message.error(response.data.respMessage); } }).catch(function (error) { ELEMENT.Message.error("请求失败"); console.log(error); }); }, doClear() { this.queryForm.userId = ''; this.queryForm.userName = ''; this.queryForm.remark = ''; }, handleEdit(row) { this.editForm.id = row.id; this.editForm.userId = row.userId; this.editForm.userName = row.userName; this.editForm.remark = row.remark; this.editForm.createTime = row.createTime; // 显示编辑 form this.dialogEditVisible = true; }, handleDelete(row) { var id = row.id; var _this = this; axios.post('/user/remove/' + id).then(function (response) { if(response.data.respCode === '0000') { _this.doQuery(); ELEMENT.Message.success("删除成功"); visible = false; } else { ELEMENT.Message.error(response.data.respMessage); } }).catch(function (error) { ELEMENT.Message.error("删除失败"); }); }, handleRole(row) { alert(row.userName); }, handleCurrentChange(val) { this.page.pageNum = val; this.doQuery(); }, closeDialogAddVisible() { this.$refs.addForm.resetFields();//element封装的方法,清空模态框的值 this.dialogAddVisible = false; }, /** *确定新增数据 * @param aaa 表单信息 */ saveAddForm(aaa) { var _this = this; this.$refs[aaa].validate((valid) => { console.log(this.$refs[aaa]) if (valid) { var user = { userName: this.addForm.userName, remark: this.addForm.remark } axios.post('/user/add', user).then(function (response) { if(response.data.respCode === '0000') { ELEMENT.Message.success("请求成功"); _this.closeDialogAddVisible(); } else { ELEMENT.Message.error(response.data.respMessage); } }).catch(function (error) { ELEMENT.Message.error("请求失败"); console.log(error); }); } }) }, closeDialogEditVisible() { this.$refs.editForm.resetFields();//element封装的方法,清空模态框的值 this.dialogEditVisible = false; }, saveEditForm(editForm) { var user = { id: this.editForm.id, userId: this.editForm.userId, userName: this.editForm.userName, remark: this.editForm.remark }; // 此处使用 qs 序列化,后端不需要使用 @RequestBody 注解。 var userData = Qs.stringify(user); var _this = this; axios.post('/user/edit', userData).then(function (response) { if(response.data.respCode === '0000') { ELEMENT.Message.success("请求成功"); _this.doQuery(); _this.closeDialogEditVisible(); } else { ELEMENT.Message.error(response.data.respMessage); } }).catch(function (error) { ELEMENT.Message.error("请求失败"); console.log(error); }); } } }) </script> </body> </html>