ImageVerifierCode 换一换
格式:DOCX , 页数:24 ,大小:290.47KB ,
资源ID:11806106      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11806106.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory.docx

1、Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactorySpring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory一、摘要上两篇文章分别介绍了Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法 和 Spring3 整合Hibernate3.5 动态切换SessionFactory (切换数据库方言),这篇文章将介绍Spring整合Mybatis 如何完成SqlSessionFactory的动态切换的。并且会简单的介绍下MyBatis整合Spring中的官方的相关代码。

2、Spring整合MyBatis切换SqlSessionFactory有两种方法,第一、 继承SqlSessionDaoSupport,重写获取SqlSessionFactory的方法。第二、继承SqlSessionTemplate 重写getSqlSessionFactory、getConfiguration和SqlSessionInterceptor这个拦截器。其中最为关键还是继承SqlSessionTemplate 并重写里面的方法。而Spring整合MyBatis也有两种方式,一种是配置MapperFactoryBean,另一种则是利用MapperScannerConfigurer进行扫

3、描接口或包完成对象的自动创建。相对来说后者更方便些。MapperFactoryBean继承了SqlSessionDaoSupport也就是动态切换SqlSessionFactory的第一种方法,我们需要重写和实现SqlSessionDaoSupport方法,或者是继承MapperFactoryBean来重写覆盖相关方法。如果利用MapperScannerConfigurer的配置整合来切换SqlSessionFactory,那么我们就需要继承SqlSessionTemplate,重写上面提到的方法。在整合的配置中很多地方都是可以注入SqlSessionTemplate代替SqlSessionF

4、actory的注入的。因为SqlSessionTemplate的创建也是需要注入SqlSessionFactory的。二、实现代码1、继承SqlSessionTemplate 重写getSqlSessionFactory、getConfiguration和SqlSessionInterceptorpackage com.hoo.framework.mybatis.support;import static java.lang.reflect.Proxy.newProxyInstance;import static org.apache.ibatis.reflection.ExceptionUt

5、il.unwrapThrowable;import static org.mybatis.spring.SqlSessionUtils.closeSqlSession;import static org.mybatis.spring.SqlSessionUtils.getSqlSession;import static org.mybatis.spring.SqlSessionUtils.isSqlSessionTransactional;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;imp

6、ort java.sql.Connection;import java.util.List;import java.util.Map;import org.apache.ibatis.exceptions.PersistenceException;import org.apache.ibatis.executor.BatchResult;import org.apache.ibatis.session.Configuration;import org.apache.ibatis.session.ExecutorType;import org.apache.ibatis.session.Resu

7、ltHandler;import org.apache.ibatis.session.RowBounds;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.MyBatisExceptionTranslator;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.dao.support.PersistenceExce

8、ptionTranslator;import org.springframework.util.Assert;/* function: 继承SqlSessionTemplate 重写相关方法* author hoojo* createDate 2013-10-18 下午03:07:46* file CustomSqlSessionTemplate.java* package com.hoo.framework.mybatis.support* project SHMB* blog * email hoojo_* version 1.0*/public class CustomSqlSessio

9、nTemplate extends SqlSessionTemplate private final SqlSessionFactory sqlSessionFactory; private final ExecutorType executorType; private final SqlSession sqlSessionProxy; private final PersistenceExceptionTranslator exceptionTranslator; private Map targetSqlSessionFactorys; private SqlSessionFactory

10、 defaultTargetSqlSessionFactory; public void setTargetSqlSessionFactorys(Map targetSqlSessionFactorys) this.targetSqlSessionFactorys = targetSqlSessionFactorys; public void setDefaultTargetSqlSessionFactory(SqlSessionFactory defaultTargetSqlSessionFactory) this.defaultTargetSqlSessionFactory = defau

