单表管理功能前后端开发

大章列表查询功能开发

增加business模块,并增加初始启动代码

business/pom.xml

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>course</artifactId>
<groupId>com.javami</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>business</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 集成mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- 热部署DevTools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.javami</groupId>
<artifactId>server</artifactId>
</dependency>
</dependencies>


<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>

business/config/BusinessApplication.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
package com.javami.business.config;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;

@SpringBootApplication
@EnableEurekaClient
@ComponentScan("com.javami")
@MapperScan("com.javami.server.mapper")
public class BusinessApplication {

private static final Logger LOG = LoggerFactory.getLogger(BusinessApplication.class);

public static void main(String[] args) {
SpringApplication app = new SpringApplication(BusinessApplication.class);
Environment env = app.run(args).getEnvironment();
LOG.info("启动成功!!");
LOG.info("Business地址: \thttp://127.0.0.1:{}", env.getProperty("server.port"));
}

}

business/controller/TestController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.javami.business.controller;

import com.javami.server.domain.Test;
import com.javami.server.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TestController {

@Resource
private TestService testService;

@RequestMapping("/test")
public List<Test> test() {
return testService.list();
}
}

增加大章chapter表sql,生成持久层代码

1
2
3
4
5
6
7
drop table if exists `chapter`;
create table `chapter` (
`id` char(8) not null comment 'ID',
`course_id` char(8) comment '课程ID',
`name` varchar(50) comment '名称',
primary key (`id`)
) engine=innodb default charset=utf8mb4 comment='大章';

增加大章列表查询接口

business/controller/ChapterController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import com.javami.course.server.domain.Chapter;
import com.javami.course.server.service.ChapterService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ChapterController {

@Resource
private ChapterService chapterService;

@RequestMapping("/chapter")
public List<Chapter> chapter() {
return chapterService.list();
}
}

server/service/ChapterService.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
package com.javami.course.server.service;

import com.javami.course.server.domain.Chapter;
import com.javami.course.server.domain.ChapterExample;
import com.javami.course.server.mapper.ChapterMapper;
import com.javami.course.server.domain.ChapterExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ChapterService {

@Resource
private ChapterMapper chapterMapper;

public List<Chapter> list() {
ChapterExample chapterExample = new ChapterExample();
chapterExample.createCriteria().andIdEqualTo("1");
chapterExample.setOrderByClause("id desc");
return chapterMapper.selectByExample(chapterExample);
}
}

增加dto层,用于controller和service层

增加ChapterDto
server/dto/ChapterDto.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
package com.javami.course.server.dto;

/**
* @author Kevin
* @create 2021-11-22 16:18
*/
public class ChapterDto {
private String id;

private String courseId;

private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getCourseId() {
return courseId;
}

public void setCourseId(String courseId) {
this.courseId = courseId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", courseId=").append(courseId);
sb.append(", name=").append(name);
sb.append("]");
return sb.toString();
}
}

修改ChapterService,将返回Chapter改成返回ChapterDto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Service
public class ChapterService {

@Resource
private ChapterMapper chapterMapper;

public List<ChapterDto> list() {
ChapterExample chapterExample = new ChapterExample();
List<Chapter> chapterList = chapterMapper.selectByExample(chapterExample);
List<ChapterDto> chapterDtoList = new ArrayList<ChapterDto>();
for (int i = 0, l = chapterList.size(); i < l; i++) {
Chapter chapter = chapterList.get(i);
ChapterDto chapterDto = new ChapterDto();
BeanUtils.copyProperties(chapter, chapterDto);
chapterDtoList.add(chapterDto);
}
return chapterDtoList;
}
}

business/controller/ChapterController.java

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
@RequestMapping("/admin")
public class ChapterController {

@Resource
private ChapterService chapterService;

@RequestMapping("/chapter")
public List<ChapterDto> chapter() {
return chapterService.list();
}
}

新增chapter页面及子路由,并使用假数据制作表格

admin/src/router.js

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
import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/login.vue'
import Admin from './views/admin.vue'
import Welcome from './views/admin/welcome.vue'
import Chapter from './views/admin/chapter.vue'

Vue.use(Router);

export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [{
path: '*',
redirect: "/login",
}, {
path: '/login',
component: Login
}, {
path: '/admin',
component: Admin,
children: [{
path: 'welcome',
component: Welcome,
}, {
path: 'chapter',
component: Chapter,
}]
}]
})

views/admin/chapter.vue

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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
<template>
<table id="simple-table" class="table table-bordered table-hover">
<thead>
<tr>
<th class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" />
<span class="lbl"></span>
</label>
</th>
<th class="detail-col">Details</th>
<th>Domain</th>
<th>Price</th>
<th class="hidden-480">Clicks</th>

<th>
<i class="ace-icon fa fa-clock-o bigger-110 hidden-480"></i>
Update
</th>
<th class="hidden-480">Status</th>

<th></th>
</tr>
</thead>

<tbody>
<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" />
<span class="lbl"></span>
</label>
</td>

<td class="center">
<div class="action-buttons">
<a href="#" class="green bigger-140 show-details-btn" title="Show Details">
<i class="ace-icon fa fa-angle-double-down"></i>
<span class="sr-only">Details</span>
</a>
</div>
</td>

<td>
<a href="#">ace.com</a>
</td>
<td>$45</td>
<td class="hidden-480">3,330</td>
<td>Feb 12</td>

<td class="hidden-480">
<span class="label label-sm label-warning">Expiring</span>
</td>

<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>

<tr class="detail-row">
<td colspan="8">
<div class="table-detail">
<div class="row">
<div class="col-xs-12 col-sm-2">
<div class="text-center">
<img height="150" class="thumbnail inline no-margin-bottom" alt="Domain Owner's Avatar" src="assets/images/avatars/profile-pic.jpg" />
<br />
<div class="width-80 label label-info label-xlg arrowed-in arrowed-in-right">
<div class="inline position-relative">
<a class="user-title-label" href="#">
<i class="ace-icon fa fa-circle light-green"></i>
&nbsp;
<span class="white">Alex M. Doe</span>
</a>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-7">
<div class="space visible-xs"></div>

<div class="profile-user-info profile-user-info-striped">
<div class="profile-info-row">
<div class="profile-info-name"> Username </div>

<div class="profile-info-value">
<span>alexdoe</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Location </div>

<div class="profile-info-value">
<i class="fa fa-map-marker light-orange bigger-110"></i>
<span>Netherlands, Amsterdam</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Age </div>

<div class="profile-info-value">
<span>38</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Joined </div>

<div class="profile-info-value">
<span>2010/06/20</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Last Online </div>

<div class="profile-info-value">
<span>3 hours ago</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> About Me </div>

<div class="profile-info-value">
<span>Bio</span>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-3">
<div class="space visible-xs"></div>
<h4 class="header blue lighter less-margin">Send a message to Alex</h4>

<div class="space-6"></div>

<form>
<fieldset>
<textarea class="width-100" resize="none" placeholder="Type something…"></textarea>
</fieldset>

