JdbcTemplate多資料來源詳解
阿新 • • 發佈:2020-12-11
建立資料庫:
建立兩個資料庫用來測試,分別為database1和database2。在兩個資料庫中都建立一張book表。
新增依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency>
- druid-spring-boot-starter:資料庫連線池依賴,幫助開發者在spring boot專案中輕鬆整合Druid資料庫連線池和監控
配置資料庫連線:
#資料來源1 spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.one.username=root spring.datasource.one.password=root spring.datasource.one.url=jdbc:mysql://localhost:3306/chapter05-1?characterEncoding=utf8&serverTimezone=UTC #資料來源2 spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.two.username=root spring.datasource.two.password=root spring.datasource.two.url=jdbc:mysql://localhost:3306/chapter05-2?characterEncoding=utf8&serverTimezone=UTC
- 配置兩個資料來源,資料庫不同,其他相同
配置資料來源:
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.one") DataSource dsOne() { return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties("spring.datasource.two") DataSource dsTwo() { return DruidDataSourceBuilder.create().build(); } }
程式碼解釋:
- @Configuration:@Configuration註解的使用
- @ConfigurationProperties:將配置檔案(application.yaml)的每個屬性值,對映到這個元件中。告訴Springboot將本類中的所有屬性和配置檔案中的配置進行繫結。此處表示使用不同字首的配置檔案來建立不同的DataSource例項
- @Bean:此註解用於告訴方法,產生一個Bean物件,然後這個Bean物件交給Spring管理。產生這個Bean物件的方法會呼叫一次,隨後這個Spring將會將這個Bean物件放在自己的IOC容器中。SpringIOC容器管理一個或多個bean,這些bean都需要在@Configuration註解下進行建立,在一個方法上使用@Bean註解就表明這個方法需要交給Spring進行管理
實體類:
import lombok.Data;
@Data
public class Book {
private Integer id;
private String name;
private String author;
}
配置JdbcTemplate:
在配置多資料來源時,需要自己提供JdbcTemplate例項:
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class JdbcTemplateConfig {
@Bean
JdbcTemplate jdbcTemplateOne(@Qualifier("dsOne") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean
JdbcTemplate jdbcTemplateTwo(@Qualifier("dsTwo") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
程式碼解釋:
- JdbcTemplateConfig中提供兩個JdbcTemplate例項。每個JdbcTemplate例項都需要提供DataSource,由於Spring容器中有兩個DataSource例項,因此需要通過方法名查詢。
- @Qualifier註解表示查詢不同名稱的DataSource例項注入進來
Controller層:
import com.example.demo.domain.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class BookController {
@Resource(name = "jdbcTemplateOne")
JdbcTemplate jdbcTemplateOne;
@Autowired
@Qualifier("jdbcTemplateTwo")
JdbcTemplate jdbcTemplateTwo;
@GetMapping("/test1")
public void test1(){
List<Book> book1 = jdbcTemplateOne.query("select * from book",new BeanPropertyRowMapper<>(Book.class));
List<Book> book2 = jdbcTemplateTwo.query("select * from book",new BeanPropertyRowMapper<>(Book.class));
System.out.println("book1:"+book1);
System.out.println("book2:"+book2);
}
}
在Controller中注入兩個不同的JdbcTemplate有兩種方式:一是使用@Resource註解,並指明name屬性,即按name進行裝配,此時會根據例項名查詢相應的例項注入;另一種時使用@Autowored註解結合@Qualifiler註解,效果等同於使用@Resource註解。
測試:
啟動系統,訪問地址:
http://127.0.0.1:8080/test1
控制檯輸出:
book1:[Book(id=1, name=水滸傳, author=施耐庵)]
book2:[Book(id=1, name=三國演義, author=羅貫中)]