Spring Boot配置多数据源

2021-07-07  本文已影响0人  十毛tenmao

业务发展后,一些查询类型的请求需要切换到备用数据源,这个时候需要使用多数据源配置,公司的项目使用的不是主备,而是需要去查询postgresql库(业务库是mysql,所以不是简单的主从,而且语句也可能变化,因为mysql是做了分库的)

整体文件结构

代码文件结构

多数据源配置

@Configuration
@MapperScan(basePackages = {"com.tenmao.multidatasource.mapper.primary"}, sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSourceConfiguration {

    @Bean("primary")
    //从配置文件中读取前缀为spring.datasource.primary的配置作为primary数据源
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("primarySqlSessionFactory")
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primary") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //配置xml文件
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }
}
@Configuration
@MapperScan(basePackages = {"com.tenmao.multidatasource.mapper.second"}, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfiguration {

    @Bean("second")
   //从配置文件中读取前缀为spring.datasource.second的配置作为second数据源 
  @ConfigurationProperties(prefix = "spring.datasource.second")
   public DataSource primaryDataSource() {
       return DataSourceBuilder.create().build();
   }

   @Bean("secondSqlSessionFactory")
   public SqlSessionFactory secondSqlSessionFactory(@Qualifier("second") DataSource dataSource) throws Exception {
       SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
       bean.setDataSource(dataSource);
       //配置xml文件
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
       return bean.getObject();
   }
}

领域类和Mapper类

@Data
public class User {
   private Integer uid;
   private String name;
}
@Data
public class Phone {
   private String model;
   private Integer price;
}
@Mapper
public interface UserMapper {
   @Select("SELECT * FROM user WHERE id=#{id}")
   User selectOne(int id);
}
@Mapper
public interface PhoneMapper {
   @Select("SELECT * FROM phone WHERE id=#{id}")
   Phone selectOne(int id);
}

启动类和配置文件

@RestController
@SpringBootApplication
public class MultidatasourceApplication {
   @Resource
   private UserMapper userMapper;

   @Resource
   private PhoneMapper phoneMapper;

   public static void main(String[] args) {
       SpringApplication.run(MultidatasourceApplication.class, args);
   }

   @GetMapping("getOneUser")
   public User getOneUser(@RequestParam Integer id) {
       return userMapper.selectOne(id);
   }

   @GetMapping("getOnePhone")
   public Phone getOnePhone(@RequestParam Integer id) {
       return phoneMapper.selectOne(id);
   }
}
spring:
 datasource:
   primary:
     jdbc-url: jdbc:mysql://localhost:3306/test?useAffectedRows=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&serverTimezone=GMT%2B8
     username: root
     password: root
   second:
     jdb-url: jdbc:mysql://192.168.1.101:3306/tenmao?useAffectedRows=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&serverTimezone=GMT%2B8
     username: tenmao
     password: tenmao

其他特性

   second:
     jdb-url: jdbc:mysql://192.168.1.101:3306/tenmao?useAffectedRows=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&serverTimezone=GMT%2B8
     username: tenmao
     password: tenmao
     # 设置second数据源为只读
     read-only: true

如果尝试修改只读数据源则会抛出异常:java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

@Bean("primarySqlSessionFactory")
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primary") DataSource dataSource) throws Exception {
    //如果使用MyBatis-Plus,则需要把SqlSessionFactoryBean替换为MybatisSqlSessionFactoryBean
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    //配置xml文件
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
    return bean.getObject();
}

常见问题

上一篇 下一篇

猜你喜欢

热点阅读