springboot配置多数据源,及多数据源下驼峰命名转换失效问题解决

配置application.yml

spring:
  datasource:
#   第一个数据源
    first:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.41.128:3306/first?useUnicode=true&characterEncoding=utf8&useSSL=false
      username: root
      password: 123456
    #   第二个数据源
    second:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://172.16.0.9:3306/second?useUnicode=true&characterEncoding=utf8&useSSL=false
      username: root
      password: 123456

获取DB配置

@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.first")
public class FirstDataBaseProperties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
}
@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.second")
public class SecondDataBaseProperties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
}

配置数据源

@Configuration
// 第一个数据源与Mapper接口绑定
@MapperScan(basePackages = "com.deri.task.dao.first",sqlSessionTemplateRef ="firstSqlSessionTemplate")
public class FirstDataSourceConfig {
    @Autowired
    FirstDataBaseProperties firstDataBaseProperties;

    @Bean(name = "firstDS")
    // 多数据源情况下需要指定主数据源
    @Primary
    public DataSource getFirstDataSource() {
        DataSource build =  DataSourceBuilder.create()
                .driverClassName(firstDataBaseProperties.getDriverClassName())
                .url(firstDataBaseProperties.getUrl())
                .username(firstDataBaseProperties.getUsername())
                .password(firstDataBaseProperties.getPassword())
                .build();
        return build;
    }

    @Bean(name = "firstSqlSessionFactory")
    @Primary
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDS") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean("firstTransactionManger")
    @Primary
    public DataSourceTransactionManager firstTransactionManger(@Qualifier("firstDS") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建SqlSessionTemplate

    @Bean(name = "firstSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
@Configuration
// 第二个数据源与Mapper接口绑定
@MapperScan(basePackages = "com.deri.task.dao.second",sqlSessionTemplateRef ="secondSqlSessionTemplate")
public class FirstDataSourceConfig {
    @Autowired
    SecondDataBaseProperties secondDataBaseProperties;

    @Bean(name = "secondDS")
    public DataSource getFirstDataSource() {
        DataSource build =  DataSourceBuilder.create()
                .driverClassName(secondDataBaseProperties.getDriverClassName())
                .url(secondDataBaseProperties.getUrl())
                .username(secondDataBaseProperties.getUsername())
                .password(secondDataBaseProperties.getPassword())
                .build();
        return build;
    }

    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("secondDS") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean("secondTransactionManger")
    public DataSourceTransactionManager firstTransactionManger(@Qualifier("secondDS") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建SqlSessionTemplate

    @Bean(name = "secondSqlSessionTemplate")
    public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

参考链接

  • https://www.cnblogs.com/zhoutao825638/p/10382261.html

已知问题

  • mybatis驼峰命名转换失效问题
# 默认在application.yml中配置,但是在多数据源场景下该配置失效,需要在代码中显式指定.
mybatis:
  configuration:
    map-underscore-to-camel-case: true
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("secondDS") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    // 指定驼峰转换
    bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return bean.getObject();
}
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于多数据源配置驼峰映射不生效的问题,可能有以下几个原因和解决方案: 1. MyBatis 配置文件设置问题:请确保每个数据源的 MyBatis 配置文件中,已经正确开启了驼峰命名规则的映射。在配置文件的 `<settings>` 标签中添加如下设置即可: ```xml <setting name="mapUnderscoreToCamelCase" value="true" /> ``` 这样就可以将数据库的下划线命名转换驼峰命名。 2. 实体类字段和数据库字段不匹配:请检查实体类中的属性名和数据库表的字段名是否一致,包括大小写。如果不一致,可以使用 `@Column` 注解或者在配置文件中进行手动映射,确保二者对应。 3. MyBatis 的 resultMap 配置问题:如果使用 resultMap 进行字段映射,需要确保 resultMap 中的字段名与数据库表中的字段名一致。可以使用 `<resultMap>` 标签进行配置,并在 `<result>` 标签中使用 column 属性指定数据库字段名。 4. 数据库连接驱动问题:有些数据库连接驱动默认是不支持驼峰命名转换的,您可以尝试更换合适的数据库连接驱动,例如使用 druid 连接池等。 5. 缓存问题:如果您启用了缓存机制,可能会导致数据不及时更新。可以尝试关闭缓存,或者在更新操作后手动清除缓存。 请根据具体情况逐一排查上述原因,并进行相应的解决方案尝试,希望能帮到您!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值