springboot2.4+druid1.2.5整合,新版只需yml配置,无需配置类

前言

因网上各种老旧版本整合+资料少,被坑了一天,特把笔记搬到博客上,供同样被坑的伙伴参考

现在springboot已经有druid的starter了,所以直接在yml里面配就行了!

不要再去搞配置类了!

 

环境准备

pom.xml

导入druid starter

<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

 我的完整pom.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <g
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过创建一个配置配置springboot+DruidDataSource+shardingsphere的数据库配置,而不是将配置信息放在yml文件中。以下是一个示例配置的代码: ```java @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DruidDataSourceBuilder.create().build(); } @Bean public DataSourceProxy dataSourceProxy(DataSource dataSource) { return new DataSourceProxy(dataSource); } @Bean public DataSourceRule dataSourceRule(DataSourceProxy dataSourceProxy) { DataSourceRule dataSourceRule = new DataSourceRule(dataSourceProxy); // 配置分库分表规则 // dataSourceRule.addDataSourceRule(...) // dataSourceRule.addTableRule(...) return dataSourceRule; } @Bean public ShardingRule shardingRule(DataSourceRule dataSourceRule) { ShardingRule shardingRule = ShardingRule.builder() .dataSourceRule(dataSourceRule) // 配置分片规则 // shardingRule.getTableRuleConfigs().add(...) .build(); return shardingRule; } @Bean public DataSource dataSource(ShardingRule shardingRule) throws SQLException { return ShardingDataSourceFactory.createDataSource(shardingRule); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); // 配置其他属性 // sessionFactory.setMapperLocations(...) return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 您可以根据自己的求在配置中添加适当的配置,比如配置数据源、分库分表规则、分片规则等。这样,您就可以将数据库配置信息集中在一个配置中,而不是放在yml文件中。 #### 引用[.reference_title] - *1* *2* [springboot+druid连接池+mybatisplus+shardingsphere5.0alpha实现分库分表实战案例](https://blog.csdn.net/qq_40726812/article/details/120594172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Springboot+mybatis+shardingsphere 分库分表](https://blog.csdn.net/l_ian/article/details/120547449)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值