最天才的是时间。——别林斯基

项目背景是apache/shenyu,使用的是mybatis,通过shenyu-admin/src/main/resources/mybatis/mybatis-config.xml来管理相关配置

此处有一行注释了的

1
<!-- <setting name="logImpl" value="STDOUT_LOGGING" />-->

我们解开注释即可打印mybatis日志

其次我在该项目中引入jpa

首先shenyu-admin/pom.xml添加依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

然后添加一个repository作为操作类,比如shenyu-admin/src/main/java/org/apache/shenyu/admin/repository/ApiRepository.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shenyu.admin.repository;

import org.apache.shenyu.admin.model.entity.ApiDO;
import org.springframework.data.jpa.repository.JpaRepository;

/**
* ApiRepository.
**/
public interface ApiRepository extends JpaRepository<ApiDO, String> {

}

这里需要注意一下对应的实体类需要添加@Entity@Table注解,如果要用主键相关方法还得添加一个@Id

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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shenyu.admin.model.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.model.dto.ApiDTO;
import org.apache.shenyu.common.utils.UUIDUtils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Optional;

/**
* api.
*/
@Entity
@Table(name = "api")
public class ApiDO {
/**
* primary key id.
*/
@Id
private String id;

/**
* the context_path.
*/
private String contextPath;

/**
* the apiPath.
*/
private String apiPath;

/**
* 0-get,1-head,2-post,3-put,4-patch,5-delete,6-options,7-trace.
*/
private Integer httpMethod;

/**
* specify the submitted content type for processing requests, such as application/json, text/html;.
*/
private String consume;

/**
* specify the content type to be returned. only when the (accept) type in the request header contains the specified type can it be returned;.
*/
private String produce;

/**
* api version,for example V0.01.
*/
private String version;

/**
* http,dubbo,sofa,tars,websocket,springCloud,motan,grpc.
*/
private String rpcType;

/**
* 0-unpublished1-published2-offline.
*/
private Integer state;

/**
* extended fields.
*/
private String ext;

/**
* apiOwner.
*/
private String apiOwner;

/**
* the api description.
*/
private String apiDesc;

/**
* 0-swagger,1-annotation generation,2-create manuallym,3-import swagger,4-import yapi.
*/
private Integer apiSource;

/**
* complete documentation of the api, including request parameters and response parameters.
*/
private String document;

/**
* document_md5.
*/
private String documentMd5;

/**
* create time.
*/
private Date dateCreated;

/**
* update time.
*/
private Date dateUpdated;

/**
* getId.
* @return id
*/
public String getId() {
return id;
}

/**
* set id.
* @param id id
*/
public void setId(final String id) {
this.id = id;
}

/**
* getContextPath.
* @return context path
*/
public String getContextPath() {
return contextPath;
}

/**
* set context path.
* @param contextPath context path
*/
public void setContextPath(final String contextPath) {
this.contextPath = contextPath;
}

/**
* getApiPath.
* @return apiPath
*/
public String getApiPath() {
return apiPath;
}

/**
* setApiPath.
* @param apiPath apiPath
*/
public void setApiPath(final String apiPath) {
this.apiPath = apiPath;
}

/**
* getHttpMethod.
* @return http method
*/
public Integer getHttpMethod() {
return httpMethod;
}

/**
* setHttpMethod.
* @param httpMethod http method
*/
public void setHttpMethod(final Integer httpMethod) {
this.httpMethod = httpMethod;
}

/**
* getConsume.
* @return consume
*/
public String getConsume() {
return consume;
}

/**
* setConsume.
* @param consume consume
*/
public void setConsume(final String consume) {
this.consume = consume;
}

/**
* getProduce.
* @return produce
*/
public String getProduce() {
return produce;
}

/**
* setProduce.
* @param produce the produce
*/
public void setProduce(final String produce) {
this.produce = produce;
}

/**
* getVersion.
* @return version
*/
public String getVersion() {
return version;
}

/**
* setVersion.
* @param version the version
*/
public void setVersion(final String version) {
this.version = version;
}

/**
* getRpcType.
* @return rpc type
*/
public String getRpcType() {
return rpcType;
}

/**
* setRpcType.
* @param rpcType the rpc type
*/
public void setRpcType(final String rpcType) {
this.rpcType = rpcType;
}

/**
* getState.
* @return state
*/
public Integer getState() {
return state;
}

/**
* setState.
* @param state state
*/
public void setState(final Integer state) {
this.state = state;
}

/**
* getExt.
* @return extension.
*/
public String getExt() {
return ext;
}

/**
* setExt.
* @param ext extension
*/
public void setExt(final String ext) {
this.ext = ext;
}

/**
* getApiOwner.
* @return apiOwner
*/
public String getApiOwner() {
return apiOwner;
}

/**
* setApiOwner.
* @param apiOwner apiOwner
*/
public void setApiOwner(final String apiOwner) {
this.apiOwner = apiOwner;
}

/**
* getApiDesc.
* @return apiDesc
*/
public String getApiDesc() {
return apiDesc;
}

/**
* setApiDesc.
* @param apiDesc apiDesc
*/
public void setApiDesc(final String apiDesc) {
this.apiDesc = apiDesc;
}

/**
* getApiSource.
* @return apiSource
*/
public Integer getApiSource() {
return apiSource;
}

/**
* setSource.
* @param apiSource apiSource
*/
public void setApiSource(final Integer apiSource) {
this.apiSource = apiSource;
}

/**
* getDocument.
* @return document
*/
public String getDocument() {
return document;
}

/**
* setDocument.
* @param document document
*/
public void setDocument(final String document) {
this.document = document;
}

/**
* getDocumentMd5.
* @return document md5
*/
public String getDocumentMd5() {
return documentMd5;
}

/**
* setDocumentMd5.
* @param documentMd5 documentMd5
*/
public void setDocumentMd5(final String documentMd5) {
this.documentMd5 = documentMd5;
}

/**
* getDateCreated.
* @return dateCreated
*/
public Date getDateCreated() {
return dateCreated;
}

/**
* setDateCreated.
* @param dateCreated dateCreated
*/
public void setDateCreated(final Date dateCreated) {
this.dateCreated = dateCreated;
}

/**
* getDateUpdated.
* @return dateUpdated
*/
public Date getDateUpdated() {
return dateUpdated;
}

/**
* setDateUpdated.
* @param dateUpdated dateUpdated
*/
public void setDateUpdated(final Date dateUpdated) {
this.dateUpdated = dateUpdated;
}

/**
* builder.
* @return ApiDOBuilder
*/
public static ApiDOBuilder builder() {
return new ApiDOBuilder();
}

/**
* buildApiDO.
* @param apiDTO apiDTO
* @return ApiDO
*/
public static ApiDO buildApiDO(final ApiDTO apiDTO) {

return Optional.ofNullable(apiDTO).map(item -> {
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
ApiDO apiDO = ApiDO.builder()
.contextPath(item.getContextPath())
.apiPath(item.getApiPath())
.httpMethod(item.getHttpMethod())
.consume(item.getConsume())
.produce(item.getProduce())
.version(item.getVersion())
.rpcType(item.getRpcType())
.state(item.getState())
.ext(item.getExt())
.apiOwner(item.getApiOwner())
.apiDesc(item.getApiDesc())
.apiSource(item.getApiSource())
.document(item.getDocument())
.documentMd5(DigestUtils.md5Hex(item.getDocument()))
.build();
if (StringUtils.isEmpty(item.getId())) {
apiDO.setId(UUIDUtils.getInstance().generateShortUuid());
apiDO.setDateCreated(currentTime);
} else {
apiDO.setId(item.getId());
}
return apiDO;
}).orElse(null);
}

public static final class ApiDOBuilder {

private String id;

private String contextPath;

private String apiPath;

private Integer httpMethod;

private String consume;

private String produce;

private String version;

private String rpcType;

private Integer state;

private String ext;

private String apiOwner;

private String apiDesc;

private Integer apiSource;

private String document;

private String documentMd5;

private Date dateCreated;

private Date dateUpdated;

/**
* Construct.
*/
private ApiDOBuilder() {
}

/**
* builder id.
* @param id id
* @return ApiDOBuilder
*/
public ApiDOBuilder id(final String id) {
this.id = id;
return this;
}

/**
* builder contextPath.
* @param contextPath contextPath
* @return ApiDOBuilder
*/
public ApiDOBuilder contextPath(final String contextPath) {
this.contextPath = contextPath;
return this;
}

/**
* builder apiPath.
* @param apiPath apiPath
* @return ApiDOBuilder
*/
public ApiDOBuilder apiPath(final String apiPath) {
this.apiPath = apiPath;
return this;
}

/**
* builder httpMethod.
* @param httpMethod httpMethod
* @return ApiDOBuilder
*/
public ApiDOBuilder httpMethod(final Integer httpMethod) {
this.httpMethod = httpMethod;
return this;
}

/**
* builder httpMethod.
* @param consume consume
* @return ApiDOBuilder
*/
public ApiDOBuilder consume(final String consume) {
this.consume = consume;
return this;
}

/**
* builder produce.
* @param produce produce
* @return ApiDOBuilder
*/
public ApiDOBuilder produce(final String produce) {
this.produce = produce;
return this;
}

/**
* builder version.
* @param version version
* @return ApiDOBuilder
*/
public ApiDOBuilder version(final String version) {
this.version = version;
return this;
}

/**
* builder rpcType.
* @param rpcType rpcType
* @return ApiDOBuilder
*/
public ApiDOBuilder rpcType(final String rpcType) {
this.rpcType = rpcType;
return this;
}

/**
* builder state.
* @param state state
* @return ApiDOBuilder
*/
public ApiDOBuilder state(final Integer state) {
this.state = state;
return this;
}

/**
* builder ext.
* @param ext ext
* @return ApiDOBuilder
*/
public ApiDOBuilder ext(final String ext) {
this.ext = ext;
return this;
}

/**
* builder apiOwner.
* @param apiOwner apiOwner
* @return ApiDOBuilder
*/
public ApiDOBuilder apiOwner(final String apiOwner) {
this.apiOwner = apiOwner;
return this;
}

/**
* builder apiDesc.
* @param apiDesc apiDesc
* @return ApiDOBuilder
*/
public ApiDOBuilder apiDesc(final String apiDesc) {
this.apiDesc = apiDesc;
return this;
}

/**
* builder apiSource.
* @param apiSource apiSource
* @return ApiDOBuilder
*/
public ApiDOBuilder apiSource(final Integer apiSource) {
this.apiSource = apiSource;
return this;
}

/**
* builder document.
* @param document document
* @return ApiDOBuilder
*/
public ApiDOBuilder document(final String document) {
this.document = document;
return this;
}

/**
* builder documentMd5.
* @param documentMd5 documentMd5
* @return ApiDOBuilder
*/
public ApiDOBuilder documentMd5(final String documentMd5) {
this.documentMd5 = documentMd5;
return this;
}

/**
* builder dateCreated.
* @param dateCreated dateCreated
* @return ApiDOBuilder
*/
public ApiDOBuilder dateCreated(final Date dateCreated) {
this.dateCreated = dateCreated;
return this;
}

/**
* builder dateUpdated.
* @param dateUpdated dateUpdated
* @return ApiDOBuilder
*/
public ApiDOBuilder dateUpdated(final Date dateUpdated) {
this.dateUpdated = dateUpdated;
return this;
}

/**
* builder.
* @return ApiDO
*/
public ApiDO build() {
ApiDO apiDO = new ApiDO();
apiDO.setId(id);
apiDO.setContextPath(contextPath);
apiDO.setApiPath(apiPath);
apiDO.setHttpMethod(httpMethod);
apiDO.setConsume(consume);
apiDO.setProduce(produce);
apiDO.setVersion(version);
apiDO.setRpcType(rpcType);
apiDO.setState(state);
apiDO.setExt(ext);
apiDO.setApiOwner(apiOwner);
apiDO.setApiDesc(apiDesc);
apiDO.setApiSource(apiSource);
apiDO.setDocument(document);
apiDO.setDocumentMd5(documentMd5);
apiDO.setDateCreated(dateCreated);
apiDO.setDateUpdated(dateUpdated);
return apiDO;
}
}
}

