shardingjdbc与springboot整合遇到dataSource无法加载

具体遇到的错误:

The bean 'dataSource', defined in class path resource [io/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

一开始从网上搜查,在配置文件里加了该配置,问题没有解决。

spring.main.allow-bean-definition-overriding=true

启动类加

(exclude = {org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.class})

引起空指针异常。
后来以为是jar包冲突,对jar进行排查,未发现问题。
最后发现是配置文件缺少properties尾缀的原因。
在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
将一个druid-springboot-starter的springboot项目转换成使用shardingjdbcspringboot项目的步骤如下: 1. 在pom.xml文件中增sharding-jdbc-spring-boot-starter和mysql-connector-java的依赖。 ```xml <dependency> <groupId>io.shardingjdbc</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${shardingjdbc.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> ``` 2. 在application.yml或application.properties中配置sharding-jdbc的数据源和分库分表规则。 ```yaml spring: shardingsphere: datasource: names: ds0, ds1 # 数据源名称 ds0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db0?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root ds1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root sharding: tables: t_order: actual-data-nodes: ds$->{0..1}.t_order_$->{0..1} table-strategy: inline: sharding-column: order_id algorithm-expression: t_order_$->{order_id % 2} key-generator: type: SNOWFLAKE column: order_id binding-tables: t_order default-database-strategy: inline: sharding-column: user_id algorithm-expression: ds$->{user_id % 2} default-table-strategy: none: ``` 3. 在代码中使用sharding-jdbc的数据源访问数据库。 ```java @Autowired private JdbcTemplate jdbcTemplate; public void query() { String sql = "select * from t_order where user_id = ?"; List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, 10); System.out.println(resultList); } ``` 通过以上步骤,就可以将一个druid-springboot-starter的springboot项目转换成使用shardingjdbcspringboot项目了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值