spring+jpa+HiKariCP+P6spy SSH HiKariCP P6spy

=============p6spy准备https://www.cnblogs.com/qgc88=====================

1.简单介绍p6spy,p6spy是一个开源项目,通常使用它来跟踪数据库操作,查看程序运行过程中执行的sql语句。
2.把p6spy-3.8.1 jar包都添加到lib目录,其它jar不需要(本人在下载:https://search.maven.org/remotecontent?filepath=p6spy/p6spy/3.8.1/p6spy-3.8.1.zip)
3.把spy.properties文件拷贝到资源文件复制到src目录下
4.修改spy.properties里面的属性
driverlist=com.mysql.jdbc.Driver
appender=com.p6spy.engine.spy.appender.StdoutLogger
5.修改驱动信息:
修改之前为:
#db.driverClass=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
修改之后为:
db.driverClass=com.p6spy.engine.spy.P6SpyDriver
db.url=jdbc:p6spy:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8


=============HikariCP连接池准备https://www.cnblogs.com/qgc88=====================

1.把HikariCP jar包都添加到lib目录

=============Spring关键配置部分代码=====================

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName" default-lazy-init="true">

 

<!--以下用于,HiKariCP连接池的数据源 -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariDataSource" >

<!--注:如果用p6spy工具类,则使用下面的代码https://www.cnblogs.com/qgc88 -->
<property name="driverClassName" value="com.p6spy.engine.spy.P6SpyDriver"/>
<property name="jdbcUrl" value="jdbc:p6spy:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"/>

<!--注:如果不用p6spy工具类,则使用下面的代码 https://www.cnblogs.com/qgc88-->

<!--https://www.cnblogs.com/qgc88

<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/wxdkf?useUnicode=true&amp;characterEncoding=UTF-8"/>

 -->
<property name="username" value="root"/>
<property name="password" value="000000"/>

<property name="poolName" value="springHikariCP" />
<!-- 连接只读数据库时配置为true, 保证安全 -->
<property name="readOnly" value="false" />
<!-- 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 -->
<property name="connectionTimeout" value="120000" />
<!-- 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->
<property name="idleTimeout" value="600000" />
<!-- 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) -->
<property name="maxLifetime" value="1800000" />
<!-- 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->
<property name="maximumPoolSize" value="15" />

<property name="autoCommit" value="true" />
<property name="connectionTestQuery" value="SELECT 1" />
</bean>
<!-- HikariCP configuration -->
<!--https://www.cnblogs.com/qgc88

注:如果不用p6spy工具类,则使用下面的代码

<bean id="dataSourceMySql" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">

-->

<!--注:如果用p6spy工具类,则使用下面的代码https://www.cnblogs.com/qgc88 -->

<bean id="dataSourceMySql" class="com.p6spy.engine.spy.P6DataSource">
<constructor-arg ref="hikariConfig" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="appUnit" />
<property name="dataSource" ref="dataSourceMySql" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
</bean>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSourceMySql"/>
</bean>

 

<tx:annotation-driven transaction-manager="jdbcTxManager"/>

<!--自动扫描,@Autowired注解标记对象的自动注入,此方式被注入的bean还是需要通过XML的方式注册的-->
<context:annotation-config />
<!-- 自动装配Bean -->
<context:component-scan base-package="com.test.qgc"/>

</beans>

提示:本人经过对比,用p6spy工具,感觉相对仅仅使用HiKariCP查询的效率会稍微慢些 ^ ^  https://www.cnblogs.com/qgc88

 

posted on 2019-01-10 17:57  qgc  阅读(889)  评论(0编辑  收藏  举报

导航