阿超
>
修改logback.xml以及mybatis-config.xml同时打印mybatis和jpa的sql
最天才的是时间。——别林斯基
项目背景是apache/shenyu,使用的是mybatis,通过shenyu-admin/src/main/resources/mybatis/mybatis-config.xml来管理相关配置
此处有一行注释了的
我们解开注释即可打印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 package org.apache.shenyu.admin.repository;import org.apache.shenyu.admin.model.entity.ApiDO;import org.springframework.data.jpa.repository.JpaRepository;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 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;@Entity @Table(name = "api") public class ApiDO { @Id 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; public String getId () { return id; } public void setId (final String id) { this .id = id; } public String getContextPath () { return contextPath; } public void setContextPath (final String contextPath) { this .contextPath = contextPath; } public String getApiPath () { return apiPath; } public void setApiPath (final String apiPath) { this .apiPath = apiPath; } public Integer getHttpMethod () { return httpMethod; } public void setHttpMethod (final Integer httpMethod) { this .httpMethod = httpMethod; } public String getConsume () { return consume; } public void setConsume (final String consume) { this .consume = consume; } public String getProduce () { return produce; } public void setProduce (final String produce) { this .produce = produce; } public String getVersion () { return version; } public void setVersion (final String version) { this .version = version; } public String getRpcType () { return rpcType; } public void setRpcType (final String rpcType) { this .rpcType = rpcType; } public Integer getState () { return state; } public void setState (final Integer state) { this .state = state; } public String getExt () { return ext; } public void setExt (final String ext) { this .ext = ext; } public String getApiOwner () { return apiOwner; } public void setApiOwner (final String apiOwner) { this .apiOwner = apiOwner; } public String getApiDesc () { return apiDesc; } public void setApiDesc (final String apiDesc) { this .apiDesc = apiDesc; } public Integer getApiSource () { return apiSource; } public void setApiSource (final Integer apiSource) { this .apiSource = apiSource; } public String getDocument () { return document; } public void setDocument (final String document) { this .document = document; } public String getDocumentMd5 () { return documentMd5; } public void setDocumentMd5 (final String documentMd5) { this .documentMd5 = documentMd5; } public Date getDateCreated () { return dateCreated; } public void setDateCreated (final Date dateCreated) { this .dateCreated = dateCreated; } public Date getDateUpdated () { return dateUpdated; } public void setDateUpdated (final Date dateUpdated) { this .dateUpdated = dateUpdated; } public static ApiDOBuilder builder () { return new ApiDOBuilder (); } 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; private ApiDOBuilder () { } public ApiDOBuilder id (final String id) { this .id = id; return this ; } public ApiDOBuilder contextPath (final String contextPath) { this .contextPath = contextPath; return this ; } public ApiDOBuilder apiPath (final String apiPath) { this .apiPath = apiPath; return this ; } public ApiDOBuilder httpMethod (final Integer httpMethod) { this .httpMethod = httpMethod; return this ; } public ApiDOBuilder consume (final String consume) { this .consume = consume; return this ; } public ApiDOBuilder produce (final String produce) { this .produce = produce; return this ; } public ApiDOBuilder version (final String version) { this .version = version; return this ; } public ApiDOBuilder rpcType (final String rpcType) { this .rpcType = rpcType; return this ; } public ApiDOBuilder state (final Integer state) { this .state = state; return this ; } public ApiDOBuilder ext (final String ext) { this .ext = ext; return this ; } public ApiDOBuilder apiOwner (final String apiOwner) { this .apiOwner = apiOwner; return this ; } public ApiDOBuilder apiDesc (final String apiDesc) { this .apiDesc = apiDesc; return this ; } public ApiDOBuilder apiSource (final Integer apiSource) { this .apiSource = apiSource; return this ; } public ApiDOBuilder document (final String document) { this .document = document; return this ; } public ApiDOBuilder documentMd5 (final String documentMd5) { this .documentMd5 = documentMd5; return this ; } public ApiDOBuilder dateCreated (final Date dateCreated) { this .dateCreated = dateCreated; return this ; } public ApiDOBuilder dateUpdated (final Date dateUpdated) { this .dateUpdated = dateUpdated; return this ; } 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 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;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; } }
此处仅修改before和testSelectByPrimaryKey为测试使用
执行发现没有jpa的sql日志输出
我们到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" ?> <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 >
再次执行,即可同时输出jpa和mbatis日志