p6使用教程_pwdump7使用

p6使用教程_pwdump7使用hibernate打印sql

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

Jetbrains全系列IDE稳定放心使用

     在之前的hibernate的总结中,遇到一个小问题,那就是打印sql语句的问题.在上个hibernate项目的基础上(spring+hibernate),继续p6spy的学习(p6spy相关文件下载)

hibernate控制sql语句的参数配置:

				<!--开发调试使用 -->
				<!--控制台打印sql语句 -->
				<prop key="hibernate.show_sql">false</prop>
				<!--格式化语句 -->
				<prop key="hibernate.format_sql">false</prop>
				<!--如果开启, Hibernate将在SQL中生成有助于调试的注释信息, 默认值为false -->
				<prop key="hibernate.use_sql_comments">false</prop>

      但是这样有个问题,那就是hibernate中打印的参数都是一些?,实际上hibernate打印都是一些预编译的sql,无法打印真正的sql.回顾一些jdbc(java使用连接数据的api,
Java DataBase Connectivity)直连数据库

		Connection conn = null;
		try {
			// 第一步,注册驱动程序 以mysql驱动为例
			Class.forName("com.mysql.jdbc.Driver");
			// 第二步,获取一个数据库的连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123");
			conn.setAutoCommit(false);
			
			
			// 第三步,创建一个会话
			//Statement stmt = conn.createStatement();	
			String sql = "select * from school s where s.id=?";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setInt(1, 1);
			
			
			// 第四步,执行SQL语句,增加,删除,修改记录 或者查询记录
			// stmt.executeUpdate();
			ResultSet rs = stmt.executeQuery();
			ResultSetMetaData m = rs.getMetaData();

			// 第五步,对查询的结果进行处理
			int columns = m.getColumnCount();
			// 显示列,表格的表头
			for (int i = 1; i <= columns; i++) {
				System.out.print(m.getColumnName(i));
				System.out.print("\t\t");
			}
			System.out.println();
			// 显示表格内容
			while (rs.next()) {
				for (int i = 1; i <= columns; i++) {
					System.out.print(rs.getString(i));
					System.out.print("\t\t");
				}
				System.out.println();
			}
			// 第六步,关闭连接
			conn.commit();
			rs.close();
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("进行回滚操作");
			conn.rollback();
		} finally {
			conn.close();
		}
	

       而我们通常配置的数据库驱动实现了与数据库的交互的前提,数据库连接池如druid,dbcp,c3p0,jboss datasource,bonecp,proxool等则管理通数据具体操作的连接connection,

通过一个个connection完成对具体数据的操作.

      从上例中可知,使用connection进行操作时摒弃了statement,而使用PreparedStatement,statement采用硬编码,每次执行sql都会进行编译,效率低,同时还能带来sql注入等不安全因素,而PreparedStatement相当于动态sql,对于相同的sql只会编译一次,参数通过注入的方式,有效阻止的sql注入,因此,项目中大多时候选择的是PreparedStatement,但对于那些一次性操作的,比如批量插入,删除等,可以直接使用statement.

hibernate默认使用的PreparedStatement,其取格式的sql是预编译的sql(参数没有被完全注入进来),从而会有一系列的?,要显示思路一种就是改变取sql的时机,等参数全部注入完成再取(p6spy),一种就是利用log,将传入的参数打印出来(这样sql,与参数分离),显然后一种没有前一种方便(hibernate打印sql)

p6spy使用com.p6spy.engine.spy.P6SpyDriver作为数据库驱动,P6SpyDriver是对原生原生驱动(此处以com.mysql.jdbc.Driver为例)的封装,其对数据的操作还是调用原生驱动的方法,只是多了sql监控的功能.

paspy基本使用.

配置驱动

		<!--设置数据库连接 -->
		<property name="driverClass" value="com.p6spy.engine.spy.P6SpyDriver" />

数据库连接(多了一个p6spy):

db.jdbcUrl  = jdbc:p6spy:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true

spy.propertie

#配置输出方式
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
#appender=com.p6spy.engine.spy.appender.FileLogger
appender=com.p6spy.engine.spy.appender.StdoutLogger

#配置输出内容格式(此处自定义)
#默认 com.p6spy.engine.spy.appender.SingleLineFormat
logMessageFormat=spring.redis.test.util.MySingleLineFormat

#输出内容选择
#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,batch,debug,commit,rollback,result,resultset

此处为了测试方便,使用控制台打印,自定义输出格式,输出内容选择statement

自定义输出内容格式:

