p6spy oracle,MyBatis集成P6Spy显示实际的SQL(代码教程)「建议收藏」

p6spy oracle,MyBatis集成P6Spy显示实际的SQL(代码教程)「建议收藏」在应用程序开发过程中,为了方便调试,通常都需要知道在DAO层,程序执行的SQL是什么,而P6spy这个组件正是提供了该功能。接下来将详细介绍P6Spy的详细使用。一系统集成P6spy1添加依赖3.6.01.1.6p6spyp6spy${p6spy.version}com.alibabadruid${druid.version}2实现自定义的SQL输出格式为了输出的内容足够的简洁,这里只保留了…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

在应用程序开发过程中,为了方便调试,通常都需要知道在DAO层,程序执行的SQL是什么,而P6spy这个组件正是提供了该功能。

接下来将详细介绍P6Spy的详细使用。

一 系统集成P6spy

1 添加依赖

3.6.0

1.1.6

p6spy

p6spy

${p6spy.version}

com.alibaba

druid

${druid.version}

2 实现自定义的SQL输出格式

为了输出的内容足够的简洁,这里只保留了当期那时间,执行SQL的耗时以及执行的SQL语句

package net.ittimeline.mybatis.practices.core;

import com.alibaba.druid.sql.SQLUtils;

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

/**

* @author tony ittimeline@163.com

* @date 2018-01-30-下午10:45

* @website wwww.ittimeline.net

* @see

* @since JDK8u162

*/

public class CustomizeLineFormat implements MessageFormattingStrategy {

public String buildMessage(String now, long elapsed, String sql) {

StringBuffer content = new StringBuffer();

if (org.apache.commons.lang3.StringUtils.isNotEmpty(now) && org.apache.commons.lang3.StringUtils.isNotEmpty(Long.valueOf(elapsed).toString())

&& org.apache.commons.lang3.StringUtils.isNotEmpty(sql)) {

content.append(“当前时间:” + now);

content.append(” SQL执行耗时(毫秒)为” + elapsed);

content.append(” SQL执行的语句是n” + SQLUtils.formatMySql(sql)+”nn”);

}

return content.toString();

}

@Override

public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql) {

return buildMessage(now, elapsed, sql);

}

}

3 配置spy.properties

该文件只需要配置

modulelist,logfile,logMessageFormat,dateformat即可。

###

# #%L

# P6Spy

# %%

# Copyright (C) 2013 P6Spy

# %%

# Licensed 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

#

# https://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.

# #L%

###

#################################################################

# P6Spy Options File #

# See documentation for detailed instructions #

# https://p6spy.github.io/p6spy/2.0/configandusage.html #

#################################################################

#################################################################

# MODULES #

# #

# Module list adapts the modular functionality of P6Spy. #

# Only modules listed are active. #

# (default is com.p6spy.engine.logging.P6LogFactory and #

# com.p6spy.engine.spy.P6SpyFactory) #

# Please note that the core module (P6SpyFactory) can’t be #

# deactivated. #

# Unlike the other properties, activation of the changes on #

# this one requires reload. #

#################################################################

#modulelist=com.p6spy.engine.spy.P6SpyFactory,com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory

modulelist=com.p6spy.engine.spy.P6SpyFactory,com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory

################################################################

# CORE (P6SPY) PROPERTIES #

################################################################

# A comma separated list of JDBC drivers to load and register.

# (default is empty)

#

# Note: This is normally only needed when using P6Spy in an

# application server environment with a JNDI data source or when

# using a JDBC driver that does not implement the JDBC 4.0 API

# (specifically automatic registration).

#driverlist=

#url=”jdbc:p6spy:oracle:thin:@localhost:1521:orcl” u5176u4e2du7684p6spyu5fc5u987bu8981uff0cu56e0u4e3au8fd9u6837u624du4f1au88abu62e6u622a

#drive=com.p6spy.engine.spy.P6SpyDriver

driverlist=com.mysql.jdbc.Driver

# for flushing per statement

# (default is false)

#autoflush = false

# prints a stack trace for every statement logged

#stacktrace=false

# if stacktrace=true, specifies the stack trace to print

#stacktraceclass=

# determines if property file should be reloaded

# Please note: reload means forgetting all the previously set

# settings (even those set during runtime – via JMX)

# and starting with the clean table

# (default is false)

#reloadproperties=false

# determines how often should be reloaded in seconds

# (default is 60)

#reloadpropertiesinterval=60

# specifies the appender to use for logging

# Please note: reload means forgetting all the previously set

# settings (even those set during runtime – via JMX)

# and starting with the clean table

# (only the properties read from the configuration file)

# (default is com.p6spy.engine.spy.appender.FileLogger)

#appender=com.p6spy.engine.spy.appender.Slf4JLogger

#appender=com.p6spy.engine.spy.appender.StdoutLogger

#appender=com.p6spy.engine.spy.appender.FileLogger

# name of logfile to use, note Windows users should make sure to use forward slashes in their pathname (e:/test/spy.log)

# (used for com.p6spy.engine.spy.appender.FileLogger only)

