弄了好長時間了,上網找各種資料,文檔,最後發現Spring 3.0 reference裏頭寫到
Note
Spring supports iBATIS 2.x. The iBATIS 1.x support classes are no longer provided.
Spring 支持2.x 沒有說支不支持3.X 那應該也就是不支持了 而且iBatis3還沒有發佈正式版本,還有就是iBatis發生了一些明顯變化,jar包也縮減整合到一個jar包裏面了,所以Spring 應該是暫時不支持iBatis3了。
經過幾天的學習和查找,自己弄出來了一套,當然,俺不是啥高手,只是喜歡研究,研究的不對希望諒解,以下僅供參考:
以我的Flex工程爲例
web.xml配置Spring監聽 和2.x相同
ps:不知啥原因,Eclipse使用User library不管用,雖然導進來了,可是啓動的時候提示ClassNotFoundException,最開始還納悶,後來直接複製到lib文件夾下了。
<!-- Spring param Config --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> com/citipf/liyunpeng/applicationContext*.xml </param-value> </context-param> <!-- Spring Listener Config --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
applicationContext.xml 爲了省事,這裏就都寫到一個配置文件裏
.properties 讀取配置文件
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/com/citipf/liyunpeng/mainConfig.properties</value> </list> </property> </bean>
C3P0連接池配置
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name="driverClass"><value>${jdbc.driverClass}</value></property> <property name="jdbcUrl"><value>${jdbc.url}</value></property> <property name="user"><value>${jdbc.user}</value></property> <property name="password"><value>${jdbc.password}</value></property> <property name="initialPoolSize"><value>${jdbc.initialPoolSize}</value></property> <property name="minPoolSize"><value>${jdbc.minPoolSize}</value></property> <property name="maxPoolSize"><value>${jdbc.maxPoolSize}</value></property> </bean>
正常數據源配置
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName"> <value>${jdbc.driverClass}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.user}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> -->
iBatis 配置 (Java文件在下面)
<bean id="sqlMapClient" class="com.citipf.liyunpeng.dao.iBatis.SqlSessionFactoryBean"> <property name="configLocation" value="/com/citipf/liyunpeng/ibatisConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean>
Dao 以及 Service
<bean id="stockDaoImpl" class="com.citipf.liyunpeng.dao.iBatis.StockDaoImpl"> <property name="sqlSessionFactory" ref="sqlMapClient" /> </bean> <!-- Service --> <bean id="citiService" class="com.citipf.liyunpeng.CitiPFService"> <property name="stockDao" ref="stockDaoImpl" /> </bean>
-----------------------Java代碼---------------------------------
由於Spring3.0並不支持iBatis 3所以,使用iBatis2的
org.springframework.orm.ibatis.SqlMapClientFactoryBean配置方式Spring會去找iBatis2中支持的類,則會出現ClassNotFoundException(明確查過7z l *.jar >iBatisJarFiles.txt,iBatis3確實沒有相應的類)
所以需要自己來重寫SqlSessionFactory和DaoSupport,而不是使用
org.springframework.orm.ibatis.SqlMapClientFactoryBean
org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
SqlSessionFactory.java 在網上找的相關代碼然後自己修改的
package com.citipf.liyunpeng.dao.iBatis; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.HashMap; import java.util.Map; import javax.sql.DataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.ibatis.builder.xml.XMLMapperBuilder; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.parsing.XNode; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.managed.ManagedTransactionFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; import org.springframework.util.Assert; public class SqlSessionFactoryBean implements FactoryBean<Object>,InitializingBean{ Log logger = LogFactory.getLog(SqlSessionFactoryBean.class); private Resource configLocation; private Resource[] mapperLocations; private DataSource dataSource; private boolean useTransactionAwareDataSource = true; SqlSessionFactory sqlSessionFactory; public void afterPropertiesSet() throws Exception { Assert.notNull(configLocation,"configLocation must be not null"); sqlSessionFactory = createSqlSessionFactory(); } private SqlSessionFactory createSqlSessionFactory() throws IOException { Reader reader = new InputStreamReader(getConfigLocation().getInputStream()); try { SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); Configuration conf = sqlSessionFactory.getConfiguration(); if(dataSource != null) { DataSource dataSourceToUse = this.dataSource; if (this.useTransactionAwareDataSource && !(this.dataSource instanceof TransactionAwareDataSourceProxy)) { dataSourceToUse = new TransactionAwareDataSourceProxy(this.dataSource); } conf.setEnvironment(new Environment("development",new ManagedTransactionFactory(),dataSourceToUse)); sqlSessionFactory = new SqlSessionFactoryBuilder().build(conf); } if(mapperLocations != null) { Map<String, XNode> sqlFragments = new HashMap<String, XNode>(); for(Resource r : mapperLocations) { logger.info("Loading iBatis3 mapper xml from file["+r.getFile().getAbsolutePath()+"]"); Reader mapperReader = new InputStreamReader(r.getInputStream()); try { XMLMapperBuilder mapperBuilder = new XMLMapperBuilder(mapperReader,conf,r.getFile().getAbsolutePath(),sqlFragments); mapperBuilder.parse(); }finally { mapperReader.close(); } } } return sqlSessionFactory; }finally { reader.close(); } } public Object getObject() throws Exception { return sqlSessionFactory; } public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public Class<? extends Object> getObjectType() { return SqlSessionFactory.class; } public boolean isSingleton() { return true; } public Resource getConfigLocation() { return configLocation; } public void setConfigLocation(Resource configurationFile) { this.configLocation = configurationFile; } public void setMapperLocations(Resource[] mapperLocations) { this.mapperLocations = mapperLocations; } }
IBatisDaoSupport.java
package com.citipf.liyunpeng.dao.iBatis; import java.sql.SQLException; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.util.Assert; import org.springframework.dao.support.DaoSupport; //import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; public abstract class IBatisDaoSupport extends DaoSupport { protected final Log log = LogFactory.getLog(getClass()); private SqlSessionFactory sqlSessionFactory; private SqlSessionTemplate sqlSessionTemplate; @Override protected void checkDaoConfig() throws IllegalArgumentException { Assert.notNull("sqlSessionFactory must be not null"); } public SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; this.sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory); } public SqlSessionTemplate getSqlSessionTemplate() { return sqlSessionTemplate; } public static class SqlSessionTemplate { SqlSessionFactory sqlSessionFactory; public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } public Object execute(SqlSessionCallback action) { SqlSession session = null; try { session = sqlSessionFactory.openSession(); Object result = action.doInSession(session); return result; }finally { if(session != null) { try { session.getConnection().close(); session.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public Object selectOne(final String statement,final Object parameter) { return execute(new SqlSessionCallback() { public Object doInSession(SqlSession session) { return session.selectOne(statement, parameter); } }); } @SuppressWarnings("unchecked") public List selectList(final String statement,final Object parameter,final int offset,final int limit) { return (List)execute(new SqlSessionCallback() { public Object doInSession(SqlSession session) { return session.selectList(statement, parameter, new RowBounds(offset,limit)); } }); } public int delete(final String statement,final Object parameter) { return (Integer)execute(new SqlSessionCallback() { public Object doInSession(SqlSession session) { return session.delete(statement, parameter); } }); } public int update(final String statement,final Object parameter) { return (Integer)execute(new SqlSessionCallback() { public Object doInSession(SqlSession session) { return session.update(statement, parameter); } }); } public int insert(final String statement,final Object parameter) { return (Integer)execute(new SqlSessionCallback() { public Object doInSession(SqlSession session) { return session.insert(statement, parameter); } }); } } public static interface SqlSessionCallback { public Object doInSession(SqlSession session); } }
接下來Dao實現IBatisDaoSupport.java就可以了
僅供參考,個人研究的並不太透徹,如果有錯誤的地方希望高人指點,不過那些找不到的類確確實實是在iBatis2中的,iBatis3確實已經不要了~~,呵呵!!