{"id":15019315,"url":"https://github.com/wangji92/mybatis-sql-log","last_synced_at":"2025-08-10T15:23:02.927Z","repository":{"id":38344183,"uuid":"205487816","full_name":"WangJi92/mybatis-sql-log","owner":"WangJi92","description":"spring boot 打印mybaits 完整的sql 日志 插件,支持spring boot2.x pagehelp mybaits-generator mybatis-plus","archived":false,"fork":false,"pushed_at":"2024-04-27T05:21:45.000Z","size":359,"stargazers_count":55,"open_issues_count":1,"forks_count":13,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T23:51:21.001Z","etag":null,"topics":["mybaits","mybaits-generator","mybatis-sql","pagehelper","print-sql-log","spring-boot","spring-boot-starter","sql","sql-log"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WangJi92.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-31T03:07:41.000Z","updated_at":"2025-01-08T01:17:39.000Z","dependencies_parsed_at":"2024-04-27T06:24:35.640Z","dependency_job_id":"b952bc06-7b85-4332-879b-7ea2f65d318c","html_url":"https://github.com/WangJi92/mybatis-sql-log","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/WangJi92/mybatis-sql-log","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WangJi92%2Fmybatis-sql-log","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WangJi92%2Fmybatis-sql-log/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WangJi92%2Fmybatis-sql-log/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WangJi92%2Fmybatis-sql-log/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WangJi92","download_url":"https://codeload.github.com/WangJi92/mybatis-sql-log/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WangJi92%2Fmybatis-sql-log/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269740901,"owners_count":24467872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mybaits","mybaits-generator","mybatis-sql","pagehelper","print-sql-log","spring-boot","spring-boot-starter","sql","sql-log"],"created_at":"2024-09-24T19:53:19.453Z","updated_at":"2025-08-10T15:23:02.862Z","avatar_url":"https://github.com/WangJi92.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mybatis-sql-log\n\n## 支持spring boot  spring pageHelp,将完整日志打印出来\n\n\u003e mybatis-sql-log 主要是为了打印mybatis 完整的sql语句，通过mybaits 提供的插件的方式进行拦截，\n\u003e 获取内部执行的sql，并将完整的sql语句打印出来。\n\nspring boot 其实可以的打印log日志出来的只要将当前的包日志打印为debug也是可以，只是参数不够完整，直接复制查询sql 不太友好。\neg: logging.level.com.boot.mybatis.mybatisdemo=debug\n\n\u003ca name=\"VIcSM\"\u003e\u003c/a\u003e\n## 1、使用\n[mybaits sql log Demo 工程](https://github.com/WangJi92/mybatis-log-demo/blob/master/README.md)\n\n插件可以在maven 仓库中搜索  mybatis-sql-log\n[阿里云搜索](https://maven.aliyun.com/mvn/search)\n```xml\n         \u003cdependency\u003e\n            \u003cgroupId\u003ecom.github.WangJi92\u003c/groupId\u003e\n            \u003cartifactId\u003emybatis-sql-log\u003c/artifactId\u003e\n            \u003cversion\u003e1.0.6\u003c/version\u003e\n        \u003c/dependency\u003e\n```\n\n实现原理主要类：com.mybatis.spring.boot.autoconfigure.MybatisSqlCompletePrintInterceptor\n```xml\n        \u003cdependency\u003e\n            \u003cgroupId\u003eorg.mybatis.spring.boot\u003c/groupId\u003e\n            \u003cartifactId\u003emybatis-spring-boot-starter\u003c/artifactId\u003e\n            \u003cversion\u003e2.1.0\u003c/version\u003e\n        \u003c/dependency\u003e\n         \u003cdependency\u003e\n            \u003cgroupId\u003ecom.github.WangJi92\u003c/groupId\u003e\n            \u003cartifactId\u003emybatis-sql-log\u003c/artifactId\u003e\n            \u003cversion\u003e1.0.6\u003c/version\u003e\n        \u003c/dependency\u003e\n```\nmybats.print=true 使用spring boot 工程集成\n\norg.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration 内部自动集成了插件\n```xml\nmybatis.print=true\nserver.port = 7012\n#数据库连接\nspring.datasource.url = jdbc:mysql://127.0.0.1:3306/test\nspring.datasource.username = root\nspring.datasource.password = mysql-root\nspring.datasource.driver-class-name= com.mysql.jdbc.Driver\nspring.datasource.hikari.connection-test-query = SELECT 1\n```\n效果，可以打印完整的SQL信息 【Mybatis Print SQL】【 select name, age, type from user WHERE ( name = '汪吉' ) 】   执行耗时=42\n\n```xml\n2019-08-31 16:58:21.996  INFO 8762 --- [nio-7012-exec-1] s.b.a.MybatisSqlCompletePrintInterceptor : 【Mybatis Print SQL】【 select name, age, type from user WHERE ( name = '汪吉' ) 】   执行耗时=42\n```\n![MyBatis Log 打印效果](https://github.com/WangJi92/mybatis-sql-log/blob/master/src/main/resources/image/mybatis-log.png)\n或者通过mybatis-config原生配置处理\n```text\n\u003c!-- mybatis-config.xml --\u003e\n\u003cplugins\u003e\n  \u003cplugin interceptor=\"com.mybatis.spring.boot.autoconfigure.MybatisSqlCompletePrintInterceptor\"\u003e\n  \u003c/plugin\u003e\n\u003c/plugins\n```\n\n\u003ca name=\"d5d3a790\"\u003e\u003c/a\u003e\n## 2、mybatis 官方插件\n\n- [插件（plugins）](http://www.mybatis.org/mybatis-3/zh/configuration.html#plugins)\u003cbr /\u003e\nMyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下，MyBatis 允许使用插件来拦截的方法调用包括：\u003cbr /\u003e\nExecutor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)\u003cbr /\u003e\nParameterHandler (getParameterObject, setParameters)\u003cbr /\u003e\nResultSetHandler (handleResultSets, handleOutputParameters)\u003cbr /\u003e\nStatementHandler (prepare, parameterize, batch, update, query)\n\n\u003ca name=\"31742b2c\"\u003e\u003c/a\u003e\n## 3、相关参考 Documentation\n\n- [Mybatis插件-sql日志美化输出](https://my.oschina.net/junjunyuanyuankeke/blog/1975439)\n- [MyBatis插件及示例----打印每条SQL语句及其执行时间](https://www.cnblogs.com/Xrq730/P/6972268.Html)\n\n上面的两篇文章整体上思路都是一致的主要是在处理参数的问题上不是很完善。这里通过mybatis 自带的ParameterHandler处理参数就比较的简单，\u003cbr /\u003e而不是通过各种复杂的判断进行处理。org/apache/ibatis/scripting/defaults/DefaultParameterHandler 这里提供了很完善的参数处理。\u003cbr /\u003e如下的代码，可以清晰的了解整个参数的解析过程，无论是动态的参数，还是对象参数，都可以完善的处理参数的解析过程。\n\n```\npublic void setParameters(PreparedStatement ps) {\n    ErrorContext.instance().activity(\"setting parameters\").object(mappedStatement.getParameterMap().getId());\n    List\u003cParameterMapping\u003e parameterMappings = boundSql.getParameterMappings();\n    if (parameterMappings != null) {\n      for (int i = 0; i \u003c parameterMappings.size(); i++) {\n        ParameterMapping parameterMapping = parameterMappings.get(i);\n        if (parameterMapping.getMode() != ParameterMode.OUT) {\n          Object value;\n          String propertyName = parameterMapping.getProperty();\n          if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params\n            value = boundSql.getAdditionalParameter(propertyName);\n          } else if (parameterObject == null) {\n            value = null;\n          } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {\n            value = parameterObject;\n          } else {\n            MetaObject metaObject = configuration.newMetaObject(parameterObject);\n            value = metaObject.getValue(propertyName);\n          }\n          TypeHandler typeHandler = parameterMapping.getTypeHandler();\n          JdbcType jdbcType = parameterMapping.getJdbcType();\n          if (value == null \u0026\u0026 jdbcType == null) {\n            jdbcType = configuration.getJdbcTypeForNull();\n          }\n          try {\n            typeHandler.setParameter(ps, i + 1, value, jdbcType);\n          } catch (TypeException e) {\n            throw new TypeException(\"Could not set parameters for mapping: \" + parameterMapping + \". Cause: \" + e, e);\n          } catch (SQLException e) {\n            throw new TypeException(\"Could not set parameters for mapping: \" + parameterMapping + \". Cause: \" + e, e);\n          }\n        }\n      }\n    }\n  }\n```\n\n\u003ca name=\"9a0fb259\"\u003e\u003c/a\u003e\n## 4、mybatis在执行期间，主要有四大核心接口对象：\n\n执行器Executor，执行器负责整个SQL执行过程的总体控制。\u003cbr /\u003e参数处理器ParameterHandler，参数处理器负责PreparedStatement入参的具体设置。\u003cbr /\u003e语句处理器StatementHandler，语句处理器负责和JDBC层具体交互，包括prepare语句，执行语句，以及调用ParameterHandler.parameterize()设置参数。\u003cbr /\u003e结果集处理器ResultSetHandler，结果处理器负责将JDBC查询结果映射到java对象。\n\n了解了这四大对象，其实我们的处理本次这个插件十分的重要，插件对象可以在Executor、或者StatementHandler两个上面处理。\n\n## plugins 实现了两种\n分别在两个分支中实现，可以根据自己的需求进行处理\n* [Executor plugins 方式拦截](https://github.com/WangJi92/mybatis-sql-log/tree/Executor)\n* [StatementHandler plugins 方式拦截](https://github.com/WangJi92/mybatis-sql-log/tree/StatementHandler)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangji92%2Fmybatis-sql-log","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwangji92%2Fmybatis-sql-log","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangji92%2Fmybatis-sql-log/lists"}