# (default is spy.log)

#logfile = spy.log

#windows u7528u6237u8bf7u7528 C:/spy.log u683cu5f0fuff0cu4e0du8981u7528

logfile=mybatis-practices-core.log

# append to the p6spy log file. if this is set to false the

# log file is truncated every time. (file logger only)

# (default is true)

uff03u8ffdu52a0u5230 p6spy u65e5u5fd7u6587u4ef6u3002u5982u679cu8bbeu7f6eu4e3afalse

uff03u65e5u5fd7u6587u4ef6u88abu622au65adu6bcfu6b21u3002 uff08u53eau9650u4e8eu6587u4ef6u8bb0u5f55u5668uff09

uff03uff08u9ed8u8ba4u4e3atrueuff09

#append=true

# class to use for formatting log messages (default is: com.p6spy.engine.spy.appender.SingleLineFormat)

#u591au884cu8f93u51fau4e3a com.p6spy.engine.spy.appender.MultiLineFormat

#u884cu7684u65f6u95f4u683cu5f0fu5316u914du7f6eu5728 dateformat u8bbeu5b9a

#current time|execution time|category|connection id|statement SQL String|effective SQL string

#u65e5u5fd7u683cu5f0fu4e3a u5f53u524du65f6u95f4u6233|u6267u884cu8bedu53e5u82b1u8d39u7684u65f6u95f4|u8c03u7528u7684u7c7bu522b|connectionId|u58f0u660eSQLu5b57u7b26u4e32|u6709u6548u7684SQLu5b57u7b26u4e32

#logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat

logMessageFormat= net.ittimeline.mybatis.practices.core.CustomizeLineFormat

# format that is used for logging of the date/time/… (has to be compatible with java.text.SimpleDateFormat)

# (default is dd-MMM-yy)

databaseDialectDateFormat=yyyy-MM-dd HH:mm:ss

# sets the date format using Java’s SimpleDateFormat routine.

# In case property is not set, miliseconds since 1.1.1970 (unix time) is used (default is empty)

#dateformat=

dateformat=yyyy-MM-dd HH:mm:ss

# whether to expose options via JMX or not

# (default is true)

#jmx=true

# if exposing options via jmx (see option: jmx), what should be the prefix used?

# jmx naming pattern constructed is: com.p6spy(.)?:name=

# please note, if there is already such a name in use it would be unregistered first (the last registered wins)

# (default is none)

#jmxPrefix=

#################################################################

# DataSource replacement #

# #

# Replace the real DataSource class in your application server #

# configuration with the name com.p6spy.engine.spy.P6DataSource #

# (that provides also connection pooling and xa support). #

# then add the JNDI name and class name of the real #

# DataSource here #

# #

# Values set in this item cannot be reloaded using the #

# reloadproperties variable. Once it is loaded, it remains #

# in memory until the application is restarted. #

# #

#################################################################

#realdatasource=/RealMySqlDS

#realdatasourceclass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource

#################################################################

# DataSource properties #

# #

# If you are using the DataSource support to intercept calls #

# to a DataSource that requires properties for proper setup, #

# define those properties here. Use name value pairs, separate #

# the name and value with a semicolon, and separate the #

# pairs with commas. #

# #

# The example shown here is for mysql #

# #

#################################################################

#realdatasourceproperties=port;3306,serverName;myhost,databaseName;jbossdb,foo;bar

#################################################################

# JNDI DataSource lookup #

# #

# If you are using the DataSource support outside of an app #

# server, you will probably need to define the JNDI Context #

# environment. #

# #

# If the P6Spy code will be executing inside an app server then #

# do not use these properties, and the DataSource lookup will #

# use the naming context defined by the app server. #

# #

# The two standard elements of the naming environment are #

# jndicontextfactory and jndicontextproviderurl. If you need #

# additional elements, use the jndicontextcustom property. #

# You can define multiple properties in jndicontextcustom, #

# in name value pairs. Separate the name and value with a #

# semicolon, and separate the pairs with commas. #

# #

# The example shown here is for a standalone program running on #

# a machine that is also running JBoss, so the JDNI context #

# is configured for JBoss (3.0.4). #

# #

# (by default all these are empty) #

#################################################################

#jndicontextfactory=org.jnp.interfaces.NamingContextFactory

#jndicontextproviderurl=localhost:1099

#jndicontextcustom=java.naming.factory.url.pkgs;org.jboss.nameing:org.jnp.interfaces

#jndicontextfactory=com.ibm.websphere.naming.WsnInitialContextFactory

#jndicontextproviderurl=iiop://localhost:900

################################################################

# P6 LOGGING SPECIFIC PROPERTIES #

################################################################

# filter what is logged

# please note this is a precondition for usage of: include/exclude/sqlexpression

# (default is false)

#filter=false

# comma separated list of strings to include

# please note that special characters escaping (used in java) has to be done for the provided regular expression

# (default is empty)

#include =

# comma separated list of strings to exclude

# (default is empty)

#exclude =

# sql expression to evaluate if using regex

# please note that special characters escaping (used in java) has to be done for the provided regular expression