<div class="hr hr-dotted"></div>

<div class="clearfix">
<label class="pull-left">
<input type="checkbox" class="ace" />
<span class="lbl"> Email me a copy</span>
</label>

<button class="pull-right btn btn-sm btn-primary btn-white btn-round" type="button">
Submit
<i class="ace-icon fa fa-arrow-right icon-on-right bigger-110"></i>
</button>
</div>
</form>
</div>
</div>
</div>
</td>
</tr>

<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" />
<span class="lbl"></span>
</label>
</td>

<td class="center">
<div class="action-buttons">
<a href="#" class="green bigger-140 show-details-btn" title="Show Details">
<i class="ace-icon fa fa-angle-double-down"></i>
<span class="sr-only">Details</span>
</a>
</div>
</td>

<td>
<a href="#">base.com</a>
</td>
<td>$35</td>
<td class="hidden-480">2,595</td>
<td>Feb 18</td>

<td class="hidden-480">
<span class="label label-sm label-success">Registered</span>
</td>

<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>

<tr class="detail-row">
<td colspan="8">
<div class="table-detail">
<div class="row">
<div class="col-xs-12 col-sm-2">
<div class="text-center">
<img height="150" class="thumbnail inline no-margin-bottom" alt="Domain Owner's Avatar" src="assets/images/avatars/profile-pic.jpg" />
<br />
<div class="width-80 label label-info label-xlg arrowed-in arrowed-in-right">
<div class="inline position-relative">
<a class="user-title-label" href="#">
<i class="ace-icon fa fa-circle light-green"></i>
&nbsp;
<span class="white">Alex M. Doe</span>
</a>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-7">
<div class="space visible-xs"></div>

<div class="profile-user-info profile-user-info-striped">
<div class="profile-info-row">
<div class="profile-info-name"> Username </div>

<div class="profile-info-value">
<span>alexdoe</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Location </div>

<div class="profile-info-value">
<i class="fa fa-map-marker light-orange bigger-110"></i>
<span>Netherlands, Amsterdam</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Age </div>

<div class="profile-info-value">
<span>38</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Joined </div>

<div class="profile-info-value">
<span>2010/06/20</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Last Online </div>

<div class="profile-info-value">
<span>3 hours ago</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> About Me </div>

<div class="profile-info-value">
<span>Bio</span>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-3">
<div class="space visible-xs"></div>
<h4 class="header blue lighter less-margin">Send a message to Alex</h4>

<div class="space-6"></div>

<form>
<fieldset>
<textarea class="width-100" resize="none" placeholder="Type something…"></textarea>
</fieldset>

<div class="hr hr-dotted"></div>

<div class="clearfix">
<label class="pull-left">
<input type="checkbox" class="ace" />
<span class="lbl"> Email me a copy</span>
</label>

<button class="pull-right btn btn-sm btn-primary btn-white btn-round" type="button">
Submit
<i class="ace-icon fa fa-arrow-right icon-on-right bigger-110"></i>
</button>
</div>
</form>
</div>
</div>
</div>
</td>
</tr>

<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" />
<span class="lbl"></span>
</label>
</td>

<td class="center">
<div class="action-buttons">
<a href="#" class="green bigger-140 show-details-btn" title="Show Details">
<i class="ace-icon fa fa-angle-double-down"></i>
<span class="sr-only">Details</span>
</a>
</div>
</td>

<td>
<a href="#">max.com</a>
</td>
<td>$60</td>
<td class="hidden-480">4,400</td>
<td>Mar 11</td>

<td class="hidden-480">
<span class="label label-sm label-warning">Expiring</span>
</td>

<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>

<tr class="detail-row">
<td colspan="8">
<div class="table-detail">
<div class="row">
<div class="col-xs-12 col-sm-2">
<div class="text-center">
<img height="150" class="thumbnail inline no-margin-bottom" alt="Domain Owner's Avatar" src="assets/images/avatars/profile-pic.jpg" />
<br />
<div class="width-80 label label-info label-xlg arrowed-in arrowed-in-right">
<div class="inline position-relative">
<a class="user-title-label" href="#">
<i class="ace-icon fa fa-circle light-green"></i>
&nbsp;
<span class="white">Alex M. Doe</span>
</a>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-7">
<div class="space visible-xs"></div>

<div class="profile-user-info profile-user-info-striped">
<div class="profile-info-row">
<div class="profile-info-name"> Username </div>

<div class="profile-info-value">
<span>alexdoe</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Location </div>

<div class="profile-info-value">
<i class="fa fa-map-marker light-orange bigger-110"></i>
<span>Netherlands, Amsterdam</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Age </div>

<div class="profile-info-value">
<span>38</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Joined </div>

<div class="profile-info-value">
<span>2010/06/20</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Last Online </div>

<div class="profile-info-value">
<span>3 hours ago</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> About Me </div>

<div class="profile-info-value">
<span>Bio</span>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-3">
<div class="space visible-xs"></div>
<h4 class="header blue lighter less-margin">Send a message to Alex</h4>

<div class="space-6"></div>

<form>
<fieldset>
<textarea class="width-100" resize="none" placeholder="Type something…"></textarea>
</fieldset>

<div class="hr hr-dotted"></div>

<div class="clearfix">
<label class="pull-left">
<input type="checkbox" class="ace" />
<span class="lbl"> Email me a copy</span>
</label>

<button class="pull-right btn btn-sm btn-primary btn-white btn-round" type="button">
Submit
<i class="ace-icon fa fa-arrow-right icon-on-right bigger-110"></i>
</button>
</div>
</form>
</div>
</div>
</div>
</td>
</tr>

<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" />
<span class="lbl"></span>
</label>
</td>

<td class="center">
<div class="action-buttons">
<a href="#" class="green bigger-140 show-details-btn" title="Show Details">
<i class="ace-icon fa fa-angle-double-down"></i>
<span class="sr-only">Details</span>
</a>
</div>
</td>

<td>
<a href="#">best.com</a>
</td>
<td>$75</td>
<td class="hidden-480">6,500</td>
<td>Apr 03</td>

<td class="hidden-480">
<span class="label label-sm label-inverse arrowed-in">Flagged</span>
</td>

<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>

<tr class="detail-row">
<td colspan="8">
<div class="table-detail">
<div class="row">
<div class="col-xs-12 col-sm-2">
<div class="text-center">
<img height="150" class="thumbnail inline no-margin-bottom" alt="Domain Owner's Avatar" src="assets/images/avatars/profile-pic.jpg" />
<br />
<div class="width-80 label label-info label-xlg arrowed-in arrowed-in-right">
<div class="inline position-relative">
<a class="user-title-label" href="#">
<i class="ace-icon fa fa-circle light-green"></i>
&nbsp;
<span class="white">Alex M. Doe</span>
</a>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-7">
<div class="space visible-xs"></div>

<div class="profile-user-info profile-user-info-striped">
<div class="profile-info-row">
<div class="profile-info-name"> Username </div>

<div class="profile-info-value">
<span>alexdoe</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Location </div>

