1. 程式人生 > >SpringBoot整合多數據源實現

SpringBoot整合多數據源實現

cati config img ng- happy prefix 相關 mark public

項目架構

技術分享圖片

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整合多數據源實現