iBATIS与Spring整合

接着iBATIS的入门实例来说,ibatis与Spring的整合其实跟hibernate与Spring的整合大同小异,都是一个模子的,回想hibernate整合spring:主要是在spring中配置,需要数据源,需要hibernate配置文件,需要sessionFactory。iBATIS走的路线完全一致,直接看配置就明白

先是src/sqlmap.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
    "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
  <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32"
    maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />

	<sqlMap resource="com/javacrazyer/myweb/ibatis/SysDeliverCompany.xml" />
	<sqlMap resource="com/javacrazyer/myweb/ibatis/SysArea.xml" />
	<sqlMap resource="com/javacrazyer/myweb/ibatis/SmsActivityExpense.xml" />
	<sqlMap resource="com/javacrazyer/myweb/ibatis/deptMgr.xml" />
	<sqlMap resource="com/javacrazyer/myweb/ibatis/userMgr.xml" />
	<sqlMap resource="com/javacrazyer/myweb/ibatis/SysCost.xml" />
	<sqlMap resource="com/javacrazyer/myweb/ibatis/orders.xml" />
	<sqlMap resource="com/javacrazyer/myweb/ibatis/privilege.xml" />
	<sqlMap resource="com/javacrazyer/myweb/ibatis/app.xml" />
  
</sqlMapConfig>

 

 很显然这个配置里没有配数据源,配置到spring即可

 applictionContext.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:p="http://www.springframework.org/schema/p"
	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-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
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

     <!--数据源,用的是spring容器自带的数据源,-->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
		</property>
		<property name="url">
			<value>jdbc:sqlserver://localhost:1433;databaseName=dbName
			</value>
		</property>
		<property name="username">
			<value>sa</value>
		</property>
		<property name="password">
			<value>123</value>
		</property>
		
		<property name="connectionProperties">
			<props>
				<!--  
				<prop key="useUnicode">true</prop>
				<prop key="characterEncoding">utf-8</prop>
				连接池中连接用完时,新的请求等待时间,毫秒,-1表示无限等待
				<prop key="maxWait">1000</prop>
				-->
				<!-- 是否清理空闲连接 -->
				<prop key="removeAbandoned">true</prop>
				<!-- 活动连接的最大空闲时间,秒 -->
				<prop key="removeAbandonedTimeout">60</prop>
				<!-- 初始连接数量 -->
				<prop key="initialSize">3</prop>
				<!-- 连接池支持的最大连接数,0表示无限制 -->
				<prop key="maxActive">50</prop>
				<!-- 连接池中最多可空闲maxIdle个连接,0表示无限制   -->
				<prop key="maxIdle">10</prop>
				<!-- 连接池中最小空闲maxIdle个连接   -->
				<prop key="minIdle">2</prop>
			</props>
		</property>
	</bean>
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="classpath:sqlmap.xml" />
		<!-- 从IBATIS基本示例可以看到,sqlmap.xml中是应该配置dataSource的,不过没配置也没关系,可以在spring中继续配 -->
		<property name="dataSource" ref="dataSource" />
		<!--
			<property name="useTransactionAwareDataSource"
			value="true"></property>
		-->
	</bean>
	<!--根据sqlMapClien创建一个SqlMapClient模版类-->
	<!-- 那么这个sqlMapClientTemplate就相当于sessionFactory获取的session -->
	<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
		<property name="sqlMapClient">
			<ref bean="sqlMapClient" />
		</property>
	</bean>
	
</beans>

 然后在需要的类写上个private SqlMapClientTemplate  sqlMapClientTemplate 当做字段并提供SETTER方法,之后在applicationContext.xml中注入sqlMapClientTemplate即可直接使用了,具体的方法与入门实例sqlMapper的方

法一样

 

你可能感兴趣的:(spring,AOP,bean,xml,ibatis)