<div class="profile-info-value">
<i class="fa fa-map-marker light-orange bigger-110"></i>
<span>Netherlands, Amsterdam</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Age </div>

<div class="profile-info-value">
<span>38</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Joined </div>

<div class="profile-info-value">
<span>2010/06/20</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Last Online </div>

<div class="profile-info-value">
<span>3 hours ago</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> About Me </div>

<div class="profile-info-value">
<span>Bio</span>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-3">
<div class="space visible-xs"></div>
<h4 class="header blue lighter less-margin">Send a message to Alex</h4>

<div class="space-6"></div>

<form>
<fieldset>
<textarea class="width-100" resize="none" placeholder="Type something…"></textarea>
</fieldset>

<div class="hr hr-dotted"></div>

<div class="clearfix">
<label class="pull-left">
<input type="checkbox" class="ace" />
<span class="lbl"> Email me a copy</span>
</label>

<button class="pull-right btn btn-sm btn-primary btn-white btn-round" type="button">
Submit
<i class="ace-icon fa fa-arrow-right icon-on-right bigger-110"></i>
</button>
</div>
</form>
</div>
</div>
</div>
</td>
</tr>

<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" />
<span class="lbl"></span>
</label>
</td>

<td class="center">
<div class="action-buttons">
<a href="#" class="green bigger-140 show-details-btn" title="Show Details">
<i class="ace-icon fa fa-angle-double-down"></i>
<span class="sr-only">Details</span>
</a>
</div>
</td>

<td>
<a href="#">pro.com</a>
</td>
<td>$55</td>
<td class="hidden-480">4,250</td>
<td>Jan 21</td>

<td class="hidden-480">
<span class="label label-sm label-success">Registered</span>
</td>

<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>

<tr class="detail-row">
<td colspan="8">
<div class="table-detail">
<div class="row">
<div class="col-xs-12 col-sm-2">
<div class="text-center">
<img height="150" class="thumbnail inline no-margin-bottom" alt="Domain Owner's Avatar" src="assets/images/avatars/profile-pic.jpg" />
<br />
<div class="width-80 label label-info label-xlg arrowed-in arrowed-in-right">
<div class="inline position-relative">
<a class="user-title-label" href="#">
<i class="ace-icon fa fa-circle light-green"></i>
&nbsp;
<span class="white">Alex M. Doe</span>
</a>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-7">
<div class="space visible-xs"></div>

<div class="profile-user-info profile-user-info-striped">
<div class="profile-info-row">
<div class="profile-info-name"> Username </div>

<div class="profile-info-value">
<span>alexdoe</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Location </div>

<div class="profile-info-value">
<i class="fa fa-map-marker light-orange bigger-110"></i>
<span>Netherlands, Amsterdam</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Age </div>

<div class="profile-info-value">
<span>38</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Joined </div>

<div class="profile-info-value">
<span>2010/06/20</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> Last Online </div>

<div class="profile-info-value">
<span>3 hours ago</span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> About Me </div>

<div class="profile-info-value">
<span>Bio</span>
</div>
</div>
</div>
</div>

<div class="col-xs-12 col-sm-3">
<div class="space visible-xs"></div>
<h4 class="header blue lighter less-margin">Send a message to Alex</h4>

<div class="space-6"></div>

<form>
<fieldset>
<textarea class="width-100" resize="none" placeholder="Type something…"></textarea>
</fieldset>

<div class="hr hr-dotted"></div>

<div class="clearfix">
<label class="pull-left">
<input type="checkbox" class="ace" />
<span class="lbl"> Email me a copy</span>
</label>

<button class="pull-right btn btn-sm btn-primary btn-white btn-round" type="button">
Submit
<i class="ace-icon fa fa-arrow-right icon-on-right bigger-110"></i>
</button>
</div>
</form>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</template>

点击sidebar菜单实现页面跳转并增加激活样式

views/admin.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 菜单激活样式,id是当前点击的菜单的id
* @param id
*/
activeSidebar: function (id) {
// 兄弟菜单去掉active样式,自身增加active样式
$("#" + id).siblings().removeClass("active");
$("#" + id).siblings().find("li").removeClass("active");
$("#" + id).addClass("active");

// 如果有父菜单,父菜单的兄弟菜单去掉open active,父菜单增加open active
let parentLi = $("#" + id).parents("li");
if (parentLi) {
parentLi.siblings().removeClass("open active");
parentLi.addClass("open active");
}
}

路由修改,去掉/admin

admin/src/router.js

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
import Vue from "vue"
import Router from "vue-router"
import Login from "./views/login.vue"
import Admin from "./views/admin.vue"
import Welcome from "./views/admin/welcome.vue"
import Chapter from "./views/admin/chapter.vue"

Vue.use(Router);

export default new Router({
mode: "history",
base: process.env.BASE_URL,
routes: [{
path: "*",
redirect: "/login",
}, {
path: "/login",
component: Login
}, {
path: "/",
name: "admin",
component: Admin,
children: [{
path: "welcome",
name: "welcome",
component: Welcome,
}, {
path: "business/chapter",
name: "business/chapter",
component: Chapter,
}]
}]
})

通用的sidebar点击激活样式方法,使用watch监听路由变化

views/admin.vue

1
2
3
4
5
6
7
8
9
10
11
12
watch: {
$route: {
handler:function(val, oldVal){
// sidebar激活样式方法二
console.log("---->页面跳转:", val, oldVal);
let _this = this;
_this.$nextTick(function(){ //页面加载完成后执行
_this.activeSidebar(_this.$route.name.replace("/", "-") + "-sidebar");
})
}
}
},

集成axios完成前后端交互

admin/src/main.js

1
2
3
4
5
6
7
8
9
10
11
12
import Vue from 'vue'
import App from './app.vue'
import router from './router'
import axios from 'axios'

Vue.config.productionTip = false;
Vue.prototype.$ajax = axios;

new Vue({
router,
render: h => h(App),
}).$mount('#app');

views/admin/chapter.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script>
export default {
name: "chapter",
mounted: function() {
let _this = this;
_this.list();
// sidebar激活样式方法一
// this.$parent.activeSidebar("business-chapter-sidebar");

},
methods: {
list() {
let _this = this;
_this.$ajax.get('http://127.0.0.1:9002/business/admin/chapter/list').then((response)=>{
console.log("查询大章列表结果:", response);
})
}
}
}
</script>

business/controller/admin/ChapterController.java

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
@RequestMapping("/admin/chapter")
public class ChapterController {

@Resource
private ChapterService chapterService;

@RequestMapping("/list")
public List<ChapterDto> list() {
return chapterService.list();
}
}

增加CorsConfig,解决前后端跨域的问题

server/config/CorsConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedHeaders(CorsConfiguration.ALL)
.allowedMethods(CorsConfiguration.ALL)
.allowCredentials(true)
.maxAge(3600); // 1小时内不需要再预检(发OPTIONS请求)
}

}

大章页面显示真实数据

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
<template>
<table id="simple-table" class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>课程ID</th>
<th>操作</th>
</tr>
</thead>

