Mybatis多数据源调用

阅读更多

环境:intellij15jdk1.8maven3

 

参考:http://www.jb51.net/article/104053.htm

 

1、  生成ssm项目,参见http://liguanshi.iteye.com/blog/2413023

2、  jdbc.properties文件中添加数据库连接

 

 

#============================================================================
# postgresql
#============================================================================
jdbc.postgresql.driver = org.postgresql.Driver
jdbc.postgresql.url = jdbc:postgresql://127.0.0.1:5432/cz?characterEncoding=UTF-8
jdbc.postgresql.username = postgres
jdbc.postgresql.password = ******

#============================================================================
# oracle
#============================================================================
jdbc.oracle.driver=oracle.jdbc.driver.OracleDriver
jdbc.oracle.url=jdbc:oracle:thin:@smart-dafeng:1521:orcl
jdbc.oracle.username=df
jdbc.oracle.password=******

 

 

3、 applicationContext-dao.xml中添加数据库连接及multipleDataSourcebean

 




    
    
    
    
        
        
        
        
        
        
        
    

    
    
        
        
        
        
        
        
        
    
    
        
        
            
                
                
            
        
    
    
        
    

    
    
        
    

    
    
    

 

 

 

4、 com.elin4it.ssm.utils添加MultipleDataSource

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * Created by Administrator on 2018/3/21.
 */
public class MultipleDataSource extends AbstractRoutingDataSource {
    private static final ThreadLocal dataSourceKey = new InheritableThreadLocal();

    public static void setDataSourceKey(String dataSource) {
        dataSourceKey.set(dataSource);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return dataSourceKey.get();
    }
}

 

 

5、  将applicationContext-transaction.xml内容注释掉

 





    
        
    

    
        
            
            
            
            
        
    

    
        
    

 

 

6、 UserServiceImpl中添加数据库选择语句:MultipleDataSource.setDataSourceKey("oracleDataSource");

 

完成代码如下所示:

 

package com.elin4it.ssm.service;

import com.elin4it.ssm.mapper.StudentsMapper;
import com.elin4it.ssm.pojo.Students;
import com.elin4it.ssm.utils.MultipleDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2018/3/21.
 */
@Service
public class UserServiceImpl implements UserService {
    //User接口 此处注入UserMapper时需要在UserMapper接口上添加@Component进行依赖注入
    @Autowired
    private StudentsMapper userMapper;
    public Students findUser() throws Exception {
// 此处切换数据库,输出不同结果
//        MultipleDataSource.setDataSourceKey("postgresqlDataSource");
        MultipleDataSource.setDataSourceKey("oracleDataSource");
        //调用mapper类中的selectByExample方法,如果传入类型为null,则表示无条件查找
        Students users = userMapper.selectByPrimaryKey(2);
        return users;
    }
}

 

7、  pom中添加oracle数据库依赖:


    
      com.oracle
      ojdbc6
      11.2.0.1.0

 

8、  运行。Oracle数据库输出结果:

 

Mybatis多数据源调用_第1张图片

修改UserServiceImpl.java代码后,Postgresql数据库输出结果:

 
Mybatis多数据源调用_第2张图片

 

  • Mybatis多数据源调用_第3张图片
  • 大小: 23.6 KB
  • Mybatis多数据源调用_第4张图片
  • 大小: 22.4 KB
  • ssm-multipleDataSource.rar (37 KB)
  • 下载次数: 0
  • 查看图片附件

你可能感兴趣的:(javaWeb,mybatis,spring)