11、ltTargetSqlSessionFactory; public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType(); public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) this(sqlSessionFactor

12、y, executorType, new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration() .getEnvironment().getDataSource(), true); public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) super(sqlSessionFactory,

13、executorType, exceptionTranslator); this.sqlSessionFactory = sqlSessionFactory; this.executorType = executorType; this.exceptionTranslator = exceptionTranslator; this.sqlSessionProxy = (SqlSession) newProxyInstance( SqlSessionFactory.class.getClassLoader(), new Class SqlSession.class , new SqlSessio

14、nInterceptor(); this.defaultTargetSqlSessionFactory = sqlSessionFactory; Override public SqlSessionFactory getSqlSessionFactory() SqlSessionFactory targetSqlSessionFactory = targetSqlSessionFactorys.get(CustomerContextHolder.getContextType(); if (targetSqlSessionFactory != null) return targetSqlSess

15、ionFactory; else if (defaultTargetSqlSessionFactory != null) return defaultTargetSqlSessionFactory; else Assert.notNull(targetSqlSessionFactorys, Property targetSqlSessionFactorys or defaultTargetSqlSessionFactory are required); Assert.notNull(defaultTargetSqlSessionFactory, Property defaultTargetSq

16、lSessionFactory or targetSqlSessionFactorys are required); return this.sqlSessionFactory; Override public Configuration getConfiguration() return this.getSqlSessionFactory().getConfiguration(); public ExecutorType getExecutorType() return this.executorType; public PersistenceExceptionTranslator getP

17、ersistenceExceptionTranslator() return this.exceptionTranslator; /* * inheritDoc */ public T selectOne(String statement) return this.sqlSessionProxy. selectOne(statement); /* * inheritDoc */ public T selectOne(String statement, Object parameter) return this.sqlSessionProxy. selectOne(statement, para

18、meter); /* * inheritDoc */ public Map selectMap(String statement, String mapKey) return this.sqlSessionProxy. selectMap(statement, mapKey); /* * inheritDoc */ public Map selectMap(String statement, Object parameter, String mapKey) return this.sqlSessionProxy. selectMap(statement, parameter, mapKey);

19、 /* * inheritDoc */ public Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) return this.sqlSessionProxy. selectMap(statement, parameter, mapKey, rowBounds); /* * inheritDoc */ public List selectList(String statement) return this.sqlSessionProxy. selectList(statem

20、ent); /* * inheritDoc */ public List selectList(String statement, Object parameter) return this.sqlSessionProxy. selectList(statement, parameter); /* * inheritDoc */ public List selectList(String statement, Object parameter, RowBounds rowBounds) return this.sqlSessionProxy. selectList(statement, par

21、ameter, rowBounds); /* * inheritDoc */ public void select(String statement, ResultHandler handler) this.sqlSessionProxy.select(statement, handler); /* * inheritDoc */ public void select(String statement, Object parameter, ResultHandler handler) this.sqlSessionProxy.select(statement, parameter, handl

22、er); /* * inheritDoc */ public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) this.sqlSessionProxy.select(statement, parameter, rowBounds, handler); /* * inheritDoc */ public int insert(String statement) return this.sqlSessionProxy.insert(statement); /* *

23、 inheritDoc */ public int insert(String statement, Object parameter) return this.sqlSessionProxy.insert(statement, parameter); /* * inheritDoc */ public int update(String statement) return this.sqlSessionProxy.update(statement); /* * inheritDoc */ public int update(String statement, Object parameter

24、) return this.sqlSessionProxy.update(statement, parameter); /* * inheritDoc */ public int delete(String statement) return this.sqlSessionProxy.delete(statement); /* * inheritDoc */ public int delete(String statement, Object parameter) return this.sqlSessionProxy.delete(statement, parameter); /* * in

25、heritDoc */ public T getMapper(Class type) return getConfiguration().getMapper(type, this); /* * inheritDoc */ public void commit() throw new UnsupportedOperationException(Manual commit is not allowed over a Spring managed SqlSession); /* * inheritDoc */ public void commit(boolean force) throw new U

26、nsupportedOperationException(Manual commit is not allowed over a Spring managed SqlSession); /* * inheritDoc */ public void rollback() throw new UnsupportedOperationException(Manual rollback is not allowed over a Spring managed SqlSession); /* * inheritDoc */ public void rollback(boolean force) thro

27、w new UnsupportedOperationException(Manual rollback is not allowed over a Spring managed SqlSession); /* * inheritDoc */ public void close() throw new UnsupportedOperationException(Manual close is not allowed over a Spring managed SqlSession); /* * inheritDoc */ public void clearCache() this.sqlSess

28、ionProxy.clearCache(); /* * inheritDoc */ public Connection getConnection() return this.sqlSessionProxy.getConnection(); /* * inheritDoc * since 1.0.2 */ public List flushStatements() return this.sqlSessionProxy.flushStatements(); /* * Proxy needed to route MyBatis method calls to the proper SqlSess

29、ion got from Springs Transaction Manager It also * unwraps exceptions thrown by code Method#invoke(Object, Object.) to pass a code PersistenceException to * the code PersistenceExceptionTranslator. */ private class SqlSessionInterceptor implements InvocationHandler public Object invoke(Object proxy, Method method, Object args) throws Throwable final SqlSession sqlSession = getSqlSession( CustomSqlSessionTemplate.this.getSqlSessionFactory(), CustomSqlSessionTemplate.this.executorType, CustomSqlSessionTemplate.this.exception

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1