版权声明:本文为博主原创文章,转载请注明出处!有时候也不是原创,手快就选了(我的文章随意转载复制,不在乎的哈!) https://blog.csdn.net/qq_31384551/article/details/81585611
beetlSQL是一个非常不错的dao数据库工具,官网地址
其实很多操作都非常方便,但是呢文档真的没有讲清楚很多关键性使用方法,我相信beetl以及尽力了,但还有很多不足,比如缓存的使用。
官网文档是这样说的:
我反正是看了又看,单独使用beetlSQL的话这个文档还是没毛病的,但是我要在spring中使用啊!,找遍文档都没有说明关于spring中beetlSQL缓存如何使用!
最后还是在上面图片中红线那句话提醒了我:同DebugInterceptor构造方式一样, SimpleCacheInterceptor能缓存指定的sql查询结果
【同DebugInterceptor构造方式一样】,这句话,我又结合beetlSQL整合spring的配置文件,beetlSQL整合spring配置文件如下:(为了方便标注,我直接截图的,要代码去官网看24.1. Spring集成和Demo)
就是标红线这段配置,我就类似的配置了一个SimpleCacheInterceptor
代码如下:
<property name="interceptors">
<list>
<bean class="org.beetl.sql.ext.DebugInterceptor"></bean>
<!--BeetlSQL自带缓存管理器,只需要设置sql.md的文件名(也叫命名空间)即可-->
<bean class="org.beetl.sql.ext.SimpleCacheInterceptor">
<constructor-arg>
<list>
<value>imageCheck</value><!--对于文件imageCheck.md-->
<value>source</value>
<value>sourceHelp</value>
<value>notice</value>
<value>module</value>
<value>hotSearch</value>
</list>
</constructor-arg>
</bean>
</list>
</property>
list部分就是要缓存的命名空间,也就是sql.md文件名称,只要配置了这个,那么在哪个sql.md 文件下的所有查询都会被缓存起来
最后再贴一张缓存成功的图:
查询获得结果时间为0ms,缓存配置成功。费了好大的劲
贴出beetlSQL.xml完整配置(整合spring)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 加载属性配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:dbconfig.properties" />
</bean>
<!--hikari数据源-->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.mysql.driver}" />
<property name="jdbcUrl" value="${jdbc.mysql.url}" />
<property name="username" value="${jdbc.mysql.username}" />
<property name="password" value="${jdbc.mysql.password}" />
<!-- 连接只读数据库时配置为true, 保证安全 -->
<property name="readOnly" value="${hikari.readOnly}" />
<!-- 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 -->
<property name="connectionTimeout" value="${hikari.connectionTimeout}" />
<!-- 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->
<property name="idleTimeout" value="${hikari.idleTimeout}" />
<!-- 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL
wait_timeout参数(show variables like '%timeout%';) -->
<property name="maxLifetime" value="${hikari.maxLifetime}" />
<!--配置检查验证连接池有效性多久检查一次-->
<property name="validationTimeout" value="${hikari.validationTimeout}" />
<property name="loginTimeout" value="${hikari.loginTimeout}" />
<!-- 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->
<property name="maximumPoolSize" value="${hikari.maximumPoolSize}" />
<property name="minimumIdle" value="${hikari.minimumIdle}" />
<property name="dataSourceProperties">
<props>
<prop key="cachePrepStmts">true</prop>
<prop key="prepStmtCacheSize">250</prop>
<prop key="prepStmtCacheSqlLimit">2048</prop>
<prop key="useServerPrepStmts">true</prop>
</props>
</property>
</bean>
<!--beetl Sql 配置 集成提供了Mapper类的自动注入以及SQLManager的自动注入,以及与spring事务集成-->
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean name="beetlSqlScannerConfigurer" class="org.beetl.sql.ext.spring4.BeetlSqlScannerConfigurer">
<!-- 哪些类可以自动注入 -->
<property name="basePackage" value="com.share"/>
<!-- 通过类后缀 来自动注入Dao -->
<property name="daoSuffix" value="Dao"/>
<property name="sqlManagerFactoryBeanName" value="sqlManagerFactoryBean"/>
</bean>
<bean id="sqlManagerFactoryBean" class="org.beetl.sql.ext.spring4.SqlManagerFactoryBean">
<property name="cs" >
<bean class="org.beetl.sql.ext.spring4.BeetlSqlDataSource">
<property name="masterSource" ref="dataSource"></property>
</bean>
</property>
<property name="dbStyle">
<bean class="org.beetl.sql.core.db.H2Style"/>
</property>
<property name="sqlLoader">
<bean class="org.beetl.sql.core.ClasspathLoader">
<property name="sqlRoot" value="/beetlSql"></property>
</bean>
</property>
<property name="nc">
<bean class="org.beetl.sql.core.UnderlinedNameConversion"/>
</property>
<property name="interceptors">
<list>
<bean class="org.beetl.sql.ext.DebugInterceptor"></bean>
<!--BeetlSQL自带缓存管理器,只需要设置sql.md的文件名(也叫命名空间)即可,官网文档没说,成功使用的过程简直呕心沥血啊!by 大BUG-->
<bean class="org.beetl.sql.ext.SimpleCacheInterceptor">
<constructor-arg>
<list>
<value>imageCheck</value>
<value>source</value>
<value>sourceHelp</value>
<value>notice</value>
<value>module</value>
<value>hotSearch</value>
</list>
</constructor-arg>
</bean>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务控制的注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
欢迎交流!