大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇文章给大家分享的是有关springboot 中怎么利用mybatis实现多数据源,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
为承德等地区用户提供了全套网页设计制作服务,及承德网站建设行业解决方案。主营业务为成都做网站、成都网站建设、承德网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
项目有一个需求,需要从多数据源读取数据。因此使用springboot+mybatis进行多数据源配置和操作。
org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 MySQL mysql-connector-java 8.0.13
# 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实现多数据源,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。