spring整合ibatis步骤及配置文件
2013-08-06 09:44
495 查看
与hibernate类似,Spring 提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。
spring与ibatis的整合,实际上是通过spring管理ibatis的事务和ibatis中的数据源。
配置步骤如下:
1.spring通过AOP来拦截方法的调用,在需要事务处理的方法上面添加声明式事务处理的能力
2.配置事务管理器,对事务进行管理
3.让spring来管理SqlMapClient对象:
具体配置文件如下。
SqlMapConfig.xml就可以简写为
只需将需要的映射配置文件引入即可,数据源配置放在spring中,ibatis中无需再次配置
spring与ibatis的整合,实际上是通过spring管理ibatis的事务和ibatis中的数据源。
配置步骤如下:
1.spring通过AOP来拦截方法的调用,在需要事务处理的方法上面添加声明式事务处理的能力
2.配置事务管理器,对事务进行管理
3.让spring来管理SqlMapClient对象:
具体配置文件如下。
<?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"> <context:annotation-config /> <context:component-scan base-package="com.ai.mdfcore.manager" /> <!-- 配置事务特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="find*" propagation="REQUIRED" read-only="true"/> <tx:method name="search*" propagation="REQUIRED" read-only="true"/> <tx:method name="query*" propagation="REQUIRED" read-only="true"/> <tx:method name="*" propagation="REQUIRED" read-only="false"/> </tx:attributes> </tx:advice> <!-- 配置哪些类的方法需要进行事务管理 --> <aop:config> <aop:pointcut id="interceptorPointCuts" expression="execution(* com.ai.mdfcore.manager.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" /> </aop:config> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" lazy-init="false"> <property name="driverClass" value="${driverClass}" /> <property name="jdbcUrl" value="${jdbcUrl}" /> <property name="user" value="${username}" /> <property name="password" value="${password}" /> <property name="minPoolSize" value="${minPoolSize}" /> <property name="maxPoolSize" value="${maxPoolSize}" /> <property name="maxIdleTime" value="120" /> <property name="acquireIncrement" value="2" /> <property name="maxStatements" value="0" /> <property name="initialPoolSize" value="2" /> </bean> <!-- 配置spring管理SqlMapClient对象 --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="classpath:orm/SqlMapConfig.xml" /> <!-- 引入SqlMapConfig.xml文件 --> <property name="dataSource" ref="dataSource" /> <!-- 引入dataSource --> </bean> </beans>
SqlMapConfig.xml就可以简写为
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <settings cacheModelsEnabled="true" enhancementEnabled="false" lazyLoadingEnabled="false" maxRequests="3000" maxSessions="3000" maxTransactions="3000" useStatementNamespaces="true" /> <sqlMap resource="orm/Favorites.xml" /> <sqlMap resource="orm/Downldetail.xml" /> <sqlMap resource="orm/Gradedetail.xml" /> <sqlMap resource="orm/MaterialStatistics.xml" /> <sqlMap resource="orm/Operationlog.xml" /> </sqlMapConfig>
只需将需要的映射配置文件引入即可,数据源配置放在spring中,ibatis中无需再次配置
相关文章推荐
- struts2 ibatis spring 框架整合配置文件的设置
- ibatis整合spring SQL文件配置
- spring整合hibernate步骤及配置文件
- SSM(spring-springmvc-mybatis)整合开发xml配置文件内容springmvc-config.xml,application.xml,web.xml
- spring-bean命名方式-以及实例化--配置文件整合
- mybatis整合spring配置文件
- spring整合hibernate配置文件
- ssh框架中spring整合hibernate的配置文件模板(带详细注释)
- CXF整合Spring配置文件
- 整合hibernate,JBPM.4.4的Spring配置文件
- JSF整合Spring时各文件的配置
- Spring、Spring MVC、MyBatis整合文件配置详解
- ibatis+springmvc的dao实现+配置文件
- ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址
- spring、mybatis整合核心配置文件applicationContext.xml
- Spring整合mybatis的配置文件
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- Spring、MyBatis的整合数据映射器类(UserMapper->iocContext.xml)配置文件详解
- SpringMVC+Shiro整合配置文件详解