baomidou / dynamic-datasource

dynamic datasource for springboot 多数据源 动态数据源 主从分离 读写分离 分布式事务
https://www.kancloud.cn/tracy5546/dynamic-datasource/2264611
Apache License 2.0
4.74k stars 1.19k forks source link

4.1.3版本报com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty不存在 #648

Closed lijl-vb closed 4 months ago

lijl-vb commented 4 months ago

从3.2.0升级到4.1.3版本后,报com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty不存在。

然后替换为com.baomidou.dynamic.datasource.creator.DataSourceProperty,又报“com.baomidou.dynamic.datasource.creator.DataSourceProperty无法转换为com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty”

以上错误在下面这段代码抛出: DataSourceProperty newDataSource = new DataSourceProperty(); newDataSource.setDriverClassName(“com.mysql.cj.jdbc.Driver”); newDataSource.setUrl(“###”); newDataSource.setUsername(“###”); newDataSource.setPassword(“###”); dynamicDataSourceProperties.getDatasource().put("pool",newDataSource);

麻烦帮看一下是什么原因?应该怎么修改,我想实现的功能是从数据库读取数据源配置信息,然后添加到数据源池中。

huayanYu commented 4 months ago

不知道,建议用最新版,不行就提供最简化没有多余依赖的demo项目

lijl-vb commented 4 months ago

不知道,建议用最新版,不行就提供最简化没有多余依赖的demo项目

我使用最新版的4.3.1也是一样。替换为com.baomidou.dynamic.datasource.creator.DataSourceProperty后,编译的时候是不报错的,是在运行的时候才报“com.baomidou.dynamic.datasource.creator.DataSourceProperty无法转换为com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty”。

demo代码如下,麻烦再帮看一下?

package org.jeecg.modules.data.service.impl;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
//import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.data.entity.DataPoolSource;
import org.jeecg.modules.data.entity.DataPoolSql;
import org.jeecg.modules.data.service.DataPoolSourceService;
import org.jeecg.modules.data.mapper.DataPoolSourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DataPoolSourceServiceImpl extends ServiceImpl<DataPoolSourceMapper, DataPoolSource>
    implements DataPoolSourceService{

    @Autowired
    private DataPoolSourceMapper dataPoolSourceMapper;

    @Autowired
    private DynamicDataSourceProperties dynamicDataSourceProperties;

    @Autowired
    private DynamicRoutingDataSource dynamicRoutingDataSource;

    @Override
    public void initDataSource(DataPoolSql dataPoolSql) {

        DataPoolSource datasourceEntity = dataPoolSourceMapper.selectById(dataPoolSql.getDataSourceId());

        addDataSource(datasourceEntity);
    }

    @Override
    public void addDataSource(DataPoolSource dataPoolSource) {
        String url = "jdbc:mysql://" + dataPoolSource.getDataBaseIp() + ":" + dataPoolSource.getDataBasePort() + "/" + dataPoolSource.getDataBaseName()+"?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
        String driverClassName = "com.mysql.cj.jdbc.Driver";

        DataSourceProperty newDataSource = new DataSourceProperty();
        newDataSource.setDriverClassName(driverClassName);
        newDataSource.setUrl(url);
        newDataSource.setUsername(dataPoolSource.getUserName());
        newDataSource.setPassword(dataPoolSource.getPassWord());
        dynamicDataSourceProperties.getDatasource().put("pool",newDataSource);

        try {
            dynamicRoutingDataSource.afterPropertiesSet();
        } catch (Exception e) {
            throw new RuntimeException("数据源连接失败,url:" + url);
        }
    }
}
zjh0517 commented 4 months ago

请问问题解决了吗?您知道如何将数据源配置在配置中心读取吗?直接配置启动会报错,找不到数据源

huayanYu commented 4 months ago

版本冲突