# (default is empty)

#sqlexpression =

#list of categories to exclude: error, info, batch, debug, statement,

#commit, rollback and result are valid values

# (default is info,debug,result,resultset,batch)

#excludecategories=info,debug,result,resultset,batch

# Execution threshold applies to the standard logging of P6Spy.

# While the standard logging logs out every statement

# regardless of its execution time, this feature puts a time

# condition on that logging. Only statements that have taken

# longer than the time specified (in milliseconds) will be

# logged. This way it is possible to see only statements that

# have exceeded some high water mark.

# This time is reloadable.

#

# executionThreshold=integer time (milliseconds)

# (default is 0)

#executionThreshold=

################################################################

# P6 OUTAGE SPECIFIC PROPERTIES #

################################################################

# Outage Detection

#

# This feature detects long-running statements that may be indicative of

# a database outage problem. If this feature is turned on, it will log any

# statement that surpasses the configurable time boundary during its execution.

# When this feature is enabled, no other statements are logged except the long

# running statements. The interval property is the boundary time set in seconds.

# For example, if this is set to 2, then any statement requiring at least 2

# seconds will be logged. Note that the same statement will continue to be logged

# for as long as it executes. So if the interval is set to 2, and the query takes

# 11 seconds, it will be logged 5 times (at the 2, 4, 6, 8, 10 second intervals).

#

# outagedetection=true|false

# outagedetectioninterval=integer time (seconds)

#

# (default is false)

#outagedetection=false

# (default is 60)

#outagedetectioninterval=30

二 MyBatis集成P6spy

MyBatis集成P6spy,这里倒不如说成DataSource集成P6Spy更贴切,

这里增加一个名为database.properties的文件,内容如下

jdbc.driver=com.p6spy.engine.spy.P6SpyDriver

jdbc.url=jdbc:p6spy:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=false

jdbc.username=root

jdbc.password=guanglei

主要包含了数据库的连接信息,和之前的配置不同的是更换了驱动实现以及数据库连接地址。而mybatis-config.xml文件中只需要增加一行配置,然后就可以采用${属性名}的方式获取数据库配置了,配置如下所示

然后运行ContryMapperTest.selectAll()方法,会发现在模块的根路径下存在一个mybatis-practices-core.log的文件,内容如下

当前时间:2018-01-30 23:03:07 SQL执行耗时(毫秒)为0 SQL执行的语句是

SELECT country_id AS countryId, country_name AS countryName, country_code AS countryCode

FROM t_country

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/188510.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 多图详解缓冲区溢出问题

    多图详解缓冲区溢出问题蠕虫病毒是一种常见的利用Unix系统中的缺点来进行攻击的病毒。缓冲区溢出一个常见的后果是:黑客利用函数调用过程中程序的返回地址,将存放这块地址的指针精准指向计算机中存放攻击代码的位置,造成程序异常中止。为了防止发生严重的后果,计算机会采用栈随机化,利用金丝雀值检查破坏栈,限制代码可执行区域等方法来尽量避免被攻击。虽然,现代计算机已经可以“智能”查错了,但是我们还是要养成良好的编程习惯,尽量避免写出有漏洞的代码,以节省宝贵的时间!

  • 所有alpha都想标记我_react native 游戏

    所有alpha都想标记我_react native 游戏从0.3.0到0.4.0,reactos花了十年。下一个版本据说就是beta了,这标志着reactos从此不再是个玩具了。无论如何,reactos终于变得可赏可玩了,react提供live和boot

  • 将 Linux 移植到 M1 Mac 真的太难了!「建议收藏」

    将 Linux 移植到 M1 Mac 真的太难了!「建议收藏」【CSDN编者按】自去年苹果自研M1芯片发布之后,激发了无数用户的体验热情,与此同时,也吸引大批开发者在M1上开启探索模式。其中,国外一位资深操作系统移植专家HectorMa…

  • 罗技k580键盘蓝牙连接不上_ipad同时连接蓝牙键盘和耳机

    罗技k580键盘蓝牙连接不上_ipad同时连接蓝牙键盘和耳机K580蓝牙连接,失败解决。

  • python图像识别与提取_图像分类python

    python图像识别与提取_图像分类python前面一篇文章介绍了图像增强知识,从而改善图像质量,增强图像识别效果,核心内容分为直方图均衡化、局部直方图均衡化和自动色彩均衡三部分。这篇文章将详细讲解图像分类知识,包括常见的图像分类算法,并介绍Python环境下的贝叶斯图像分类算法、基于KNN算法的图像分类和基于神经网络算法的图像分类等案例。万字长文整理,希望对您有所帮助。同时,该部分知识均为作者查阅资料撰写总结,并且开设成了收费专栏,为小宝赚点奶粉钱,感谢您的抬爱。当然如果您是在读学生或经济拮据,可以私聊我给你每篇文章开白名单,或者转发原文给你,更希望

    2022年10月14日
  • 查看服务器并发连接数_查看数据库当前连接数

    查看服务器并发连接数_查看数据库当前连接数查看Sql Server当前的并发连接数

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号