我们到一个测试类shenyu-admin/src/test/java/org/apache/shenyu/admin/mapper/ApiMapperTest.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
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shenyu.admin.mapper;

import jakarta.annotation.Resource;
import org.apache.shenyu.admin.AbstractSpringIntegrationTest;
import org.apache.shenyu.admin.model.entity.ApiDO;
import org.apache.shenyu.admin.model.page.PageParameter;
import org.apache.shenyu.admin.model.query.ApiQuery;
import org.apache.shenyu.admin.repository.ApiRepository;
import org.apache.shenyu.common.utils.UUIDUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Test cases for ApiMapper.
*/
public final class ApiMapperTest extends AbstractSpringIntegrationTest {

@Resource
private ApiMapper apiMapper;

@Resource
private ApiRepository apiRepository;

private final ApiDO apiDO = buildApiDO();

@BeforeEach
public void before() {
ApiDO api = apiRepository.save(apiDO);
assertNotNull(api);
}

@Test
public void testInsert() {
ApiDO newApiDO = buildApiDO();
int count = apiMapper.insert(newApiDO);
assertEquals(1, count);
}

@Test
public void testInsertSelective() {
ApiDO newApiDO = buildApiDO();
int count = apiMapper.insertSelective(newApiDO);
assertEquals(1, count);
}

@Test
public void testSelectByPrimaryKey() {
ApiDO apiDO = apiMapper.selectByPrimaryKey(this.apiDO.getId());
assertNotNull(apiDO);
apiDO = apiRepository.findById(this.apiDO.getId()).orElse(null);
assertNotNull(apiDO);
}

@Test
public void testUpdateByPrimaryKeySelective() {
Timestamp now = new Timestamp(System.currentTimeMillis());
this.apiDO.setDateUpdated(now);
this.apiDO.setContextPath("/dubbo1");
this.apiDO.setApiPath("/demo/findById1");
this.apiDO.setHttpMethod(1);
this.apiDO.setVersion("V0.02");
this.apiDO.setRpcType("dubbo1");
this.apiDO.setState(1);
int count = apiMapper.updateByPrimaryKeySelective(this.apiDO);
assertEquals(1, count);
}

@Test
public void testUpdateByPrimaryKey() {
Timestamp now = new Timestamp(System.currentTimeMillis());
this.apiDO.setDateUpdated(now);
this.apiDO.setContextPath("/dubbo2");
this.apiDO.setApiPath("/demo/findById2");
this.apiDO.setHttpMethod(2);
this.apiDO.setVersion("V0.03");
this.apiDO.setRpcType("dubbo2");
this.apiDO.setState(2);
this.apiDO.setApiSource(3);
int count = apiMapper.updateByPrimaryKeySelective(this.apiDO);
assertEquals(1, count);
}

@Test
public void testSelectByQuery() {
ApiQuery query = new ApiQuery();
query.setState(0);
query.setApiPath("/demo/findById");
query.setPageParameter(new PageParameter(1, 10));
List<ApiDO> apiDOS = apiMapper.selectByQuery(query);
assertThat(apiDOS.size(), greaterThan(0));
}

@Test
public void testSelectByIds() {
List<String> strings = Collections.singletonList(apiDO.getId());
List<ApiDO> apiDOS = apiMapper.selectByIds(strings);
assertThat(apiDOS.size(), greaterThan(0));
}

@Test
public void testDeleteByPrimaryKey() {
int count = apiMapper.deleteByPrimaryKey(this.apiDO.getId());
assertEquals(1, count);
}

@Test
public void testDeleteByIds() {
ApiDO newApiDO = buildApiDO();
apiMapper.insertSelective(newApiDO);
List<String> ids = Collections.singletonList(newApiDO.getId());
int deleteRows = apiMapper.deleteByIds(ids);
assertEquals(1, deleteRows);
}

private ApiDO buildApiDO() {
Timestamp now = new Timestamp(System.currentTimeMillis());
ApiDO apiDO = new ApiDO();
apiDO.setId(UUIDUtils.getInstance().generateShortUuid());
apiDO.setContextPath("/dubbo");
apiDO.setApiPath("/demo/findById");
apiDO.setHttpMethod(2);
apiDO.setConsume("application/json");
apiDO.setProduce("accept");
apiDO.setVersion("V0.01");
apiDO.setRpcType("dubbo");
apiDO.setState(0);
apiDO.setApiOwner("admin");
apiDO.setApiDesc("hello world api");
apiDO.setDocument("{\"request\":{\"id\":\"123\"},\"response\":{\"id\":\"123\"}}");
apiDO.setDocumentMd5("933833690e1710c7c1f26f9a9cb84659");
apiDO.setApiSource(2);
apiDO.setDateCreated(now);
apiDO.setDateUpdated(now);
return apiDO;
}
}

