文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

springboot 多mysql,多oracle,多mongodb数据源

2023-10-25 19:03

关注

目录

前言

一、springboot yml文件配置

三、mysql 和oracle 多数据源的切换

四、测试方法的调用

五、mongodb多数据源配置

六、mongodb 测试方法的调用

总结


前言

最近的工作中要做数据的迁移,用的数据库有 两个mysql库,两个oracle库,两个mongodb库。需要把一个主mysql库的数据迁移到oracel,中途需要关联查询 mysql库,oralce库,mongodb库从中取业务数据。先做个简单的demo 研究一下多数据源。


一、springboot yml文件配置

application.yml 配置如下:

server:
    port: 8080

test1-datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/database1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: root
    password: 123456
test2-datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/database2?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: root
    password: 123456
test3-datasource:
    driverClassName: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@ (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxx)(PORT=61512))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xxx)))
    username: root
    password: 123456
test4-datasource:
    driverClassName: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxx)(PORT=61512))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xxx)))
    username: root
    password: 123456
#数据库连接池
# mongodb多数据源
spring:
  data:
    mongodb:
      one:
        uri: mongodb://localhost:27017/test
        database: test
      two:
        uri: mongodb://localhost:27017/test1
        database: test1


二、mysql 和oracle多数据源的配置

主要有以下五步:

1 根据yml中的配置创建数据源 DataSource

2 设置动态数据源DynamicDataSource

3 mapper 扫描配置 MapperScannerConfigurer

4 根据数据源创建SqlSessionFactory

5 配置事务管理器DataSourceTransactionManager

代码如下:

@Configuration // 该注解类似于spring配置文件public class MyBatisConfig implements EnvironmentAware {    private Environment environment;    @Override    public void setEnvironment(final Environment environment) {        this.environment = environment;    }        @Bean    public DataSource test1DataSource() throws Exception {        Properties props = new Properties();        props.put("driverClassName", environment.getProperty("test1-datasource.driverClassName"));        props.put("url", environment.getProperty("test1-datasource.url"));        props.put("username", environment.getProperty("test1-datasource.username"));        props.put("password", environment.getProperty("test1-datasource.password"));        return DruidDataSourceFactory.createDataSource(props);    }    @Bean    public DataSource test2DataSource() throws Exception {        Properties props = new Properties();        props.put("driverClassName", environment.getProperty("test2-datasource.driverClassName"));        props.put("url", environment.getProperty("test2-datasource.url"));        props.put("username", environment.getProperty("test2-datasource.username"));        props.put("password", environment.getProperty("test2-datasource.password"));        return DruidDataSourceFactory.createDataSource(props);    }        @Bean    public DataSource test3DataSource() throws Exception {    Properties props = new Properties();    props.put("driverClassName", environment.getProperty("test3-datasource.driverClassName"));    props.put("url", environment.getProperty("test3-datasource.url"));    props.put("username", environment.getProperty("test3-datasource.username"));    props.put("password", environment.getProperty("test3-datasource.password"));    return DruidDataSourceFactory.createDataSource(props);    }        @Bean    public DataSource test4DataSource() throws Exception {        Properties props = new Properties();        props.put("driverClassName", environment.getProperty("test4-datasource.driverClassName"));        props.put("url", environment.getProperty("test4-datasource.url"));        props.put("username", environment.getProperty("test4-datasource.username"));        props.put("password", environment.getProperty("test4-datasource.password"));        return DruidDataSourceFactory.createDataSource(props);    }        @Bean    @Primary    public DynamicDataSource dataSource(@Qualifier("test1DataSource") DataSource test1DataSource,            @Qualifier("test2DataSource") DataSource test2DataSource,            @Qualifier("test3DataSource") DataSource test3DataSource,            @Qualifier("test4DataSource") DataSource test4DataSource            ) {        Map targetDataSources = new HashMap<>();        targetDataSources.put(DatabaseType.test1, test1DataSource);        targetDataSources.put(DatabaseType.test2, test2DataSource);        targetDataSources.put(DatabaseType.test3, test3DataSource);        targetDataSources.put(DatabaseType.test4, test4DataSource);        DynamicDataSource dataSource = new DynamicDataSource();        dataSource.setTargetDataSources(targetDataSources);// 该方法是AbstractRoutingDataSource的方法        dataSource.setDefaultTargetDataSource(test2DataSource);// 默认的datasource设置为myTestDbDataSource        return dataSource;    }    @Bean    public MapperScannerConfigurer mapperScannerConfigurer() {        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();        scannerConfigurer.setBasePackage("com.xing.mapper");        Properties props = new Properties();        props.setProperty("mappers", "tk.mybatis.mapper.common.Mapper");//        props.setProperty("IDENTITY", "MYSQL"); 既有mysql 又有oracle        props.setProperty("notEmpty", "true");        scannerConfigurer.setProperties(props);        return scannerConfigurer;    }        @Bean    public SqlSessionFactory sqlSessionFactory(DynamicDataSource ds) throws Exception {        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();        fb.setDataSource(ds);// 指定数据源(这个必须有,否则报错)        // 下边两句仅仅用于*.xml文件,如果整个持久层操作不需要使用到xml文件的话(只用注解就可以搞定),则不加        fb.setTypeAliasesPackage("com.xing.model");// 指定基包        fb.setMapperLocations(resolver.getResources("classpath:mapper*.xml"));//        return fb.getObject();    }        @Bean    public DataSourceTransactionManager transactionManager(DynamicDataSource dataSource) throws Exception {        return new DataSourceTransactionManager(dataSource);    }}


三、mysql 和oracle 多数据源的切换

