欢迎投稿

今日深度:

Mybatis-SQLite整合,

Mybatis-SQLite整合,


1.项目层级关系


2.Mybatis.xml文件

<?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:p="http://www.springframework.org/schema/p" 
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	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.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/cache 
http://www.springframework.org/schema/cache/spring-cache.xsd">
	
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<!-- 配置数据源1 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="dbType" value="${dbType}"></property>
		<property name="url" value="${sqlServer.url}" />
		<property name="driverClassName" value="${sqlServer.driverClassName}"></property>
		<property name="username" value="${sqlServer.username}" />
		<property name="password" value="${sqlServer.password}" />
		<property name="initialSize" value="1" />
		<property name="maxActive" value="20" />
		<property name="minIdle" value="10" />
		<property name="maxWait" value="60000" />
		<!--<property name="poolPreparedStatements" value="false"></property>
		<property name="maxOpenPreparedStatements" value="20"></property> -->
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="25200000" />
		<property name="removeAbandoned" value="true" />
		<property name="removeAbandonedTimeout" value="1800" />
		<property name="logAbandoned" value="true" />
		<!-- 配置监控统计拦截的filters,配置log4j可以打印出Parameters -->
        <property name="filters" value="log4j" />
	 </bean>
    
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />  
		<property name="mapperLocations" value="classpath:com/hytera/eems/mapping/*/*.xml"/>
	</bean>
	<!-- 配置扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描dao这个包以及它的子包下的所有映射接口类 -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
		<property name="basePackage" value="com.hytera.eems.dao.mybatis" />
	</bean>
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 注解方式配置事物 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

	<!-- 拦截器方式配置事物 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="transactionPointcut" expression="execution(* com.hytera.eems.service..*(..))" />
		<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
	</aop:config>
	
	<!-- 配置druid监控spring jdbc -->
	<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>
	<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
		<property name="patterns">
			<list>
				<value>com.hytera.eems.service.*</value>
			</list>
		</property>
	</bean>
	<aop:config>
		<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
	</aop:config>
	
    
	<!-- =====================================配置多数据源================================ -->
	
	
	<!-- =====================================结束================================ -->
	
	<!-- 手动加入配置 -->
	<bean id="UserDao" class="com.hytera.eems.dao.mybatis.UserDao">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
	<bean id="NetElementDao" class="com.hytera.eems.dao.mybatis.NetElementDao">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
	
	
</beans>

3.jdbc.properties

dbType=sqlite
validationQuery=SELECT 'x'

sqlServer.driverClassName=org.sqlite.JDBC
sqlServer.url=jdbc:sqlite::resource:data/sql.db
sqlServer.username=
sqlServer.password=
4.SQLite需要的驱动jar

sqlite-jdbc-3.7.2.jar

5.注意事项

在开发时修改数据在Tomcat下,原数据文件在src下。使用eclipse开发时经常会重新启动,原因分析是修改的文件位置为webRoot下的位置。

db.sql文件一共有三分

1.src下:原数据文件

2.web-inf,classes下:eclipse运行时修改的文件,会造成tomcat重启

3.tomcat下数据发布路径

www.htsjk.Com true http://www.htsjk.com/SQLite/34079.html NewsArticle Mybatis-SQLite整合, 1.项目层级关系 2.Mybatis.xml文件 ?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.springf...
相关文章
    暂无相关文章
评论暂时关闭