# MultiSource **Repository Path**: esionwong/multi-source ## Basic Information - **Project Name**: MultiSource - **Description**: mybatis多数据源配置例子 - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-12-10 - **Last Updated**: 2023-12-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### Mybatis 多数据源配置 ##### 1、配置 - MybatisPlugConfig ```java @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } ``` - DataSourceConfig ```java @Bean(name = "db1SqlSessionFactory") @Primary public SqlSessionFactory dbSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource, @Value("classpath*:mapper/db1/*Mapper.xml") Resource[] mapperLocations) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //这个的getResources指向的是你的mapper.xml文件,相当于在yml中配置的mapper-locations,此处配置了yml中就不用配置,或者说不会读取yml中的该配置。 bean.setMapperLocations(mapperLocations); // 设置分页 bean.setPlugins(mybatisPlusInterceptor); MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setMapUnderscoreToCamelCase(true); // 配置打印sql语句,如果不配置,多数据源下不会打印 configuration.setLogImpl(StdOutImpl.class); bean.setConfiguration(configuration); return bean.getObject(); } ``` ##### 测试 ```shell GET http://127.0.0.1:8080/page { "msg": null, "result": { "records": [ { "id": 1, "username": "test1", "birthday": null, "sex": "男", "address": "123" }, { "id": 2, "username": "test1", "birthday": null, "sex": "男", "address": "123" }, { "id": 3, "username": "test2", "birthday": null, "sex": "男", "address": "123" }, { "id": 4, "username": "test3", "birthday": null, "sex": "男", "address": "123" }, { "id": 5, "username": "test4", "birthday": null, "sex": "男", "address": "123" }, { "id": 6, "username": "test5", "birthday": null, "sex": "男", "address": "123" }, { "id": 7, "username": "test6", "birthday": null, "sex": "男", "address": "123" }, { "id": 8, "username": "test7", "birthday": null, "sex": "男", "address": "123" }, { "id": 9, "username": "test8", "birthday": null, "sex": "男", "address": "123" }, { "id": 10, "username": "test9", "birthday": null, "sex": "男", "address": "123" } ], "total": 21, "size": 10, "current": 1, "orders": [], "optimizeCountSql": true, "searchCount": true, "maxLimit": null, "countId": null, "pages": 3 }, "succ": "ok" } ```