此处仅修改beforetestSelectByPrimaryKey为测试使用

执行发现没有jpasql日志输出

我们到shenyu-admin/src/main/resources/logback.xml添加<logger name="org.hibernate.SQL" level="DEBUG"/>这么一个配置即可

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
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<configuration>

<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
<property name="LOG_HOME" value="${log.home:-./logs}"/>
<property name="LOG_APP_NAME" value="${log.app-name:-shenyu-admin}"/>
<property name="MAX_SINGLE_LOG_FILE_SIZE" value="${log.max-single-log-file-size:-100MB}"/>
<property name="MAX_LOG_FILE_HISTORY" value="${log.max-log-file-history:-7}"/>
<property name="LOG_FILE_TOTAL_CAPACITY" value="${log.log-file-total-capacity:-10GB}"/>
<property name="ASYNC_DISCARDING_THRESHOLD" value="${log.async.discarding-threshold:-0}"/>
<property name="ASYNC_LOG_QUEUE_SIZE" value="${log.async.queue-size:-256}"/>

<property name="LOG_PATTERN" value="%red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger) - %cyan(%msg%n)"/>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>

<appender name="ASYNC_STDOUT" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>${ASYNC_DISCARDING_THRESHOLD}</discardingThreshold>
<queueSize>${ASYNC_LOG_QUEUE_SIZE}</queueSize>
<includeCallerData>true</includeCallerData>
<appender-ref ref="STDOUT"/>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${LOG_APP_NAME}.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/${LOG_APP_NAME}.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>${MAX_SINGLE_LOG_FILE_SIZE}</maxFileSize>
<maxHistory>${MAX_LOG_FILE_HISTORY}</maxHistory>
<totalSizeCap>${LOG_FILE_TOTAL_CAPACITY}</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>

<appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>${ASYNC_DISCARDING_THRESHOLD}</discardingThreshold>
<queueSize>${ASYNC_LOG_QUEUE_SIZE}</queueSize>
<includeCallerData>true</includeCallerData>
<appender-ref ref="FILE"/>
</appender>

<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_HOME}/${LOG_APP_NAME}-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/${LOG_APP_NAME}.%d{yyyy-MM-dd}-error.%i.log.zip</fileNamePattern>
<maxFileSize>${MAX_SINGLE_LOG_FILE_SIZE}</maxFileSize>
<maxHistory>${MAX_LOG_FILE_HISTORY}</maxHistory>
<totalSizeCap>${LOG_FILE_TOTAL_CAPACITY}</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>

<appender name="ASYNC_ERROR_FILE" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>${ASYNC_DISCARDING_THRESHOLD}</discardingThreshold>
<queueSize>${ASYNC_LOG_QUEUE_SIZE}</queueSize>
<includeCallerData>true</includeCallerData>
<appender-ref ref="ERROR_FILE"/>
</appender>

<root level="INFO">
<appender-ref ref="ASYNC_STDOUT"/>
<appender-ref ref="ASYNC_FILE"/>
<appender-ref ref="ASYNC_ERROR_FILE"/>
</root>
<logger name="org.hibernate.SQL" level="DEBUG"/>
</configuration>

再次执行,即可同时输出jpambatis日志