比较常用的配置,多数据源、主从等等。
大概的逻辑是:
1、spring-mybatis.xml中配置多个数据源
2、使用Spring提供的AbstractRoutingDataSource类来根据请求路由到不同的数据源
3、ThreadLocal解决线程安全问题
详情参考:
【Java】一次SpringMVC+ Mybatis 配置多数据源经历
利用Spring的AbstractRoutingDataSource解决多数据源的问题
spring官网demo:Dynamic DataSource Routing
一直没调通,切换后仍是默认数据库!!!
=================放弃spring动态数据源,用mybatis的environments来解决=============
关于mybatis的environments,请查询官网文档:http://www.mybatis.org/mybatis-3/zh/configuration.html#environments
一、mybatis-conf.xml 配置两个数据源:<environment id="env_stg">,<environment id="env_vtp">

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 引入配置文件 --> <properties resource="jdbc.properties"/> <settings> <!-- 驼峰命名自动映射 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- bean短别名 --> <typeAliases> <package name="qa.vop.model" /> </typeAliases> <!-- 多环境配置 --> <environments default="env_stg"> <environment id="env_stg"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> <environment id="env_vtp"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url_vtp}" /> <property name="username" value="${username_vtp}" /> <property name="password" value="${password_vtp}" /> </dataSource> </environment> </environments> <!-- xml映射文件 --> <mappers> <package name="qa.vop.dao"/> </mappers> </configuration>
二、写一个类,根据不同的环境来build SqlSessionFactory

public class MySQLSessionFactory { private SqlSessionFactory sqlSessionFactory = null; public SqlSessionFactory getSqlSessionFactory(String cfgFilePath,String env) { InputStream is = null; try { is = Resources.getResourceAsStream("mybatis-conf.xml"); if (null == env || env.isEmpty()) { sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } else { sqlSessionFactory = new SqlSessionFactoryBuilder().build(is,env); } } catch (IOException e) { System.out.println(e.getMessage()); } return sqlSessionFactory; } }
三、controller中,根据不同环境,来切换数据源。

@Controller public class SuperController{ private static final String MYBATIS_CFG = "mybatis-conf.xml"; private static SqlSession session = new MySQLSessionFactory().getSqlSessionFactory(MYBATIS_CFG,null).openSession(); @RequestMapping("/queryJit") public ModelAndView getJit(ModelAndView mv){ mv.addObject("jitList", session.getMapper(JitMapper.class).selectAllJits() ); mv.setViewName("jit"); return mv; } @RequestMapping("/queryVTP") public ModelAndView getVTPRunResult(ModelAndView mv){ SqlSession session1 = new MySQLSessionFactory().getSqlSessionFactory(MYBATIS_CFG,"env_vtp").openSession(); mv.addObject("vtpRunResultList", session1.getMapper(VtpRunResultMapper.class).selectAllResults() ); mv.setViewName("quality-vtp"); return mv; } }