SpringBoot整合多数据源实现
Posted 波涛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合多数据源实现相关的知识,希望对你有一定的参考价值。
项目架构
1.导入相关依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-test</artifactId> 9 <scope>test</scope> 10 </dependency> 11 12 <!--freemarker支持--> 13 <dependency> 14 <groupId>org.springframework.boot</groupId> 15 <artifactId>spring-boot-starter-freemarker</artifactId> 16 </dependency> 17 18 <!-- mysql驱动 --> 19 <dependency> 20 <groupId>mysql</groupId> 21 <artifactId>mysql-connector-java</artifactId> 22 </dependency> 23 24 <!--整合mybatis--> 25 <dependency> 26 <groupId>org.mybatis.spring.boot</groupId> 27 <artifactId>mybatis-spring-boot-starter</artifactId> 28 <version>1.1.1</version> 29 </dependency>
2.application.properties
1 ## test1 数据源配置 2 #spring.datasource.test1.driverClassName=com.mysql.jdbc.Driver 3 #spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8 4 #spring.datasource.test1.username=root 5 #spring.datasource.test1.password=123 6 7 8 ## test2 数据源配置 9 #spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver 10 #spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8 11 #spring.datasource.test2.username=root 12 #spring.datasource.test2.password=123
3.创建datasource包 下TestMyBatisConfig1
1 @Configuration 2 @MapperScan(basePackages = "cn.happy.test1", sqlSessionFactoryRef = "test1SqlSessionFactory") 3 public class DataSource1Config { 4 @Bean(name = "test1DataSource") 5 @ConfigurationProperties(prefix = "spring.datasource.test1") 6 @Primary 7 public DataSource testDataSource() { 8 return DataSourceBuilder.create().build(); 9 } 10 11 @Bean(name = "test1SqlSessionFactory") 12 @Primary 13 public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { 14 SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 15 bean.setDataSource(dataSource); 16 //读取mybatis小配置文件 17 // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml")); 18 return bean.getObject(); 19 } 20 21 @Bean(name = "test1TransactionManager") 22 @Primary 23 public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) { 24 return new DataSourceTransactionManager(dataSource); 25 } 26 27 @Bean(name = "test1SqlSessionTemplate") 28 @Primary 29 public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 30 return new SqlSessionTemplate(sqlSessionFactory); 31 } 32 }
@Primary注解标识默认使用的数据源
如果不加启动会报如下错误:意思是有两个数据源 不知需要使用哪个数据源
3.创建datasource包 下TestMyBatisConfig2
1 @Configuration 2 @MapperScan(basePackages = "cn.happy.test2", sqlSessionFactoryRef = "test2SqlSessionFactory") 3 public class DataSource2Config { 4 @Bean(name = "test2DataSource") 5 @ConfigurationProperties(prefix = "spring.datasource.test2") 6 public DataSource testDataSource() { 7 return DataSourceBuilder.create().build(); 8 } 9 10 @Bean(name = "test2SqlSessionFactory") 11 public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception { 12 SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 13 bean.setDataSource(dataSource); 14 //读取mybatis小配置文件 15 // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml")); 16 return bean.getObject(); 17 } 18 19 @Bean(name = "test2TransactionManager") 20 public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) { 21 return new DataSourceTransactionManager(dataSource); 22 } 23 24 @Bean(name = "test2SqlSessionTemplate") 25 public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 26 return new SqlSessionTemplate(sqlSessionFactory); 27 } 28 29 }
4.entity层
1 /** 2 * author: 刘涛 3 * 4 * @create 2018-04-04 11:18 5 */ 6 public class Book { 7 8 private Integer bookid; 9 10 private String bookname; 11 12 private Integer bookprice; 13 14 get set省略。。 15 }
5.test1下的dao中BookMapperTest1
1 public interface BookMapperTest1 { 2 3 @Select("select * from book where bookname=#{bookname}") 4 public Book findByName(@Param("bookname") String bookname); 5 6 @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})") 7 public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice); 8 9 @Select("select * from book") 10 public Book findBook(@Param("Book") Book book); 11 }
6.test2下的dao中BookMapperTest2
1 public interface BookMapperTest2 { 2 3 @Select("select * from book where bookname=#{bookname}") 4 public Book findByName(@Param("bookname") String bookname); 5 6 @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})") 7 public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice); 8 }
7.controller层
@RestController public class BookController { @Autowired private BookMapperTest1 bookMapperTest1; @Autowired private BookMapperTest2 bookMapperTest2; @RequestMapping("insert001") public String insert001(String bookname,Double bookprice){ bookMapperTest1.insertBook(bookname,bookprice); return "success"; } @RequestMapping("insert002") public String insert002(String bookname,Double bookprice){ bookMapperTest2.insertBook(bookname,bookprice); return "success"; } }
8.启动项目
test1数据库
test2数据库
以上是关于SpringBoot整合多数据源实现的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot整合ElasticSearch实现多版本的兼容
SpringBoot-技术专区-Mybatis plus 实现多数据源整合
Springboot定时任务quartz整合(多数据源+quartz持久化到数据库)
全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段