栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

spring boot(四) 多数据源

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot(四) 多数据源

前言

       前一篇中我们使用spring boot+mybatis创建了单一数据源,其中单一数据源不需要我们自己手动创建,spring boot自动配置在程序启动时会替我们创建好数据源。

准备工作

application.yml中配置connection的4个属性

spring:  
  datasource:    
    read:   
      driver-class-name: com.mysql.jdbc.Driver      
      url: jdbc:mysql://192.168.80.129:3306/test      
      username: root      
      password: 123456    
    write:      
      driver-class-name: com.mysql.jdbc.Driver      
      url: jdbc:mysql://192.168.80.129:3306/test      
      username: root      
      password: 123456


举个栗子

      1、多数据源主要是需要我们手动来创建DataSource、SqlSessionFactory、SqlSessionTemplate。这里我们基于同一个库来创建读写分离的数据源。这里两个方法的返回值都是javax.sql.DataSource。

@Configuration
public class DataSourceConfig {      
@Primary    
@Bean(name="readDataSource")    
@ConfigurationProperties(prefix = "spring.datasource.read")    
public DataSource readDataSource(){        
   return DataSourceBuilder.create().build();    
}       

@Bean(name="writeDataSource")    
@ConfigurationProperties(prefix = "spring.datasource.write")    
public DataSource writeDataSource(){        
   return DataSourceBuilder.create().build();    
}  
}

     2、读写分离的配置类。也就是分别创建读写的SqlSessionFactory和SqlSessionTemplate

@Configuration
@MapperScan(basePackages = {"com.zhangfei.dao.read"},sqlSessionFactoryRef = "readSqlSessionFactory")
public class MyBatisDbAConfig {     
   @Autowired    
   @Qualifier("readDataSource")    
   private DataSource dataSource;      
   
   @Bean    
   public SqlSessionFactory readSqlSessionFactory() throws Exception{        
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();        
        sqlSessionFactoryBean.setDataSource(dataSource);         
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();        
        Resource[] resource=resolver.getResources("classpath:mybatis/read/*.xml");        
        sqlSessionFactoryBean.setMapperLocations(resource);         
        return sqlSessionFactoryBean.getObject();   
   }     
   @Bean    
   public SqlSessionTemplate readSqlSession() throws Exception{        
       SqlSessionTemplate sqlSessionTemplate=new SqlSessionTemplate(readSqlSessionFactory());        
       return sqlSessionTemplate;   
      }
   }
@Configuration
@MapperScan(basePackages = {"com.zhangfei.dao.write"},sqlSessionFactoryRef = "writeSqlSessionFactory")
public class MyBatisDbBConfig {     
   @Autowired    
   @Qualifier("writeDataSource")    
   private DataSource dataSource;      
   @Bean    
   public SqlSessionFactory writeSqlSessionFactory() throws Exception{        
      SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();        
      sqlSessionFactoryBean.setDataSource(dataSource);         
      PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();        
      Resource[] resource=resolver.getResources("classpath:mybatis/write/*.xml");        
      sqlSessionFactoryBean.setMapperLocations(resource);         
      return sqlSessionFactoryBean.getObject();    
      }     
      @Bean    
      public SqlSessionTemplate writeSqlSession() throws Exception{        
      SqlSessionTemplate sqlSessionTemplate=new SqlSessionTemplate(writeSqlSessionFactory());         
      return sqlSessionTemplate;    
      } 
 }

      3、read包下的dao接口

public interface StudentReadDao {     
   List getStudentList();     
   Student getById(long id); 
 }

     4、wite包下的dao接口

public interface StudentWriteDao {     
    int delete(long id);     
    int insert(Student student);     
    int update(Student student);  
}

    5、分别创建读写的mapper文件。我本地分别创建了:

resources/mybatis/read/studentdao.xml、

/resources/mybatis/write/studentdao.xml


    
        
select * from student;    
     
        
select * from student where id=#{id};    
  

     
        
 insert into student (name,age) values (#{name},#{age})    
      
         
 update student set name=#{name},age=#{age} where id=#{id}    
      
         
 delete from student where id=#{id}    
 
 

OK。以上几部就搞定了读写分离的准备工作,接着就可以在controller里调用了。 准备工作完成后,还有重要的一点就是需要在程序入口处排除springboot自动属性提供的数据源  @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

@RestController@RequestMapping("/student")
public class StudentController {      
@Autowired    
StudentReadDao studentDao;     
@GetMapping("/getbyid/{id}/")    
public Student getById(@PathVariable("id")long id){         
Student student=studentDao.getById(id);        
return student;    
}      
@GetMapping("/all/")    
public List getAll(){        
return studentDao.getStudentList();   
 }  
 }

总结

       好了,基本上又是3分钟就搞定了SpringBoot+MyBatis多数据源或者叫做读写分离的工作。那么不知道你又没有反问我们手动创建的DataSource具体类型是什么呢? 这里我们在这里只写了javax.sql.DataSource接口。 我本地用的是springboot 2.0.2 ,当前 DataSourceBuilder 只支持3种类型的数据源: com.zaxxer.hikari.HikariDataSource、org.apache.tomcat.jdbc.pool.DataSource、org.apache.commons.dbcp2.BasicDataSource。可以在DataSourceBuilder类中看到相关代码。那么现在这种情况因为我们引用的内嵌tomcat,所以我们这里返回的数据源类型是org.apache.tomcat.jdbc.pool.DataSource。 可以用instanceof验证一下具体的数据源类型。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235731.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号