04_Spring Boot 多数据源配置:JdbcTemplate

1、pom.xml配置

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.linjb

springboot-multi-source-jdbc-template

war

0.0.1-SNAPSHOT

springboot-multi-source-jdbc-template Maven Webapp

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.3.3.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-jdbc

io.springfox

springfox-swagger2

2.2.2

io.springfox

springfox-swagger-ui

2.2.2

mysql

mysql-connector-java

springboot-multi-source-jdbc-template

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

UTF-8

 

2、application.properties 配置

## 数据源配置

spring.datasource.primary.url=jdbc:mysql://127.0.0.1:3306/test_base_1?useUnicode=true&characterEncoding=utf-8

spring.datasource.primary.username=root

spring.datasource.primary.password=root

spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver

 

spring.datasource.secondary.url=jdbc:mysql://127.0.0.1:3306/test_base_1?useUnicode=true&characterEncoding=utf-8

spring.datasource.secondary.username=root

spring.datasource.secondary.password=root

spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver

3、数据源配置类:

1)、DataSourceConfig

package com.linjb.config;

 

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import org.springframework.jdbc.core.JdbcTemplate;

 

@Configuration

public class DataSourceConfig {

 

@Bean(name = "primaryDataSource")

@Qualifier("primaryDataSource")

@ConfigurationProperties(prefix="spring.datasource.primary")

public DataSource primaryDataSource() {

return DataSourceBuilder.create().build();

}

 

@Bean(name = "secondaryDataSource")

@Qualifier("secondaryDataSource")

@Primary

@ConfigurationProperties(prefix="spring.datasource.secondary")

public DataSource secondaryDataSource() {

return DataSourceBuilder.create().build();

}

@Bean(name = "primaryJdbcTemplate")

public JdbcTemplate primaryJdbcTemplate(

@Qualifier("primaryDataSource") DataSource dataSource) {

return new JdbcTemplate(dataSource);

}

 

@Bean(name = "secondaryJdbcTemplate")

public JdbcTemplate secondaryJdbcTemplate(

@Qualifier("secondaryDataSource") DataSource dataSource) {

return new JdbcTemplate(dataSource);

}

 

}

定义两个DataSource用来读取application.properties中的不同配置。如上例子中,主数据源配置为spring.datasource.primary开头的配置,第二数据源配置为spring.datasource.secondary开头的配置。

对JdbcTemplate的支持比较简单,只需要为其注入对应的datasource即可,如下例子,在创建JdbcTemplate的时候分别注入名为primaryDataSource和secondaryDataSource的数据源来区分不同的JdbcTemplate。

 

3、Model层

package com.linjb.model;

 

public class User {

 

private Integer id;

private String username;

private Integer age;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

}

4、Controller层

package com.linjb.controller;

 

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.List;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

 

import com.linjb.model.User;

 

@RestController

@RequestMapping(value = "/user")

public class UserController {

 

@Autowired

@Qualifier("primaryJdbcTemplate")

protected JdbcTemplate jdbcTemplate1;

 

@Autowired

@Qualifier("secondaryJdbcTemplate")

protected JdbcTemplate jdbcTemplate2;

@RequestMapping(value="/insertUser", method=RequestMethod.POST)

public String insertUser2(){

// 往第一个数据源中插入两条数据

jdbcTemplate1.update("insert into user(id,username,age) values(?, ?, ?)", 5, "aaa", 20);

jdbcTemplate2.update("insert into user(id,username,age) values(?, ?, ?)", 5, "aaa", 20);

return "success";

}

@RequestMapping(value="/findAllUser1", method=RequestMethod.POST)

public List findAllUser1(){

return jdbcTemplate1.query("select id,username,age from user ", new UserInfo());

}

public class UserInfo implements RowMapper{

@Override

public User mapRow(ResultSet rs, int rowNum) throws SQLException {

User userInfo = new User();

userInfo.setId(rs.getInt("id"));

userInfo.setUsername(rs.getString("username"));

userInfo.setAge(rs.getInt("age"));

return userInfo;

}

}

}

 

6、Application启动类

package com.linjb;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

/**

* Spring Boot应用启动类

* @Copyright Copyright (C) 2019 linjb

* @author linjb

* @version 1.0

* @CreateDate 2019年1月31日下午10:25:16

*/

@SpringBootApplication

public class Application /*extends SpringBootServletInitializer*/{

 

public static void main(String[] args) {

SpringApplication.run(Application.class,args);

}

// @Override

// protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

//

// return builder.sources(Application.class);

// }

}

 

你可能感兴趣的:(搬砖之,SpringBoot,专栏)