iBatis2学习笔记:与Spring2的整合
Spring2.5.5
iBatis 2.3
iBatis是一个ORMapping框架,Spring对其提供了强力的支持,这是整合Spring的基础。
整合的原因:
Spring框架已经深入人心,在J2EE开发中普遍应用。
iBatis虽然简化了数据操作,但是没有对JDBC的SQL异常做转换处理,开发者必须自己处理这些SQL异常,很麻烦。而Spring框架(SqlMap模板)对这些异常做了统一的转换和处理,不再强制开发人员捕获和处理SQL异常。
Spring对事务的处理很灵活方便,解决了iBatis难以解决的问题。
整合方法:
Spring应用的主配置文件ApplicationContext.xml:
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
< beans default-autowire ="byName"
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee ="http://www.springframework.org/schema/jee"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]
[url]http://www.springframework.org/schema/jee[/url] [url]http://www.springframework.org/schema/jee/spring-jee-2.0.xsd[/url]
[url]http://www.springframework.org/schema/aop[/url] [url]http://www.springframework.org/schema/aop/spring-aop-2.0.xsd[/url]
[url]http://www.springframework.org/schema/tx[/url] [url]http://www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]" >
<!-- 指定Spring配置中用到的属性文件-->
< bean id ="propertyConfig"
class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name ="locations" >
< list >
< value >classpath:jdbc.properties </ value >
</ list >
</ property >
</ bean >
<!-- 配置系统的数据源 -->
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" >
< property name ="driverClassName" >
< value >${jdbc.driver} </ value >
</ property >
< property name ="url" >
< value >${jdbc.url} </ value >
</ property >
< property name ="username" >
< value >${jdbc.username} </ value >
</ property >
< property name ="password" >
< value >${jdbc.password} </ value >
</ property >
</ bean >
<!-- 事物配置 -->
< aop:config >
< aop:pointcut id ="serviceOperation"
expression ="execution(* lavasoft.*.service.*(..))" />
< aop:advisor pointcut-ref ="serviceOperation"
advice-ref ="txAdvice" />
</ aop:config >
< bean id ="transactionManager"
class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name ="dataSource" ref ="dataSource" />
</ bean >
<!-- 配置事务通知 -->
< tx:advice id ="txAdvice" transaction-manager ="transactionManager" >
< tx:attributes >
< tx:method name ="create*" rollback-for ="Exception" />
< tx:method name ="delete*" rollback-for ="Exception" />
< tx:method name ="save*" rollback-for ="Exception" />
< tx:method name ="insert*" rollback-for ="Exception" />
< tx:method name ="update*" rollback-for ="Exception" />
< tx:method name ="del*" rollback-for ="Exception" />
< tx:method name ="vote*" rollback-for ="Exception" />
< tx:method name ="*" read-only ="true" rollback-for ="Exception" />
</ tx:attributes >
</ tx:advice >
<!-- 配置iBatis的sqlMapClient -->
< bean id ="sqlMapClient"
class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="configLocation" >
< value > SqlMapConfig.xml </ value >
</ property >
</ bean >
<!-- 根据sqlMapClien获取一个SqlMapClient模版 -->
< bean id ="sqlMapClientTemplate" class ="org.springframework.orm.ibatis.SqlMapClientTemplate" >
< property name ="sqlMapClient" ref ="sqlMapClient" />
</ bean >
< import resource ="ApplicationContext_test.xml" />
</ beans >
< beans default-autowire ="byName"
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee ="http://www.springframework.org/schema/jee"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]
[url]http://www.springframework.org/schema/jee[/url] [url]http://www.springframework.org/schema/jee/spring-jee-2.0.xsd[/url]
[url]http://www.springframework.org/schema/aop[/url] [url]http://www.springframework.org/schema/aop/spring-aop-2.0.xsd[/url]
[url]http://www.springframework.org/schema/tx[/url] [url]http://www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]" >
<!-- 指定Spring配置中用到的属性文件-->
< bean id ="propertyConfig"
class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name ="locations" >
< list >
< value >classpath:jdbc.properties </ value >
</ list >
</ property >
</ bean >
<!-- 配置系统的数据源 -->
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" >
< property name ="driverClassName" >
< value >${jdbc.driver} </ value >
</ property >
< property name ="url" >
< value >${jdbc.url} </ value >
</ property >
< property name ="username" >
< value >${jdbc.username} </ value >
</ property >
< property name ="password" >
< value >${jdbc.password} </ value >
</ property >
</ bean >
<!-- 事物配置 -->
< aop:config >
< aop:pointcut id ="serviceOperation"
expression ="execution(* lavasoft.*.service.*(..))" />
< aop:advisor pointcut-ref ="serviceOperation"
advice-ref ="txAdvice" />
</ aop:config >
< bean id ="transactionManager"
class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name ="dataSource" ref ="dataSource" />
</ bean >
<!-- 配置事务通知 -->
< tx:advice id ="txAdvice" transaction-manager ="transactionManager" >
< tx:attributes >
< tx:method name ="create*" rollback-for ="Exception" />
< tx:method name ="delete*" rollback-for ="Exception" />
< tx:method name ="save*" rollback-for ="Exception" />
< tx:method name ="insert*" rollback-for ="Exception" />
< tx:method name ="update*" rollback-for ="Exception" />
< tx:method name ="del*" rollback-for ="Exception" />
< tx:method name ="vote*" rollback-for ="Exception" />
< tx:method name ="*" read-only ="true" rollback-for ="Exception" />
</ tx:attributes >
</ tx:advice >
<!-- 配置iBatis的sqlMapClient -->
< bean id ="sqlMapClient"
class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="configLocation" >
< value > SqlMapConfig.xml </ value >
</ property >
</ bean >
<!-- 根据sqlMapClien获取一个SqlMapClient模版 -->
< bean id ="sqlMapClientTemplate" class ="org.springframework.orm.ibatis.SqlMapClientTemplate" >
< property name ="sqlMapClient" ref ="sqlMapClient" />
</ bean >
< import resource ="ApplicationContext_test.xml" />
</ beans >
引入的数据库链接配置jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/testdb
jdbc.username=root
jdbc.password=leizhimin
jdbc.url=jdbc:mysql://localhost/testdb
jdbc.username=root
jdbc.password=leizhimin
log4j.properties 的配置,这样可以在控制台打印出执行的SQL,千万注意,此文件必须放在src根目录下面,真TM扯淡,我放到一个res(资源文件夹)下就不行,暂时还不知道为什么。
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout
Spring应用模块的配置:
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
< beans default-autowire ="byName"
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]" >
<!-- DAO -->
< bean id ="accountDAO" class ="com.lavasoft.ibatissut.simple.domain.dao.AccountDAOImpl" />
< beans default-autowire ="byName"
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]" >
<!-- DAO -->
< bean id ="accountDAO" class ="com.lavasoft.ibatissut.simple.domain.dao.AccountDAOImpl" />
<!-- Service -->
</ beans >
</ beans >
iBatis的配置文件
<?
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 ="false"
enhancementEnabled ="false"
lazyLoadingEnabled ="true"
errorTracingEnabled ="true"
<!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 ="false"
enhancementEnabled ="false"
lazyLoadingEnabled ="true"
errorTracingEnabled ="true"
useStatementNamespaces
="true"
maxRequests ="32"
maxSessions ="10"
maxTransactions ="5"
/>
< sqlMap resource ="com/lavasoft/ibatissut/simple/domain/entity/Account.xml" />
</ sqlMapConfig >
maxRequests ="32"
maxSessions ="10"
maxTransactions ="5"
/>
< sqlMap resource ="com/lavasoft/ibatissut/simple/domain/entity/Account.xml" />
</ sqlMapConfig >
注意:如果你用的是Spring2.5以前的版本,那么你需要去掉:
maxRequests
="32"
maxSessions ="10"
maxTransactions ="5"
maxSessions ="10"
maxTransactions ="5"
,否则,会提示Spring配置错误,很扯吧!呵呵。
这样就配置完成了。