欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Boot整合持久層之JdbcTemplate多數(shù)據(jù)源

 更新時間:2022年08月13日 10:57:16   作者:一只小熊貓呀  
持久層是JavaEE中訪問數(shù)據(jù)庫的核心操作,SpringBoot中對常見的持久層框架都提供了自動化配置,例如JdbcTemplate、JPA 等,MyBatis 的自動化配置則是MyBatis官方提供的。接下來分別向讀者介紹Spring Boot整合這持久層技術(shù)中的整合JdbcTemplate

多數(shù)據(jù)源

所謂多數(shù)據(jù)源,就是一個 Java EE 項目中采用了不同數(shù)據(jù)庫實例中的多個庫,或者同一個數(shù)據(jù)庫實例中多個不同的庫。一般來說,采用 MyCat 等分布式數(shù)據(jù)庫中間件是比較好的解決方案,這樣可以把數(shù)據(jù)庫讀寫分離、分庫分表、備份等操作交給中間件去做,Java 代碼只需要專注于業(yè)務(wù)即可。不過這并不意味著無法使用 Java 代碼解決類似的問題,在 Spring Framework 中就可以配置多數(shù)據(jù)源,Spring Boot 繼承其衣缽,只不過配置方式有所變化。

JdbcTemplate 多數(shù)據(jù)源

JdbcTemplate 多數(shù)據(jù)源是比較簡單的,因為一個 JdbcTemplate 對應(yīng)一個 DataSource,開發(fā)者只需要手動提供多個 DataSource ,再手動配置 JdbcTemplate 即可。

1. 創(chuàng)建數(shù)據(jù)庫

創(chuàng)建兩個數(shù)據(jù)庫:chapter05-1 和 chapter05-2.兩個庫中都創(chuàng)建 book 表,再各預(yù)設(shè) 1 條數(shù)據(jù),腳本如下

create database `chapter05-1` default character set utf8;
CREATE TABLE `chapter05-1`.`book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `author` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `chapter05-1`.`book`(`id`, `name`, `author`) VALUES (1, '水滸傳', '施耐庵');
create database `chapter05-2` default character set utf8;
CREATE TABLE `chapter05-2`.`book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `author` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `chapter05-2`.`book`(`id`, `name`, `author`) VALUES (1, '三國演義', '羅貫中');

2.創(chuàng)建項目

創(chuàng)建 Spring Boot Web 項目,添加如下依賴:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid-spring-boot-starter</artifactId>
  <version>1.1.10</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>

注意這里添加的數(shù)據(jù)庫連接池依賴是 druid-spring-boot-starter 。druid-spring-boot-starter 可以幫助開發(fā)者在 Spring Boot 項目中輕松集成 Druid 數(shù)據(jù)庫連接池和監(jiān)控。

3. 配置數(shù)據(jù)庫連接

在application.properties 中配置數(shù)據(jù)庫連接信息

# 數(shù)據(jù)源1
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.url=jdbc:mysql://localhost:3306/chapter05-1?useUnicode=true&characterEncoding=utf8&useSSL=true
spring.datasource.one.username=root
spring.datasource.one.password=root
# 數(shù)據(jù)源2
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.url=jdbc:mysql://localhost:3306/chapter05-2?useUnicode=true&characterEncoding=utf8&useSSL=true
spring.datasource.two.username=root
spring.datasource.two.password=root

4. 配置數(shù)據(jù)源

創(chuàng)建 DataSourceConfig 配置數(shù)據(jù)源,根據(jù) application.properties 中的配置生成兩個數(shù)據(jù)源

@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();
    }
}

代碼解釋:

  • DataSourceConfig 中提供了兩個數(shù)據(jù)源:dsOne 和 dsTwo,默認方法名即實例名
  • @ConfigurationProperties 注解表示使用不同前綴的配置文件來創(chuàng)建不同的 DataSource 實例

5. 配置 JdbcTemplate

在 5.1節(jié) 中得知,只要引入了 spring-jdbc 依賴,開發(fā)者沒有提供 JdbcTemplate 實例時,Spring Boot 默認會提供一個 JdbcTemplate 實例?,F(xiàn)在配置多數(shù)據(jù)源時,由開發(fā)者自己提供 JdbcTemplate 實例

@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 實例注入進來

6. 創(chuàng)建BookController

創(chuàng)建實體類 Book 和 BookController 進行測試

public class Book {
    private Integer id;
    private String name;
    private String author;
    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}
@RestController
public class BookController {
    @Resource(name = "jdbcTemplateOne")
    JdbcTemplate jdbcTemplate;
    @Autowired
    @Qualifier("jdbcTemplateTwo")
    JdbcTemplate jdbcTemplateTwo;
    @GetMapping("/test1")
    public void test1() {
        List<Book> books1 = jdbcTemplate.query("select * from book",
                new BeanPropertyRowMapper<>(Book.class));
        List<Book> books2 = jdbcTemplateTwo.query("select * from book",
                new BeanPropertyRowMapper<>(Book.class));
        System.out.println("books1:"+books1);
        System.out.println("books2:"+books2);
    }
}

簡單起見,這里沒有添加 service 層,而是直接將 JdbcTemplate 注入到了 Controller 中。在Controller 中注入兩個不同的 JdbcTemplate 有兩種方式:一種是使用 @Resource 注解,并指明 name 屬性,即按 name 進行裝配,此時會根據(jù)實例名查找相應(yīng)的實例注入;另一種是使用 @Autowired 注解結(jié)合 @Qualifier 注解,效果等同于使用 @Resource 注解。

7. 測試

http://localhost:8081/test1,查看打印日志

books1:[Book{id=1, name='水滸傳', author='施耐庵'}]
books2:[Book{id=1, name='三國演義', author='羅貫中'}]

到此這篇關(guān)于Spring Boot整合持久層之JdbcTemplate多數(shù)據(jù)源的文章就介紹到這了,更多相關(guān)Spring Boot JdbcTemplate 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論