环境
maven依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.microservice</groupId>
<artifactId>dbandcache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dbandcache</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--数据库操作-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--数据库连接-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
多数据源配置类
@Configuration
@MapperScan(basePackages = "com.microservice.dbandcache.mapper")
public class MybatisConfig
{
@Autowired
private Environment environment;
/**
* 单一数据源
* @return
* @throws Exception
*/
// @Bean
// public DataSource dataSource () throws Exception{
// Properties properties = new Properties();
// properties.put("driverClassName",environment.getProperty("dbandcache1.jdbc.driverClassName"));
// properties.put("url",environment.getProperty("dbandcache1.jdbc.url"));
// properties.put("username",environment.getProperty("dbandcache1.jdbc.username"));
// properties.put("password",environment.getProperty("dbandcache1.jdbc.passwrd"));
//
// return DruidDataSourceFactory.createDataSource(properties);
// }
/********多数据源开始********/
@Bean
public DataSource micorservicedb1 () throws Exception{
Properties props = new Properties();
props.put("driverClassName",environment.getProperty("dbandcache1.jdbc.driverClassName"));
props.put("url",environment.getProperty("dbandcache1.jdbc.url"));
props.put("username",environment.getProperty("dbandcache1.jdbc.username"));
props.put("password",environment.getProperty("dbandcache1.jdbc.passwrd"));
return DruidDataSourceFactory.createDataSource(props);
}
@Bean
public DataSource microservicedb2 () throws Exception{
Properties properties = new Properties();
properties.put("driverClassName",environment.getProperty("dbandcache2.jdbc.driverClassName"));
properties.put("url",environment.getProperty("dbandcache2.jdbc.url"));
properties.put("username",environment.getProperty("dbandcache2.jdbc.username"));
properties.put("password",environment.getProperty("dbandcache2.jdbc.passwrd"));
return DruidDataSourceFactory.createDataSource(properties);
}
//@Qualifier注解用来区别同类的bean
@Bean
@Primary
public DynamicDataSource dataSource(@Qualifier("micorservicedb1") DataSource micorservicedb1,@Qualifier("microservicedb2") DataSource microservicedb2){
HashMap<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataBaseType.micorservicedb1,micorservicedb1);
targetDataSources.put(DataBaseType.microservicedb2,microservicedb2);
DynamicDataSource dynamicDataSource = new DynamicDataSource();
//AbstractRoutingDataSource#setTargetDataSources 设置目标数据源s
dynamicDataSource.setTargetDataSources(targetDataSources);
//设置默认的数据源
dynamicDataSource.setDefaultTargetDataSource(micorservicedb1);
return dynamicDataSource;
}
/**********************动态数据源结束****************************/
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
{
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//设置数据源
sqlSessionFactoryBean.setDataSource(dataSource);
//指定基础包
sqlSessionFactoryBean.setTypeAliasesPackage("com.microservice.dbandcache.model");
//指定mapper位置
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapping/*.xml"));
return sqlSessionFactoryBean.getObject();
}
}
启动后出现依赖循环异常,许多博客的解决办法是:在application.propertites文件中添加
sprign.datasource.initialize=false
或者升级springboot的
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
看了看都不能解决我的问题,经查找发现了解决办法:
@Import(MybatisConfig.class)
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
//@MapperScan(basePackages = "com.microservice.dbandcache.mapper")
public class DbandcacheApplication
{
public static void main(String[] args)
{
SpringApplication.run(DbandcacheApplication.class, args);
}
}
题外话
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)中的exclude解释:
SpringBootApplication注解的源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
//排除某些类
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface AliasFor {
@AliasFor("attribute")
String value() default "";
@AliasFor("value")
String attribute() default "";
Class<? extends Annotation> annotation() default Annotation.class;
}