<tbody>
<tr v-for="chapter in chapters">
<td>{{chapter.id}}</td>
<td>{{chapter.name}}</td>
<td>{{chapter.courseId}}</td>
<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</template>

<script>
export default {
name: "chapter",
data: function() {
return {
chapters: []
}
},
mounted: function() {
let _this = this;
_this.list();
// sidebar激活样式方法一
// this.$parent.activeSidebar("business-chapter-sidebar");

},
methods: {
list() {
let _this = this;
_this.$ajax.get('http://127.0.0.1:9002/business/admin/chapter/list').then((response)=>{
console.log("查询大章列表结果:", response);
_this.chapters = response.data;
})
}
}
}
</script>

使用gateway路由转发,vue页面只访问gateway的端口

gateway/src/main/resources/application.properties

1
2
3
4
spring.cloud.gateway.routes[1].id=business
spring.cloud.gateway.routes[1].uri=http://127.0.0.1:9002
spring.cloud.gateway.routes[1].predicates[0].name=Path
spring.cloud.gateway.routes[1].predicates[0].args[0]=/business/**

解决gateway跨域问题

gateway/GatewayApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 配置跨域
* @return
*/
@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();

config.setAllowCredentials(Boolean.TRUE);
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.setMaxAge(3600L);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);

return new CorsWebFilter(source);
}

使用lb://+注册中心名称作路由转发

resources/application.properties

1
2
3
4
5
spring.cloud.gateway.routes[1].id=business
#spring.cloud.gateway.routes[1].uri=http://127.0.0.1:9002
spring.cloud.gateway.routes[1].uri=lb://business
spring.cloud.gateway.routes[1].predicates[0].name=Path
spring.cloud.gateway.routes[1].predicates[0].args[0]=/business/**

分页功能开发

集成分页插件pagehelper,注意页码从1开始

父pom.xml

1
2
3
4
5
6
<!-- mybatis分页插件pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>

server/pom.xml

1
2
3
4
5
<!-- mybatis分页插件pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>

server/service/ChapterService.java

1
PageHelper.startPage(2, 1);

分页功能的关键字:limit。从日志可以看出,该sql执行的是limit 1,相当于limit 0,1,即从第0行开始,查1条。

会针对第一条sql查询语句生效。

前端分页组件的使用

分页参数前后端交互,axios的post请求默认是以流的方式传递参数,所以controller里的参数要加@RequestBody注解

views/admin/chapter.vue

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
<script>
export default {
name: "chapter",
data: function() {
return {
chapters: []
}
},
mounted: function() {
let _this = this;
_this.list();
// sidebar激活样式方法一
// this.$parent.activeSidebar("business-chapter-sidebar");

},
methods: {
list() {
let _this = this;
_this.$ajax.post('http://127.0.0.1:9000/business/admin/chapter/list', {
page: 1,
size: 1
}).then((response)=>{
console.log("查询大章列表结果:", response);
_this.chapters = response.data.list;
})
}
}
}
</script>

business/controller/admin/ChapterController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RestController
@RequestMapping("/admin/chapter")
public class ChapterController {

private static final Logger LOG = LoggerFactory.getLogger(ChapterController.class);

@Resource
private ChapterService chapterService;

@RequestMapping("/list")
public PageDto list(@RequestBody PageDto pageDto) {
LOG.info("pageDto: {}", pageDto);
chapterService.list(pageDto);
return pageDto;
}
}

server/dto/PageDto.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
package com.javami.course.server.dto;

import java.util.List;

public class PageDto<T> {

/**
* 当前页码
*/
protected int page;

/**
* 每页条数
*/
protected int size;

/**
* 总条数
*/
protected long total;

protected List<T> list;

public int getPage() {
return page;
}

public void setPage(int page) {
this.page = page;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public long getTotal() {
return total;
}

public void setTotal(long total) {
this.total = total;
}

public List<T> getList() {
return list;
}

public void setList(List<T> list) {
this.list = list;
}

@Override
public String toString() {
final StringBuffer sb = new StringBuffer("PageDto{");
sb.append("page=").append(page);
sb.append(", size=").append(size);
sb.append(", total=").append(total);
sb.append(", list=").append(list);

sb.append('}');
return sb.toString();
}
}

server/service/ChapterService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void list(PageDto pageDto) {
PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
ChapterExample chapterExample = new ChapterExample();
List<Chapter> chapterList = chapterMapper.selectByExample(chapterExample);
PageInfo<Chapter> pageInfo = new PageInfo<>(chapterList);
pageDto.setTotal(pageInfo.getTotal());
List<ChapterDto> chapterDtoList = new ArrayList<ChapterDto>();
for (int i = 0, l = chapterList.size(); i < l; i++) {
Chapter chapter = chapterList.get(i);
ChapterDto chapterDto = new ChapterDto();
BeanUtils.copyProperties(chapter, chapterDto);
chapterDtoList.add(chapterDto);
}
pageDto.setList(chapterDtoList);
}

增加刷新功能

admin/src/views/admin/chapter.vue

1
2
3
4
5
6
<p>
<button v-on:click="list()" class="btn btn-white btn-default btn-round">
<i class="ace-icon fa fa-refresh"></i>
刷新
</button>
</p>

增加分页组件pagination.vue

components/pagination.vue

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
<template>
<div class="pagination" role="group" aria-label="分页">
<button type="button" class="btn btn-default btn-white btn-round"
v-bind:disabled="page === 1"
v-on:click="selectPage(1)">
1
</button>
<button type="button" class="btn btn-default btn-white btn-round"
v-bind:disabled="page === 1"
v-on:click="selectPage(page - 1)">
上一页
</button>
<button v-for="p in pages" v-bind:id="'page-' + p"
type="button" class="btn btn-default btn-white btn-round"
v-bind:class="{'btn-primary active':page == p}"
v-on:click="selectPage(p)">
{{p}}
</button>
<button type="button" class="btn btn-default btn-white btn-round"
v-bind:disabled="page === pageTotal"
v-on:click="selectPage(page + 1)">
下一页
</button>
<button type="button" class="btn btn-default btn-white btn-round"
v-bind:disabled="page === pageTotal"
v-on:click="selectPage(pageTotal)">
{{pageTotal||1}}
</button>
&nbsp;
<span class="m--padding-10">
每页
<select v-model="size">
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
条,共【{{total}}】条
</span>
</div>
</template>

