Spring 3.0 整合 iBatis 3 Beta10 配置


  弄了好长时间了,上网找各种资料,文档,最后发现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文件夹下了。 
  applicationContext.xml 为了省事,这里就都写到一个配置文件里 
  .properties 读取配置文件 C3P0连接池配置  ${jdbc.driverClass} ${jdbc.url} ${jdbc.user} ${jdbc.password} ${jdbc.initialPoolSize} ${jdbc.minPoolSize} ${jdbc.maxPoolSize}  
  正常数据源配置 iBatis 配置  (Java文件在下面) Dao 以及 Service -----------------------Java代码--------------------------------- 
  由于Spring3.0并不支持iBatis 3所以,使用iBatis2的 
  org.springframework.orm.ibatis.SqlMapClientFactory Bean配置方式Spring会去找iBatis2中支持的类,则会出现ClassNotFoundException(明确查过7z l *.jar >iBatisJarFiles.txt,iBatis3确实没有相应的类) 
  所以需要自己来重写SqlSessionFactory和DaoSupport,而不是使用 
  org.springframework.orm.ibatis.SqlMapClientFactory Bean 
  org.springframework.orm.ibatis.support.SqlMapClien tDaoSupport 
  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.ManagedTrans actionFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean ; import org.springframework.core.io.Resource; import org.springframework.jdbc.datasource.TransactionAwa reDataSourceProxy; import org.springframework.util.Assert; public class SqlSessionFactoryBean implements FactoryBean,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().getInputStre am()); 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 sqlFragments = new HashMap(); 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().get AbsolutePath(),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 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.SqlMapClien tDaoSupport; 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确实已经不要了~~,呵呵!! 

猜你喜欢

转载自nmv884rp.iteye.com/blog/1572646