2.2.5.RELEASE 6.0.6 1.1.10 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-aop mysql mysql-connector-java runtime com.alibaba druid
// 这个就不需要在启动类加了,相比较单数据源 @MapperScan("com.swapping.dao.mapper.mysql")
@SpringBootApplication
@ComponentScan(basePackages = {"com.swapping.*"}) @Slf4j public class SwappingApiApplication { public static void main(String[] args) { try { SpringApplication.run(SwappingApiApplication.class, args); System.out.println("SwappingApiApplication 启动成功......"); } catch (Exception e) { log.error("SwappingApiApplication 启动失败", e); } } }
yml配置文件如下,双数据源如下,多数据源就往下加配置就行 注意:相比较单数据源,应该用jdbc-url做key,否则会有运行时异常 多数据源java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName. ${my_first_mysql.host} 或 ${my_sencond_tidb.host} 可以是配置文件取的配置,也可以像username一样直接写死,都可以
# 驱动配置信息 spring: datasource: my-first-mysql: #连接池的配置信息 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: filters: max-active: 16 initial-size: 2 max-wait: 3000 minIdle: 2 time-between-eviction-runsMillis: 60000 min-evictable-idle-timeMillis: validation-query: SELECT 1 test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: false max-open-prepared-statements: -1 jdbc-url: jdbc:mysql://${my_first_mysql.host}:${my_first_mysql.port}/my_first_mysql_dbName?characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=GMT%2B8&connectTimeout=2000&socketTimeout= username: XXX password: XXX my-sencond-tidb: #连接池的配置信息 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: filters: max-active: 16 initial-size: 2 max-wait: 3000 minIdle: 2 time-between-eviction-runsMillis: 60000 min-evictable-idle-timeMillis: validation-query: SELECT 1 test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: false max-open-prepared-statements: -1 jdbc-url: jdbc:mysql://${my_sencond_tidb.host}:${my_sencond_tidb.port}/my_sencond_mysql_dbName?characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=GMT%2B8&connectTimeout=2000&socketTimeout= username: XXX password: XXX
自定义DataSourceConfig,而不用Springboot自启动的DataSourceConfig。 两个数据源就定义两个,多个数据源就定义多个 如果重点2没有区分各数据源的包扫描路径,会有运行时异常:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; / * 第一个数据源 * * @author xudong.shen * @date 2022/04/16 */ @Configuration @MapperScan(basePackages = "com.swapping.dao.mapper.mysql", sqlSessionTemplateRef = "sqlSessionTemplate")//重点1 指定包扫描,当前这个数据源对应的mapper.java文件放在哪个包下,这里路径就指定哪里 public class MyFirstDataSourceConfig { @Bean @Primary @ConfigurationProperties(prefix = "spring.datasource.my-first-mysql")//重点3 这里对应yml的当前数据源的前缀 public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean @Primary public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/mysql/*.xml"));//重点2 指定包扫描,当前这个数据源对应的mapper.xml文件在哪个resource下的包里,这里路径就指定哪里 // bean.setPlugins(new Interceptor[] {xxx}); // 设置MyBatis插件 return bean.getObject(); } @Bean @Primary public DataSourceTransactionManager testTransactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean @Primary public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; / * 第二个数据源 * * @author xudong.shen * @date 2022/04/18 */ @Configuration @MapperScan(basePackages = "com.swapping.dao.mapper.tidb", sqlSessionTemplateRef = "sqlSessionTemplateSecond")//重点1 指定包扫描,当前这个数据源对应的mapper.java文件放在哪个包下,这里路径就指定哪里 public class MySecondDataSourceConfig { @Bean(name = "dataSourceSecond") @ConfigurationProperties(prefix = "spring.datasource.my-sencond-tidb")//重点3 这里对应yml的当前数据源的前缀 public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactorySecond") public SqlSessionFactory testSqlSessionFactory(@Qualifier("dataSourceSecond") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/tidb/*.xml"));//重点2 指定包扫描,当前这个数据源对应的mapper.xml文件在哪个resource下的包里,这里路径就指定哪里 // bean.setPlugins(new Interceptor[] {xxx}); // 设置mybatis插件 return bean.getObject(); } @Bean(name = "transactionManagerSecond") public DataSourceTransactionManager testTransactionManager(@Qualifier("dataSourceSecond") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplateSecond") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("sqlSessionFactorySecond") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
到这里,多数据源的配置层面东西,就全部完成了。 后面的mapper.java mapper.xml 就放在上面各自数据源的指定包路径下,即可。 后面Service调用Mapper的动作,就和单数据源 一样了。正常调用就行了。(当然,跨数据源了,事务肯定就行不通了!)
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/rfx/23887.html