ITPub博客

首页 > IT职业 > IT职场 > spring boot(四) 多数据源

spring boot(四) 多数据源

IT职场 作者:yinlin7937 时间:2021-09-09 09:32:54 0 删除 编辑

前言

       前一篇中我们使用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

mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        " 
    
     
  
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        " 
     
        
 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验证一下具体的数据源类型。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3193/viewspace-2804567/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: guava net包介绍
请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    78
  • 访问量
    787