亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

JdbcTemplate 多數(shù)據(jù)源是比較簡(jiǎn)單的,因?yàn)橐粋€(gè) JdbcTemplate 對(duì)應(yīng)一個(gè) DataSource,開發(fā)者只需要手動(dòng)提供多個(gè) DataSource ,再手動(dòng)配置 JdbcTemplate 即可。

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

創(chuàng)建兩個(gè)數(shù)據(jù)庫(kù):chapter05-1 和 chapter05-2.兩個(gè)庫(kù)中都創(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, '三國(guó)演義', '羅貫中');

2.創(chuàng)建項(xiàng)目

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

<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ù)庫(kù)連接池依賴是 druid-spring-boot-starter 。druid-spring-boot-starter 可以幫助開發(fā)者在 Spring Boot 項(xiàng)目中輕松集成 Druid 數(shù)據(jù)庫(kù)連接池和監(jiān)控。

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

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

# 數(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 中的配置生成兩個(gè)數(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 中提供了兩個(gè)數(shù)據(jù)源:dsOne 和 dsTwo,默認(rèn)方法名即實(shí)例名
  • @ConfigurationProperties 注解表示使用不同前綴的配置文件來(lái)創(chuàng)建不同的 DataSource 實(shí)例

5. 配置 JdbcTemplate

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

@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 中提供兩個(gè) JdbcTemplate 實(shí)例。每個(gè) JdbcTemplate 實(shí)例都需要提供 DataSource,由于Spring 容器中有兩個(gè) DataSource 實(shí)例,因此需要通過方法名查找。@Qualifier 注解表示查找不同名稱的 DataSource 實(shí)例注入進(jìn)來(lái)

6. 創(chuàng)建BookController

創(chuàng)建實(shí)體類 Book 和 BookController 進(jìn)行測(cè)試

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

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

7. 測(cè)試

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

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

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

相關(guān)文章

最新評(píng)論