您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章给大家分享的是有关springboot 中怎么利用mybatis实现多数据源,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
项目有一个需求,需要从多数据源读取数据。因此使用springboot+mybatis进行多数据源配置和操作。
<!-- mybatis db --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency>
# ds1数据源配置 spring.datasource.ds1.url=jdbc:mysql://ip1:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 spring.datasource.ds1.username=xxxxxx spring.datasource.ds1.password=xxxxxx spring.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver # ds2数据源配置 spring.datasource.ds2.url=jdbc:mysql://ip2:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 spring.datasource.ds2.username=xxxxxx spring.datasource.ds2.password=xxxxxx spring.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver # ds3数据源配置 spring.datasource.ds3.url=jdbc:mysql://ip3:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 spring.datasource.ds3.username=xxxxxx spring.datasource.ds3.password=xxxxxx spring.datasource.ds3.driver-class-name=com.mysql.cj.jdbc.Driver
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; @Configuration // 扫描的包路径 @MapperScan(basePackages = "com.qishu.demo.ds2.dao", sqlSessionFactoryRef = "ds2SqlSessionFactory") public class Ds2DatasourceConfig { @Bean(name = "ds2DataSource") // 配置前缀 @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文件目录 sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:ds2-mapper/*.xml")); return sessionFactoryBean.getObject(); } }
ds3就不写了
使用方式
@Autowired private AppDAO appDAO;
// 排除自动注册配置 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
// EnableAutoConfiguration注解,关闭springBoot关于mybatis的一些自动注入。 @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)); } }
以上就是springboot 中怎么利用mybatis实现多数据源,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。