1、 Spring中整合Ibatis
a) 配置数据源:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url" value="jdbc:oracle:thin:@连接库IP:1521:服务名">
</property>
<property name="username" value="用户名"></property>
<property name="password" value="密码"></property>
</bean>
BasicDataSource提供了close()方法关闭数据源,所以必须设定destroy-method=”close”属性,以便Spring容器关闭时,数据源能够正常关闭
b) 加载SqlMap映射文件:
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:sqlmap-config.xml">
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>
c) 配置数据源事务管理器:
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
d) IoC注入:
<bean name="/testController.htm" class="myh.controller.Test">
<property name="dao" ref="testDao"></property>
<property name="methodNameResolver">
<ref bean="methodNameResolver"/>
</property>
</bean>
<bean id="testDao" class="myh.dao.TestDao">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
dao层需继承SqlMapClientDaoSupport,如下图所示
2、 Ibatis配置文件,注意以下两个文件的文件头的区别
a) sqlmap-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL MapConfig 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="false"
lazyLoadingEnabled="false" maxRequests="3000" maxSessions="3000"
maxTransactions="3000" useStatementNamespaces="true" />
<sqlMapresource="sqlmap-mapping-tree.xml" />
</sqlMapConfig>
b) sqlmap-mapping-tree.xml:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap>
<!-- 根据父节点ID查询是否存在子节点 -->
<select id="countNodeByPare" parameterClass="java.lang.String"
resultClass="java.lang.Integer">
select count(*)
fromT_SUPERVISE_REPORT_TREE s
wheres.C_PRE_CDE=#pareCde#
</select>
</sqlMap>