<script>
export default {
name: 'pagination',
props: {
list: {
type: Function,
default: null
},
itemCount: Number // 显示的页码数,比如总共有100页,只显示10页,其它用省略号表示
},
data: function () {
return {
total: 0, // 总行数
size: 10, // 每页条数
page: 0, // 当前页码
pageTotal: 0, // 总页数
pages: [], // 显示的页码数组
}
},
methods: {
/**
* 渲染分页组件
* @param page
* @param total
*/
render(page, total) {
let _this = this;
_this.page = page;
_this.total = total;
_this.pageTotal = Math.ceil(total / _this.size);
_this.pages = _this.getPageItems(_this.pageTotal, page, _this.itemCount || 5);
},

/**
* 查询某一页
* @param page
*/
selectPage(page) {
let _this = this;
if (page < 1) {
page = 1;
}
if (page > _this.pageTotal) {
page = _this.pageTotal;
}
if (this.page !== page) {
_this.page = page;
if (_this.list) {
_this.list(page);
}
}
},

/**
* 当前要显示在页面上的页码
* @param total
* @param current
* @param length
* @returns {Array}
*/
getPageItems(total, current, length) {
let items = [];
if (length >= total) {
for (let i = 1; i <= total; i++) {
items.push(i);
}
} else {
let base = 0;
// 前移
if (current - 0 > Math.floor((length - 1) / 2)) {
// 后移
base = Math.min(total, current - 0 + Math.ceil((length - 1) / 2)) - length;
}
for (let i = 1; i <= length; i++) {
items.push(base + i);
}
}
return items;
}
}
}
</script>

<style scoped>
.pagination {
vertical-align: middle !important;
font-size: 16px;
margin-top: 0;
margin-bottom: 10px;
}

.pagination button {
margin-right: 5px;
}

.btn-primary.active {
background-color: #2f7bba !important;
border-color: #27689d !important;
color: white !important;
font-weight: 600;
}

/*.pagination select {*/
/*vertical-align: middle !important;*/
/*font-size: 16px;*/
/*margin-top: 0;*/
/*}*/
</style>

大章管理页面使用分页组件,可自定义初始每页5条,最多显示8个按钮

views/admin/chapter.vue

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
<template>
<div>
<p>
<button v-on:click="list(1)" class="btn btn-white btn-default btn-round">
<i class="ace-icon fa fa-refresh"></i>
刷新
</button>
</p>

<pagination ref="pagination" v-bind:list="list" v-bind:itemCount="8"></pagination>

<table id="simple-table" class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>课程ID</th>
<th>操作</th>
</tr>
</thead>

<tbody>
<tr v-for="chapter in chapters">
<td>{{chapter.id}}</td>
<td>{{chapter.name}}</td>
<td>{{chapter.courseId}}</td>
<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</template>

<script>
import Pagination from "../../components/pagination";
export default {
components: {Pagination},
name: "chapter",
data: function() {
return {
chapters: []
}
},
mounted: function() {
let _this = this;
_this.$refs.pagination.size = 5;
_this.list(1);
// sidebar激活样式方法一
// this.$parent.activeSidebar("business-chapter-sidebar");

},
methods: {
list(page) {
let _this = this;
_this.$ajax.post('http://127.0.0.1:9000/business/admin/chapter/list', {
page: page,
size: _this.$refs.pagination.size,
}).then((response)=>{
console.log("查询大章列表结果:", response);
_this.chapters = response.data.list;
_this.$refs.pagination.render(page, response.data.total);

})
}
}
}
</script>

增加新增大章功能

增加新增大章功能,前端代码开发

views/admin/chapter.vue

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
<template>
<div>
<p>
<button v-on:click="add()" class="btn btn-white btn-default btn-round">
<i class="ace-icon fa fa-edit"></i>
新增
</button>
&nbsp;
<button v-on:click="list(1)" class="btn btn-white btn-default btn-round">
<i class="ace-icon fa fa-refresh"></i>
刷新
</button>
</p>

<pagination ref="pagination" v-bind:list="list" v-bind:itemCount="8"></pagination>

<table id="simple-table" class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>课程ID</th>
<th>操作</th>
</tr>
</thead>

<tbody>
<tr v-for="chapter in chapters">
<td>{{chapter.id}}</td>
<td>{{chapter.name}}</td>
<td>{{chapter.courseId}}</td>
<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>

<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">表单</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">名称</label>
<div class="col-sm-10">
<input class="form-control" placeholder="名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">课程ID</label>
<div class="col-sm-10">
<input class="form-control" placeholder="课程ID">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary">保存</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
</template>

<script>
import Pagination from "../../components/pagination";
export default {
components: {Pagination},
name: "chapter",
data: function() {
return {
chapters: []
}
},
mounted: function() {
let _this = this;
_this.$refs.pagination.size = 5;
_this.list(1);
// sidebar激活样式方法一
// this.$parent.activeSidebar("business-chapter-sidebar");

},
methods: {
add() {
let _this = this;
$(".modal").modal("show");
},
list(page) {
let _this = this;
_this.$ajax.post('http://127.0.0.1:9000/business/admin/chapter/list', {
page: page,
size: _this.$refs.pagination.size,
}).then((response)=>{
console.log("查询大章列表结果:", response);
_this.chapters = response.data.list;
_this.$refs.pagination.render(page, response.data.total);

})
}
}
}
</script>

eslint关闭no-unused-vars校验

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module.exports = {
root: true,
env: {
node: true
},
'extends': [
'plugin:vue/essential',
'eslint:recommended'
],
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-undef': 'off',
'vue/no-unused-vars': 'off',
'vue/require-v-for-key': 'off',
'no-unused-vars': 'off'
},
parserOptions: {
parser: 'babel-eslint'
}
};

增加新增大章功能,后端代码开发,完成前后端联调,保存成功

views/admin/chapter.vue

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
<template>
<div>
<p>
<button v-on:click="add()" class="btn btn-white btn-default btn-round">
<i class="ace-icon fa fa-edit"></i>
新增
</button>
&nbsp;
<button v-on:click="list(1)" class="btn btn-white btn-default btn-round">
<i class="ace-icon fa fa-refresh"></i>
刷新
</button>
</p>

<pagination ref="pagination" v-bind:list="list" v-bind:itemCount="8"></pagination>

<table id="simple-table" class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>课程ID</th>
<th>操作</th>
</tr>
</thead>

<tbody>
<tr v-for="chapter in chapters">
<td>{{chapter.id}}</td>
<td>{{chapter.name}}</td>
<td>{{chapter.courseId}}</td>
<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>

<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">表单</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">名称</label>
<div class="col-sm-10">
<input v-model="chapter.name" class="form-control" placeholder="名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">课程ID</label>
<div class="col-sm-10">
<input v-model="chapter.courseId" class="form-control" placeholder="课程ID">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
<button v-on:click="save()" type="button" class="btn btn-primary">保存</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
</template>

<script>
import Pagination from "../../components/pagination";
export default {
components: {Pagination},
name: "chapter",
data: function() {
return {
chapter: {},
chapters: []
}
},
mounted: function() {
let _this = this;
_this.$refs.pagination.size = 5;
_this.list(1);
// sidebar激活样式方法一
// this.$parent.activeSidebar("business-chapter-sidebar");

},
methods: {
add() {
let _this = this;
$(".modal").modal("show");
},

list(page) {
let _this = this;
_this.$ajax.post('http://127.0.0.1:9000/business/admin/chapter/list', {
page: page,
size: _this.$refs.pagination.size,
}).then((response)=>{
console.log("查询大章列表结果:", response);
_this.chapters = response.data.list;
_this.$refs.pagination.render(page, response.data.total);

})
},

save(page) {
let _this = this;
_this.$ajax.post('http://127.0.0.1:9000/business/admin/chapter/save', _this.chapter).then((response)=>{
console.log("保存大章列表结果:", response);
})
}
}
}
</script>