package spring.redis.test.util;

import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;

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

public class MySingleLineFormat extends SingleLineFormat {
	private final static BasicFormatterImpl sqlformat = new BasicFormatterImpl();
	  /**
	   * Formats a log message for the logging module
	   *
	   * @param connectionId the id of the connection
	   * @param now          the current ime expressing in milliseconds
	   * @param elapsed      the time in milliseconds that the operation took to complete
	   * @param category     the category of the operation
	   * @param prepared     the SQL statement with all bind variables replaced with actual values
	   * @param sql          the sql statement executed
	   * @return the formatted log message
	   */
	  @Override
	  public String formatMessage(final int connectionId, final String now, final long elapsed, final String category, final String prepared, final String sql) {
	   return "#token time:" + elapsed + "ms | category:" + category + " | connectionId: " + connectionId + "\n" + "预编译语句"+sqlformat.format(prepared) + "\nread sql" + sqlformat.format(sql)
	  }
}

这是使用了hibernate格式化类,BasicFormatterImpl,以上展示的参数都是可以展示,测试打印

		//执行打印sql
		hibernateTemplate.get(School.class, 1);
/*		#token time:3ms | category:statement | connectionId: 2
		预编译语句
		    select
		        school0_.id as id1_1_0_,
		        school0_.create_time as create_t2_1_0_,
		        school0_.des as des3_1_0_,
		        school0_.name as name4_1_0_,
		        school0_.update_time as update_t5_1_0_ 
		    from
		        school school0_ 
		    where
		        school0_.id=?
		read sql
		    select
		        school0_.id as id1_1_0_,
		        school0_.create_time as create_t2_1_0_,
		        school0_.des as des3_1_0_,
		        school0_.name as name4_1_0_,
		        school0_.update_time as update_t5_1_0_ 
		    from
		        school school0_ 
		    where
		        school0_.id=1*/
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185430.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Linux系统RWX权限规则[通俗易懂]

    Linux系统RWX权限规则[通俗易懂]话不多说,先了解一下文件所对应的书写字段:其中:-rw-r–r–1rootroot0Nov3014:46a.txt1、新增一个文件test.txt,并该文件对任何人都没有任何权限:root@lhb:~#chmodu=,g=,o=test.txtroot@lhb:~#ls-ltest.txt———-1rootro

    2022年5月10日
    41
  • Security身份认证之UserDetailsService[通俗易懂]

    Security身份认证之UserDetailsService[通俗易懂]之前我们采用了配置文件的方式从数据库中读取用户进行登录。虽然该方式的灵活性相较于静态账号密码的方式灵活了许多,但是将数据库的结构暴露在明显的位置上,绝对不是一个明智的做法。本文通过Java代码实现UserDetailsService接口来实现身份认证。1.1UserDetailsService在身份认证中的作用SpringSecurity中进行身份验证的是AuthenticationMan…

    2022年4月19日
    375
  • MFC + CxImage 实现自绘半透明按钮

    MFC + CxImage 实现自绘半透明按钮

    2021年8月27日
    59
  • 类模板友元函数的声明(友元函数可以在类内定义吗)

    2009-08-09摘自《SunStudio12:C++用户指南》第6.7.3节 模板在使用前必须先声明。一个友元声明构成了模板的使用,而非模板的声明。(Afrienddeclarationconstitutesauseofthetemplate,notadeclarationofthetemplate.)所以实际的模板声明必须在友元声明之前

    2022年4月10日
    42
  • EmguCV 常用函数功能说明「建议收藏」

    AbsDiff,计算两个数组之间的绝对差。dst(I)c=abs(src1(I)c-src2(I)c)。所有数组必须具有相同的数据类型和相同的大小(或ROI大小)。累加,将整个图像或其所选区域添加到累加器和。累积产品,将2张图像或其选定区域的产品添加到累加器中。AccumulateSquare,将输入src或其选定的区域,增加到功率2,添加到累加器sqsum。累积权重,计算输

    2022年4月8日
    46
  • 锐捷交换机(S2924/2928G)光口改电口,或电口改光口的准确方法「建议收藏」

    锐捷交换机(S2924/2928G)光口改电口,或电口改光口的准确方法「建议收藏」软件需求:telnet。依次输入Ruijie&gt;输入enable//若未设置enable密码,需要在web中先设置Ruijie#输入configRuijie(config)#输入intg0/24//其中g代表千兆口,0/24代表24号端口Ruijie(config-if-GigabitEthernet0/24)#输入medium-typ…

    2022年10月9日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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