In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use mybatis to achieve multiple data sources in springboot, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Demand
The project has a requirement to read data from multiple data sources. Therefore, springboot+mybatis is used for multi-data source configuration and operation.
Rely on the introduction of org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java 8.0.13 configuration file # ds1 data source configuration spring.datasource.ds1.url=jdbc:mysql://ip1:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false& ServerTimezone=GMT%2B8spring.datasource.ds1.username=xxxxxxspring.datasource.ds1.password=xxxxxxspring.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver# ds2 data Source configuration spring.datasource.ds2.url=jdbc:mysql://ip2:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8spring.datasource.ds2.username=xxxxxxspring.datasource.ds2.password=xxxxxxspring.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver# ds3 data Source configuration spring.datasource.ds3.url=jdbc:mysql://ip3:port/ Database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8spring.datasource.ds3.username=xxxxxxspring.datasource.ds3.password=xxxxxxspring.datasource.ds3.driver-class-name=com.mysql.cj.jdbc.DriverJava configuration package com.qishu.demo.config Import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;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.context.annotation.Primary;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource @ Configuration@MapperScan (basePackages = "com.qishu.demo.ds1.dao", sqlSessionFactoryRef = "ds1SqlSessionFactory") public class Ds1DatasourceConfig {@ Primary @ Bean (name = "ds1DataSource") @ ConfigurationProperties ("spring.datasource.ds1") public DataSource masterDataSource () {return DataSourceBuilder.create (). Build ();} @ Primary @ Bean (name = "ds1SqlSessionFactory") public SqlSessionFactory sqlSessionFactory (@ Qualifier ("ds1DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean () SessionFactoryBean.setDataSource (dataSource); sessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath:ds1-mapper/*.xml")); return sessionFactoryBean.getObject ();}} package com.qishu.demo.config;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;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.context.annotation.Primary;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource @ MapperScan (basePackages = "com.qishu.demo.ds2.dao", sqlSessionFactoryRef = "ds2SqlSessionFactory") public class Ds2DatasourceConfig {@ Bean (name = "ds2DataSource") / / configure the prefix @ ConfigurationProperties ("spring.datasource.ds2") public DataSource masterDataSource () {return DataSourceBuilder.create () .build () } @ Bean (name = "ds2SqlSessionFactory") public SqlSessionFactory sqlSessionFactory (@ Qualifier ("ds2DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean (); sessionFactoryBean.setDataSource (dataSource); / / mapper file directory sessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath:ds2-mapper/*.xml")); return sessionFactoryBean.getObject ();}}
Ds3 will not write.
Mode of use
@ Autowired private AppDAO appDAO; modifies the startup class / / excludes the auto-registration configuration @ SpringBootApplication (exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class}) unit test / / EnableAutoConfiguration comment, and turns off some of springBoot's automatic mybatis injection. @ RunWith (SpringRunner.class) @ SpringBootTest (classes = Application.class) @ EnableAutoConfiguration (exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class}) public class AppDAOTest {@ Autowired private AppDAO appDAO; @ Test public void selectTest () {AppDO appDO = appDAO.selectByPrimaryKey (70); System.out.println (JSON.toJSONString (appDO));}} directory structure
The above is how to use mybatis to achieve multiple data sources in springboot. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.