这里定义一个切面,根据不同的数据源的对应service动态切换。

代码如下:

@Aspect@Componentpublic class DataSourceAspect {        @Pointcut("execution(* com.xing.service.**.*(..))")    public void declareJointPointExpression() {    }    @Before("declareJointPointExpression()")    public void setDataSourceKey(JoinPoint point){        //根据连接点所属的类实例,动态切换数据源        if (point.getTarget() instanceof Test1Service                || point.getTarget() instanceof Test1ServiceImpl) {            DatabaseContextHolder.setDatabaseType(DatabaseType.test1);        }         else if (point.getTarget() instanceof Test3Service                || point.getTarget() instanceof Test3ServiceImpl) {         DatabaseContextHolder.setDatabaseType(DatabaseType.test3);        }        else if (point.getTarget() instanceof Test4Service                || point.getTarget() instanceof Test4ServiceImpl) {            DatabaseContextHolder.setDatabaseType(DatabaseType.test4);        }        else {//连接点所属的类实例是(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)            DatabaseContextHolder.setDatabaseType(DatabaseType.test2);        }    }}

四、测试方法的调用

代码如下:

@RestControllerpublic class TestDataSourceController {    @Autowired    private CommonService commonService;    @RequestMapping(value = "/test1", method = RequestMethod.GET)    public List selectUser() {        System.out.println("查询第一个数据源");        List list = commonService.selectUserInfo();        System.out.println(JSON.toJSONString(list));        return list;    }    @RequestMapping(value = "/test2", method = RequestMethod.GET)    public List userDetail() {        System.out.println("查询第二个数据源");        List list = commonService.selectUserDetail();        System.out.println(JSON.toJSONString(list));        return list;    }    @RequestMapping(value = "/test3", method = RequestMethod.GET)    public List user() {    System.out.println("查询第三个数据源");        List list = commonService.selectUser();        System.out.println(JSON.toJSONString(list));        return list;    }    @RequestMapping(value = "/test4", method = RequestMethod.GET)    public List userOrT1() {        System.out.println("查询第四个数据源");        List list = commonService.selectUserOrT1();        System.out.println(JSON.toJSONString(list));        return list;    }}

五、mongodb多数据源配置

分两个MongoTemplateConf配置,OneMongoTemplateConf配置第一个mongodb数据源,TwoMongoTemplateConf 配置第二个mongodb数据源。主要分两步: 第一步获取yml配置后创建MongoDatabaseFactory ,然后在创建MongoTemplate ,给bean 取个名。

OneMongoTemplateConf代码如下:

@Configuration@EnableMongoRepositories(mongoTemplateRef = "oneMongo")public class OneMongoTemplateConf implements EnvironmentAware{    private Environment environment;    @Override    public void setEnvironment(final Environment environment) {        this.environment = environment;    }        @Bean(name = "oneMongo")    @Primary    public MongoTemplate mongoTemplate() {        return new MongoTemplate(mongoDatabaseFactory());    }        @Bean(name = "oneMongoFactory")    @Primary    public MongoDatabaseFactory mongoDatabaseFactory() {        MongoClient client = MongoClients.create(environment.getProperty("spring.data.mongodb.one.uri"));        return new SimpleMongoClientDatabaseFactory(client, environment.getProperty("spring.data.mongodb.one.database"));    }}

TwoMongoTemplateConf 代码如下:

@Configuration@EnableMongoRepositories(mongoTemplateRef = "twoMongo")public class TwoMongoTemplateConf implements EnvironmentAware{    private Environment environment;    @Override    public void setEnvironment(final Environment environment) {        this.environment = environment;    }        @Bean(name = "twoMongo")    public MongoTemplate mongoTemplate() {        return new MongoTemplate(mongoDatabaseFactory());    }        @Bean(name = "twoMongoFactory")    public MongoDatabaseFactory mongoDatabaseFactory() {        MongoClient client = MongoClients.create(environment.getProperty("spring.data.mongodb.two.uri"));        return new SimpleMongoClientDatabaseFactory(client, environment.getProperty("spring.data.mongodb.two.database"));    }}

六、mongodb 测试方法的调用

注入MongoTemplate,注入时 需要用@Qualifier 注解指定用哪个MongoTemplate 实例。

代码如下:

    @Autowired    @Qualifier("oneMongo")    private MongoTemplate oneMongoTemplate;    @Autowired    @Qualifier("twoMongo")    private MongoTemplate twoMongoTemplate;        @RequestMapping("/mongo1")    public String testOneMongoInsert() {        Map data = Maps.newHashMap();        Map info = Maps.newHashMap();        data.put("name", "cgg");        data.put("attr", "one");        info.put("age" , 18);        data.put("info", info);//        oneMongoTemplate.insert(data, "inventory_27017");        List list = oneMongoTemplate.findAll(Map.class, "inventory_27017");        System.out.println(JSON.toJSONString(list));        return JSON.toJSONString(list);    }        @RequestMapping("/mongo2")    public String testTwoMongoInsert() {        Map data = Maps.newHashMap();        Map info = Maps.newHashMap();        data.put("name", "cgg");        data.put("attr", "two");        info.put("age" , 18);        data.put("info", info);//        twoMongoTemplate.insert(data, "inventory_27018");        List list = twoMongoTemplate.findAll(Map.class, "inventory_27018");        System.out.println(JSON.toJSONString(list));        return JSON.toJSONString(list);    }

总结

以上就是今天要讲的内容,具体完整的项目代码已放到github上。

完整项目代码已在github上:github代码

来源地址:https://blog.csdn.net/weixin_43171875/article/details/128834266

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