business/controller/admin/ChapterController.java

1
2
3
4
5
6
@RequestMapping("/save")
public ChapterDto save(@RequestBody ChapterDto chapterDto) {
LOG.info("chapterDto: {}", chapterDto);
chapterService.save(chapterDto);
return chapterDto;
}

server/service/ChapterService.java

1
2


server/util/UuidUtil.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
package com.javami.course.server.util;

import java.util.UUID;

public class UuidUtil {

public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z" };


/**
* 获取短UUID
* @return
*/
public static String getShortUuid() {
StringBuffer shortBuffer = new StringBuffer();
String uuid = UuidUtil.getUuid();
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]); // 对62取余
}
return shortBuffer.toString();

}

/**
* 获得32位UUID
*/
public static String getUuid(){
String uuid = UUID.randomUUID().toString();
//去掉“-”符号
return uuid.replaceAll("-", "");
}

public static void main(String[] args) {
System.out.println(getShortUuid());
}
}

增加复制工具类CopyUtil,封装BeanUtils.copyProperties,简化单实体复制和列表复制的代码

server/util/CopyUtil.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
package com.javami.course.server.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class CopyUtil {

public static <T> List<T> copyList(List source, Class<T> clazz) {
List<T> target = new ArrayList<>();
if (!CollectionUtils.isEmpty(source)){
if (!CollectionUtils.isEmpty(source)){
for (Object c: source) {
T obj = copy(c, clazz);
target.add(obj);
}
}
}
return target;
}

public static <T> T copy(Object source, Class<T> clazz) {
if (source == null) {
return null;
}
T obj = null;
try {
obj = clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
BeanUtils.copyProperties(source, obj);
return obj;
}
}

增加统一返回实体类ResponseDto,前后端代码针对ResponseDto做修改

business/controller/admin/ChapterController.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
@RestController
@RequestMapping("/admin/chapter")
public class ChapterController {

private static final Logger LOG = LoggerFactory.getLogger(ChapterController.class);

@Resource
private ChapterService chapterService;

@RequestMapping("/list")
public ResponseDto list(@RequestBody PageDto pageDto) {
LOG.info("pageDto: {}", pageDto);
ResponseDto responseDto = new ResponseDto();
chapterService.list(pageDto);
responseDto.setContent(pageDto);
return responseDto;
}

@RequestMapping("/save")
public ResponseDto save(@RequestBody ChapterDto chapterDto) {
LOG.info("chapterDto: {}", chapterDto);
ResponseDto responseDto = new ResponseDto();
chapterService.save(chapterDto);
responseDto.setContent(chapterDto);
return responseDto;
}
}

server/dto/ResponseDto.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
package com.javami.course.server.dto;

/**
* @author Kevin
* @create 2021-11-22 23:23
*/
public class ResponseDto<T> {

/**
* 业务上的成功或失败
*/
private boolean success = true;

/**
* 返回码
*/
private String code;

/**
* 返回信息
*/
private String message;

/**
* 返回泛型数据,自定义类型
*/
private T content;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public boolean getSuccess() {
return success;
}

public void setSuccess(boolean success) {
this.success = success;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public T getContent() {
return content;
}

public void setContent(T content) {
this.content = content;
}

@Override
public String toString() {
final StringBuffer sb = new StringBuffer("ResponseDto{");
sb.append("success=").append(success);
sb.append(", code='").append(code).append('\'');
sb.append(", message='").append(message).append('\'');
sb.append(", content=").append(content);
sb.append('}');
return sb.toString();
}
}

chapter保存成功后关闭表单,并刷新列表,为modal增加id属性

admin/src/views/admin/chapter.vue

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
<template>
<div>
<p>
<button v-on:click="add()" class="btn btn-white btn-default btn-round">
<i class="ace-icon fa fa-edit"></i>
新增
</button>
&nbsp;
<button v-on:click="list(1)" class="btn btn-white btn-default btn-round">
<i class="ace-icon fa fa-refresh"></i>
刷新
</button>
</p>

<pagination ref="pagination" v-bind:list="list" v-bind:itemCount="8"></pagination>

<table id="simple-table" class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>课程ID</th>
<th>操作</th>
</tr>
</thead>

<tbody>
<tr v-for="chapter in chapters">
<td>{{chapter.id}}</td>
<td>{{chapter.name}}</td>
<td>{{chapter.courseId}}</td>
<td>
<div class="hidden-sm hidden-xs btn-group">
<button class="btn btn-xs btn-success">
<i class="ace-icon fa fa-check bigger-120"></i>
</button>

<button class="btn btn-xs btn-info">
<i class="ace-icon fa fa-pencil bigger-120"></i>
</button>

<button class="btn btn-xs btn-danger">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</button>

<button class="btn btn-xs btn-warning">
<i class="ace-icon fa fa-flag bigger-120"></i>
</button>
</div>

<div class="hidden-md hidden-lg">
<div class="inline pos-rel">
<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
</button>

<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
<li>
<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
<span class="blue">
<i class="ace-icon fa fa-search-plus bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
<span class="green">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</span>
</a>
</li>

<li>
<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
<span class="red">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</span>
</a>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>

<div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">表单</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">名称</label>
<div class="col-sm-10">
<input v-model="chapter.name" class="form-control" placeholder="名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">课程ID</label>
<div class="col-sm-10">
<input v-model="chapter.courseId" class="form-control" placeholder="课程ID">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
<button v-on:click="save()" type="button" class="btn btn-primary">保存</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
</template>

<script>
import Pagination from "../../components/pagination";
export default {
components: {Pagination},
name: "chapter",
data: function() {
return {
chapter: {},
chapters: []
}
},
mounted: function() {
let _this = this;
_this.$refs.pagination.size = 5;
_this.list(1);
// sidebar激活样式方法一
// this.$parent.activeSidebar("business-chapter-sidebar");

},
methods: {
add() {
let _this = this;
$("#form-modal").modal("show");
},

list(page) {
let _this = this;
_this.$ajax.post('http://127.0.0.1:9000/business/admin/chapter/list', {
page: page,
size: _this.$refs.pagination.size,
}).then((response)=>{
console.log("查询大章列表结果:", response);
let resp = response.data;
_this.chapters = resp.content.list;
_this.$refs.pagination.render(page, resp.content.total);

})
},

save(page) {
let _this = this;
_this.$ajax.post('http://127.0.0.1:9000/business/admin/chapter/save', _this.chapter).then((response)=>{
console.log("保存大章列表结果:", response);
let resp = response.data;
if (resp.success) {
$("#form-modal").modal("hide");
_this.list(1);
}
})
}
}
}
</script>

修改删除大章功能

增加修改大章功能,新增和修改用同一个保存功能,通过传入的参数id有没有值来判断

admin/src/views/admin/chapter.vue

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
@Service
public class ChapterService {

@Resource
private ChapterMapper chapterMapper;

public void list(PageDto pageDto) {
PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
ChapterExample chapterExample = new ChapterExample();
List<Chapter> chapterList = chapterMapper.selectByExample(chapterExample);
PageInfo<Chapter> pageInfo = new PageInfo<>(chapterList);
pageDto.setTotal(pageInfo.getTotal());
// List<ChapterDto> chapterDtoList = new ArrayList<ChapterDto>();
// for (int i = 0, l = chapterList.size(); i < l; i++) {
// Chapter chapter = chapterList.get(i);
// ChapterDto chapterDto = new ChapterDto();
// BeanUtils.copyProperties(chapter, chapterDto);
// chapterDtoList.add(chapterDto);
// }
List<ChapterDto> chapterDtoList = CopyUtil.copyList(chapterList, ChapterDto.class);
pageDto.setList(chapterDtoList);
}

public void save(ChapterDto chapterDto) {
Chapter chapter = CopyUtil.copy(chapterDto, Chapter.class);
if (StringUtils.isEmpty(chapterDto.getId())) {
this.insert(chapter);
} else {
this.update(chapter);
}
}

private void insert(Chapter chapter) {
chapter.setId(UuidUtil.getShortUuid());
chapterMapper.insert(chapter);
}

private void update(Chapter chapter) {
chapterMapper.updateByPrimaryKey(chapter);
}
}

增加删除大章功能

admin/src/views/admin/chapter.vue

1
2
3
4
5
6
7
8
9
10
del(id) {
let _this = this;
_this.$ajax.delete('http://127.0.0.1:9000/business/admin/chapter/delete/' + id).then((response) => {
console.log("删除大章列表结果:", response);
let resp = response.data;
if (resp.success) {
_this.list(1);
}
})
}

business/controller/admin/ChapterController.java

1
2
3
4
5
6
7
@DeleteMapping("/delete/{id}")
public ResponseDto save(@PathVariable String id) {
LOG.info("id: {}", id);
ResponseDto responseDto = new ResponseDto();
chapterService.delete(id);
return responseDto;
}

service/ChapterService.java

1
2
3
public void delete(String id) {
chapterMapper.deleteByPrimaryKey(id);
}

集成前端通用组件

集成sweetalert2,删除时弹出确认框

admin/public/index.html

1
2

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>

admin/src/views/admin/chapter.vue

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
del(id) {
let _this = this;
Swal.fire({
title: '确认删除?',
text: "删除后不可恢复,确认删除?",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: '确认!'
}).then((result) => {
if (result.value) {
_this.$ajax.delete('http://127.0.0.1:9000/business/admin/chapter/delete/' + id).then((response) => {
console.log("删除大章列表结果:", response);
let resp = response.data;
if (resp.success) {
_this.list(1);
Swal.fire(
'删除成功!',
'删除成功!',
'success'
)
}
})

}
})

}

制作toast组件,内部用sweetalert2实现

admin/public/index.html

1
<script src="<%= BASE_URL %>static/js/toast.js"></script>

admin/public/static/js/toast.js

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
const Toast = Swal.mixin({
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 3000,
timerProgressBar: true,
onOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
});

toast = {
success: function (message) {
Toast.fire({
icon: 'success',
title: message
});
},

error: function (message) {
Toast.fire({
icon: 'error',
title: message
});
},

warning: function (message) {
Toast.fire({
icon: 'warning',
title: message
});
}
};

admin/src/views/admin/chapter.vue

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
del(id) {
let _this = this;
Swal.fire({
title: '确认删除?',
text: "删除后不可恢复,确认删除?",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: '确认!'
}).then((result) => {
if (result.value) {
_this.$ajax.delete('http://127.0.0.1:9000/business/admin/chapter/delete/' + id).then((response)=>{
console.log("删除大章列表结果:", response);
let resp = response.data;
if (resp.success) {
_this.list(1);
toast.success("删除成功!");
}
})

}
})

}

制作Loading组件,内部用jquery blockui插件实现

admin/public/index.html

1
2
3
<!--  loading等待框  -->
<script src="https://cdn.bootcss.com/jquery.blockUI/2.70.0-2014.11.23/jquery.blockUI.min.js"></script>
<script src="<%= BASE_URL %>static/js/loading.js"></script>

admin/public/static/js/loading.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Loading = {
show: function () {
$.blockUI({
message: '<img src="/static/image/loading.gif" />',
css: {
padding: "10px",
left: "50%",
width: "80px",
marginLeft: "-40px",
}
});
},
hide: function () {
// 本地查询速度太快,loading显示一瞬间,故意做个延迟
setTimeout(function () {
$.unblockUI();
}, 500)
}
};

admin/src/views/admin/chapter.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
list(page) {
let _this = this;
Loading.show();
_this.$ajax.post('http://127.0.0.1:9000/business/admin/chapter/list', {
page: page,
size: _this.$refs.pagination.size,
}).then((response)=>{
Loading.hide();
console.log("查询大章列表结果:", response);
let resp = response.data;
_this.chapters = resp.content.list;
_this.$refs.pagination.render(page, resp.content.total);

})
},

修改Toastr组件的显示效果,更大气

admin/public/index.html

1
<script src="<%= BASE_URL %>static/js/confirm.js"></script>

admin/public/static/js/confirm.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Confirm = {
show: function (message, callback) {
Swal.fire({
title: '确认?',
text: message,
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: '确认!'
}).then((result) => {
if (result.value) {
if (callback) {
callback()
}
}
})



}
}

admin/public/static/js/toast.js

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
Toast = {
success: function (message) {
Swal.fire({
position: 'top-end',
icon: 'success',
title: message,
showConfirmButton: false,
timer: 3000
})
},

error: function (message) {
Swal.fire({
position: 'top-end',
icon: 'error',
title: message,
showConfirmButton: false,
timer: 3000
})
},

warning: function (message) {
Swal.fire({
position: 'top-end',
icon: 'warning',
title: message,
showConfirmButton: false,
timer: 3000
})
}
};

views/admin/chapter.vue

1
2
3
4
5
6
7
8
9
10
11
12
Confirm.show("删除大章后不可恢复,确认删除?", function () {
Loading.show();
_this.$ajax.delete('http://127.0.0.1:9000/business/admin/chapter/delete/' + id).then((response)=>{
Loading.hide();
console.log("删除大章列表结果:", response);
let resp = response.data;
if (resp.success) {
_this.list(1);
Toast.success("删除成功!");
}
})
});

增加工具类tool.js和校验类validator.js

admin/public/index.html

1
2
3
4
<!--  通用工具类  -->
<script src="<%= BASE_URL %>static/js/tool.js"></script>
<!-- 校验类 -->
<script src="<%= BASE_URL %>static/js/validator.js"></script>

admin/public/static/js/tool.js

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
Tool = {
/**
* 空校验 null或""都返回true
*/
isEmpty: function (obj) {
if ((typeof obj == 'string')) {
return !obj || obj.replace(/\s+/g, "") == ""
} else {
return (!obj || JSON.stringify(obj) === "{}" || obj.length === 0);
}
},

/**
* 非空校验
*/
isNotEmpty: function (obj) {
return !this.isEmpty();
},

/**
* 长度校验
*/
isLength: function (str, min, max) {
return $.trim(str).length >= min && $.trim(str).length <= max;
}
};

admin/public/static/js/validator.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Validator = {
require: function (value, text) {
if (Tool.isEmpty(value)) {
Toast.warning(text + "不能为空");
return false;
} else {
return true
}
},

length: function (value, text, min, max) {
if (!Tool.isLength(value, min, max)) {
Toast.warning(text + "长度" + min + "~" + max + "位");
return false;
} else {
return true
}
}
};

大章保存非空和长度增加校验

admin/src/views/admin/chapter.vue

1
2
3
4
5
6
// 保存校验
if (!Validator.require(_this.chapter.name, "名称")
|| !Validator.require(_this.chapter.courseId, "课程ID")
|| !Validator.length(_this.chapter.courseId, "课程ID", 1, 8)) {
return;
}

增加后端校验工具类ValidatorUtil

增加统一异常处理,ControllerExceptionHandler,关键字:@ControllerAdvice

business/controller/ControllerExceptionHandler.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@ControllerAdvice
public class ControllerExceptionHandler {

private static final Logger LOG = LoggerFactory.getLogger(ControllerExceptionHandler.class);

@ExceptionHandler(value = ValidatorException.class)
@ResponseBody
public ResponseDto validatorExceptionHandler(ValidatorException e) {
ResponseDto responseDto = new ResponseDto();
responseDto.setSuccess(false);
LOG.warn(e.getMessage());
responseDto.setMessage("请求参数异常!");
return responseDto;
}
}

business/controller/admin/ChapterController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@PostMapping("/save")
public ResponseDto save(@RequestBody ChapterDto chapterDto) {
LOG.info("chapterDto: {}", chapterDto);

// 保存校验
ValidatorUtil.require(chapterDto.getName(), "名称");
ValidatorUtil.require(chapterDto.getCourseId(), "课程ID");
ValidatorUtil.length(chapterDto.getCourseId(), "课程ID", 1, 8);

ResponseDto responseDto = new ResponseDto();
chapterService.save(chapterDto);
responseDto.setContent(chapterDto);
return responseDto;
}

server/exception/ValidatorException.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.javami.course.server.exception;

/**
* @author Kevin
* @create 2021-11-23 0:46
*/
public class ValidatorException extends RuntimeException{

public ValidatorException(String message) {
super(message);
}
}

server/src/main/java/com/course/server/util/ValidatorUtil.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
package com.javami.course.server.util;

import com.javami.course.server.exception.ValidatorException;
import org.springframework.util.StringUtils;

public class ValidatorUtil {

/**
* 空校验(null or "")
*/
public static void require(String str, String fieldName) {
if (StringUtils.isEmpty(str)) {
throw new ValidatorException(fieldName + "不能为空");
}
}

/**
* 长度校验
*/
public static void length(String str, String fieldName, int min, int max) {
int length = 0;
if (!StringUtils.isEmpty(str)) {
length = str.length();
}
if (length < min || length > max) {
throw new ValidatorException(fieldName + "长度" + min + "~" + max + "位");
}
}
}

代码优化

增加日志AOP,统一日志输出

business/controller/admin/ChapterController.java

1
public static final String BUSINESS_NAME = "大章";

business/src/main/resources/logback.xml

1
2

<Pattern>%d{ss.SSS} %highlight(%-5level) %blue(%-30logger{30}:%-4line) %green(%-8X{UUID}) %msg%n</Pattern>

父pom.xml

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>

server/pom.xml

1
2
3
4
5
6
7
8
9
10
 <!-- AOP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>

server/config/LogAspect.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
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
package com.javami.course.server.config;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
import com.javami.course.server.util.UuidUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
/**
* @author Kevin
* @create 2021-11-23 0:55
*/
@Aspect
@Component
public class LogAspect {

private final static Logger LOG = LoggerFactory.getLogger(LogAspect.class);

/** 定义一个切点 */
@Pointcut("execution(public * com.javami.course.*.controller..*Controller.*(..))")
public void controllerPointcut() {}

@Before("controllerPointcut()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 日志编号
MDC.put("UUID", UuidUtil.getShortUuid());

// 开始打印请求日志
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Signature signature = joinPoint.getSignature();
String name = signature.getName();

// 打印业务操作
String nameCn = "";
if (name.contains("list") || name.contains("query")) {
nameCn = "查询";
} else if (name.contains("save")) {
nameCn = "保存";
} else if (name.contains("delete")) {
nameCn = "删除";
} else {
nameCn = "操作";
}

// 使用反射,获取业务名称
Class clazz = signature.getDeclaringType();
Field field;
String businessName = "";
try {
field = clazz.getField("BUSINESS_NAME");
if (!StringUtils.isEmpty(field)) {
businessName = (String) field.get(clazz);
}
} catch (NoSuchFieldException e) {
LOG.error("未获取到业务名称");
} catch (SecurityException e) {
LOG.error("获取业务名称失败", e);
}

// 打印请求信息
LOG.info("------------- 【{}】{}开始 -------------", businessName, nameCn);
LOG.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
LOG.info("类名方法: {}.{}", signature.getDeclaringTypeName(), name);
LOG.info("远程地址: {}", request.getRemoteAddr());

// 打印请求参数
Object[] args = joinPoint.getArgs();
Object[] arguments = new Object[args.length];
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof ServletRequest
|| args[i] instanceof ServletResponse
|| args[i] instanceof MultipartFile) {
continue;
}
arguments[i] = args[i];
}
// 排除字段,敏感字段或太长的字段不显示
String[] excludeProperties = {};
PropertyPreFilters filters = new PropertyPreFilters();
PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
excludefilter.addExcludes(excludeProperties);
LOG.info("请求参数: {}", JSONObject.toJSONString(arguments, excludefilter)); // 为空的会不打印,但是像图片等长字段也会打印
}

@Around("controllerPointcut()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
// 排除字段,敏感字段或太长的字段不显示
String[] excludeProperties = {"password"};
PropertyPreFilters filters = new PropertyPreFilters();
PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
excludefilter.addExcludes(excludeProperties);
LOG.info("返回结果: {}", JSONObject.toJSONString(result, excludefilter));
LOG.info("------------- 结束 耗时:{} ms -------------", System.currentTimeMillis() - startTime);
return result;
}

}

前端增加统一日志输出

admin/src/main.js

1
2
3
4
5
6
7
8
9
10
11
/**
* axios拦截器
*/
axios.interceptors.request.use(function (config) {
console.log("请求:", config);
return config;
}, error => {});
axios.interceptors.response.use(function (response) {
console.log("返回结果:", response);
return response;
}, error => {});