spring整合sqlite

spring整合sqlite

1. 引入sqlite驱动

<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.21.0</version>
</dependency>

 

2. 配置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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 配置连接池 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="poolProperties">
<bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
<property name="driverClassName" value="org.sqlite.JDBC"/>
<property name="url" value="jdbc:sqlite:G:/sqlite/database/stock.db"/>
<property name="minIdle" value="10"/>
<property name="maxActive" value="100"/>
<!-- 数据库连接池配置 -->
<!-- 初始化连接数量 -->
<property name="initialSize" value="50"/>
<!-- 最大连接数量 -->
<!-- 最小空闲连接数量 -->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="600" />
<!-- 验证连接是否有效 -->
<property name="validationQuery" value="select 1" />
<!-- 验证失败时,是否将连接从池中丢弃 -->
<property name="testWhileIdle" value="true" />
<!-- 把空闲时间超过minEvictableIdleTimeMillis毫秒的连接断开,直到连接池中的连接数到minIdle为止(毫秒,30分钟) -->
<property name="timeBetweenEvictionRunsMillis" value="1200000" />
<!-- 连接池中连接可空闲的时间(毫秒,5分钟) -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
<property name="numTestsPerEvictionRun" value="5" />
</bean>
</property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 事务配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="create*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"
read-only="true" />

</tx:attributes>
</tx:advice>
</beans>

posted on 2019-01-14 00:23  祁连牧师  阅读(2477)  评论(0)    收藏  举报