目前業(yè)界操作數(shù)據(jù)庫的框架一般是 Mybatis,但在很多業(yè)務(wù)場景下,我們需要在一個(gè)工程里配置多個(gè)數(shù)據(jù)源來實(shí)現(xiàn)業(yè)務(wù)邏輯。在SpringBoot中也可以實(shí)現(xiàn)多數(shù)據(jù)源并配合Mybatis框架編寫xml文件來執(zhí)行SQL。在SpringBoot中,配置多數(shù)據(jù)源的方式十分便捷,
堅(jiān)守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都邊坡防護(hù)網(wǎng)小微創(chuàng)業(yè)公司專業(yè)提供成都企業(yè)網(wǎng)站定制營銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。
下面開始上代碼:
在pom.xml文件中需要添加一些依賴
<!-- Spring Boot Mybatis 依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!-- MySQL 連接驅(qū)動依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- Druid 數(shù)據(jù)連接池依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency>
application.properties 配置兩個(gè)數(shù)據(jù)源配置
# master 數(shù)據(jù)源配置 master.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8 master.datasource.username=root master.datasource.password=xieshuai6666 master.datasource.driverClassName=com.mysql.jdbc.Driver # second 數(shù)據(jù)源配置 second.datasource.url=jdbc:mysql://localhost:3306/springbootdb_second?useUnicode=true&characterEncoding=utf8 second.datasource.username=root second.datasource.password=xieshuai6666 second.datasource.driverClassName=com.mysql.jdbc.Driver
數(shù)據(jù)源配置
多數(shù)據(jù)源配置的時(shí)候注意,必須要有一個(gè)主數(shù)據(jù)源,即 MasterDataSourceConfig 配置
MasterDataSourceConfig的代碼:
@Configuration // 掃描 Mapper 接口并容器管理 @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") public class MasterDataSourceConfig { // 精確到 master 目錄,以便跟其他數(shù)據(jù)源隔離 static final String PACKAGE = "org.spring.springboot.dao.master"; static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml"; @Value("${master.datasource.url}") private String url; @Value("${master.datasource.username}") private String user; @Value("${master.datasource.password}") private String password; @Value("${master.datasource.driverClassName}") private String driverClass; @Bean(name = "masterDataSource") @Primary public DataSource masterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; } @Bean(name = "masterTransactionManager") @Primary public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(masterDataSource()); } @Bean(name = "masterSqlSessionFactory") @Primary public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(MasterDataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); } }
第二個(gè)數(shù)據(jù)源SecondDataSourceConfig
的配置如下:
@Configuration // 掃描 Mapper 接口并容器管理 @MapperScan(basePackages = ClusterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "clusterSqlSessionFactory") public class ClusterDataSourceConfig { // 精確到 cluster 目錄,以便跟其他數(shù)據(jù)源隔離 static final String PACKAGE = "org.spring.springboot.dao.cluster"; static final String MAPPER_LOCATION = "classpath:mapper/cluster/*.xml"; @Value("${cluster.datasource.url}") private String url; @Value("${cluster.datasource.username}") private String user; @Value("${cluster.datasource.password}") private String password; @Value("${cluster.datasource.driverClassName}") private String driverClass; @Bean(name = "clusterDataSource") public DataSource clusterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; } @Bean(name = "clusterTransactionManager") public DataSourceTransactionManager clusterTransactionManager() { return new DataSourceTransactionManager(clusterDataSource()); } @Bean(name = "clusterSqlSessionFactory") public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource clusterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(clusterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(ClusterDataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); } }
到此,不同的數(shù)據(jù)源配置就已經(jīng)完成,剩下的只需要將將Mybatis的xml文件和DAO層的接口寫好,并在Service層注入,直接使用就行。
Service層的代碼:
@Service public class UserAndCityServiceImpl implements UserAndCityService{ @Autowired private UserDao userDao; @Autowired private CityDao cityDao; @Override public UserVo getUser(String userName) { UserVo userVo = userDao.selectByUserName(userName); CityVo cityVo = cityDao.selectByCityName("北京市"); userVo.setDescription(cityVo.getCityName()); return userVo; } }
Mybatis的xml文件UserDao.xml和CityDao.xml的內(nèi)容,就不一一展示了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前名稱:詳解SpringBoot和Mybatis配置多數(shù)據(jù)源
網(wǎng)頁網(wǎng)址:http://redsoil1982.com.cn/article22/gdicjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、電子商務(wù)、云服務(wù)器、ChatGPT、定制開發(fā)、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)