阿咚的多数据实现是使用配置类,以及yml文件的方式,ORM使用mybatis 话不多说直接上代码。
数据源1配置类
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;
数据源1
@Configuration
@MapperScan(basePackages = "com.**.mapper.aDong1",sqlSessionFactoryRef = "aDong1SqlSessionFactory")
public class aDong1DataSourceConfig {
@Primary //默认数据源
@Bean("DaibaiDataSource")
@ConfigurationProperties(prefix = "spring.datasource.dynamic.aDong1") //yml配置文件
public DataSource getaDong1DataSource(){
return DataSourceBuilder.*create*().build();
}
@Primary
@Bean("aDong1SqlSessionFactory")
public SqlSessionFactory aDong1SqlSessionFactory(@Qualifier("DaibaiDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/mapper/aDong1*.xml"));
return bean.getObject();
}
@Primary
@Bean("aDong1SqlSessionTemplate")
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("aDong1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
数据源2
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;
//数据源2
@Configuration
@MapperScan(basePackages = "com.*.mapper.sso",sqlSessionFactoryRef = "ssoSqlSessionFactory")
public class aDong2SourceConfig {
@Primary //默认数据源
@Bean("aDong2Source")
@ConfigurationProperties(prefix = "spring.datasource.dynamic.sso") //yml配置文件
public DataSource getaDong1DataSource(){
return DataSourceBuilder.*create*().build();
}
@Primary
@Bean("aDong2SqlSessionFactory")
public SqlSessionFactory aDong2SqlSessionFactory(@Qualifier("aDong2Source") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/mapper/aDong2*.xml"));
return bean.getObject();
}
@Bean("aDong2SqlSessionTemplate")
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("aDong2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
yml文件配置: 根据自己情况填上 信息
datasource:
dynamic:
primary: dabai *#**设置默认数据源
* dabai:
jdbc-url:
driver-class-name: com.mysql.cj.jdbc.Driver
username:
password:
sso:
jdbc-url:
driver-class-name: com.mysql.cj.jdbc.Driver
username:
password:
使用的时候跟单数据源一样,只要直接使用对应表的mapper就直接可以。



