1.引入pom依赖
mysql mysql-connector-javaruntime org.mybatis.spring.boot mybatis-spring-boot-starter2.1.3 com.alibaba druid-spring-boot-starter1.1.0 com.alibaba druid1.0.28
2.建立两套dao层及实体类文件以便多数据源隔离使用,具体层级如下
3.配置文件中配置多数据源配置信息
4.编写数据源配置类
主库配置类:
package com.example.demo.config; import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; 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; import java.sql.SQLException; @Configuration @MapperScan(basePackages = MasterDbConfig.PACKAGE , sqlSessionFactoryRef = "masterSqlSessionFactory") public class MasterDbConfig { private Logger logger = LoggerFactory.getLogger(MasterDbConfig.class); // 配置数据源文件位置 static final String PACKAGE = "com.example.demo.ds1mapper.**"; private static final String MAPPER_LOCATION = "classpath*:ds1mapper @Configuration @MapperScan(basePackages = ClusterDbConfig.PACKAGE , sqlSessionFactoryRef = "clusterSqlSessionFactory") public class ClusterDbConfig { private Logger logger = LoggerFactory.getLogger(ClusterDbConfig.class); // 精确到 cluster 目录,以便跟其他数据源隔离 static final String PACKAGE = "com.example.demo.ds2mapper.**"; private static final String MAPPER_LOCATION = "classpath*:ds2mapper/*.xml"; private static final String DOMAIN_PACKAGE = "com.example.demo.ds2entity.**"; @Value("${spring.datasource.url2}") private String dbUrl; @Value("${spring.datasource.username2}") private String username; @Value("${spring.datasource.password2}") private String password; @Value("${spring.datasource.driverClassName2}") private String driverClassName; @Bean(name="clusterDataSource") //声明其为Bean实例 public DataSource clusterDataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); return datasource; } @Bean(name = "clusterTransactionManager") public DataSourceTransactionManager clusterTransactionManager() { return new DataSourceTransactionManager(clusterDataSource()); } @Bean(name = "clusterSqlSessionFactory") public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource culsterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(culsterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(ClusterDbConfig.MAPPER_LOCATION)); sessionFactory.setTypeAliasesPackage(DOMAIN_PACKAGE); //mybatis 数据库字段与实体类属性驼峰映射配置 sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return sessionFactory.getObject(); } }
使用时想要调用哪个数据源的数据,@Autowired 哪个mapper即可
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)