介绍

  • 对 p6spy 的进行了简单配置,然后实现引入即用
  • p6spy 是对sql的监视
  • 如果改模块不能满足你的功能、
    • 要么改本模块的配置文件
    • 要么自己依赖原生的p6spy进行配置
  • 线上不建议用这个,可能会对性能造成影响

    引入依赖

    1. <!-- p6spy -->
    2. <dependency>
    3. <groupId>cn.jdevelops</groupId>
    4. <artifactId>logs-cache-p6spy</artifactId>
    5. <version>2.0.2</version>
    6. </dependency>

    使用

  • 配置只需要简单的把数据库连接改下就行了

    spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver spring.datasource.url=jdbc:p6spy:mysql:

  1. server.port=1238
  2. spring.jpa.show-sql=true
  3. spring.jpa.hibernate.ddl-auto=update
  4. spring.datasource.password=root
  5. spring.datasource.username=root
  6. spring.datasource.url=jdbc:p6spy:mysql://192.168.0.3:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
  7. #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  8. spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver

示例测试

在测试jpa的时候一起用了

  1. @PostMapping("dyFind")
  2. public ResultVO<List<UserVO>> dyFind(@RequestBody UserFindDTO user){
  3. User to = user.to(User.class);
  4. JPAUtilExpandCriteria<User> selectBean = CommUtils.getSelectBean(to);
  5. List<User> all = userService.getJpaBasicsDao().findAll(selectBean);
  6. return ResultVO.successForData(User.to(all, UserVO.class));
  7. }
  • 效果
    1. 2021-12-10 16:37:57 |INFO |p6spy:60 |=====================================================
    2. 连接id1
    3. 当前时间:2021-12-10 16:37:57.156
    4. 类别:statement
    5. 花费时间(ms):2
    6. 预编译sqlselect user0_.id as id1_0_, user0_.create_time as create_t2_0_, user0_.create_user_name as create_u3_0_, user0_.update_time as update_t4_0_, user0_.update_user_name as update_u5_0_, user0_.address as address6_0_, user0_.login_name as login_na7_0_, user0_.login_pwd as login_pw8_0_, user0_.name as name9_0_, user0_.phone as phone10_0_, user0_.user_icon as user_ic11_0_, user0_.user_no as user_no12_0_ from sys_user user0_ where user0_.user_no like ? or user0_.name like ?
    7. 最终执行的sqlselect user0_.id as id1_0_, user0_.create_time as create_t2_0_, user0_.create_user_name as create_u3_0_, user0_.update_time as update_t4_0_, user0_.update_user_name as update_u5_0_, user0_.address as address6_0_, user0_.login_name as login_na7_0_, user0_.login_pwd as login_pw8_0_, user0_.name as name9_0_, user0_.phone as phone10_0_, user0_.user_icon as user_ic11_0_, user0_.user_no as user_no12_0_ from sys_user user0_ where user0_.user_no like '%146664%' or user0_.name like '%超%'
    8. =====================================================

    示例项目地址

    在测试jpa的时候一起用了 https://github.com/en-o/Jdevelops-Example/tree/main/HJPA