- 浏览: 60334 次
文章分类
最新评论
13.从零开始学springboot-jdbc-多数据源
前言
上一节实现了springboot jpa多数据源案例,本节将实现springboot jdbc多数据源案例
创建项目
IDEA创建一个springboot空项目即可,过程略
添加依赖
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
添加配置
application.yml:
spring:
datasource:
master:
username: root
password: 123456
jdbc-url: jdbc:mysql://192.168.145.131:3306/test
# url: jdbc:mysql://192.168.145.131:3306/test
driver-class-name: com.mysql.cj.jdbc.Driver
slave:
username: root
password: 123456
jdbc-url: jdbc:mysql://192.168.145.131:3306/test2
# url: jdbc:mysql://192.168.145.131:3306/test2
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
建库
我们新增test/test2数据库 test新建表
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`age` int(11) NOT NULL,
`grade` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `student` VALUES ('1', '1', '1', '1');
test2新建表:
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL,
`age` varchar(255) DEFAULT NULL,
`course` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `teacher` VALUES ('1', '1', '1', '1');
完善
目录结构
config/DataSource:
package com.mrcoder.sbjdbcmultidb.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
//方案一
@Primary
@Bean(name = "masterDataSource")
@Qualifier("masterDataSource")
@ConfigurationProperties(prefix="spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "slaveDataSource")
@Qualifier("slaveDataSource")
@ConfigurationProperties(prefix="spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "masterJdbcTemplate")
@Qualifier("masterJdbcTemplate")
public JdbcTemplate masterJdbcTemplate(@Qualifier("masterDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "slaveJdbcTemplate")
@Qualifier("slaveJdbcTemplate")
public JdbcTemplate slaveJdbcTemplate(@Qualifier("slaveDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
//方案二(请将方案一注释,application.yml修改jdbc-url为url)
// //master库
// @Primary
// @Bean(name = "masterDataSourceProperties")
// @Qualifier("masterDataSourceProperties")
// @ConfigurationProperties(prefix = "spring.datasource.master")
// public DataSourceProperties masterDataSourceProperties() {
// return new DataSourceProperties();
// }
//
// @Primary
// @Bean(name = "masterDataSource")
// @Qualifier("masterDataSource")
// @ConfigurationProperties(prefix = "spring.datasource.master")
// public DataSource masterDataSource(@Qualifier("masterDataSourceProperties") DataSourceProperties dataSourceProperties) {
// return dataSourceProperties.initializeDataSourceBuilder().build();
// }
//
// //slave库
// @Bean(name = "slaveDataSourceProperties")
// @Qualifier("slaveDataSourceProperties")
// @ConfigurationProperties(prefix = "spring.datasource.slave")
// public DataSourceProperties slaveDataSourceProperties() {
// return new DataSourceProperties();
// }
//
// @Bean(name = "slaveDataSource")
// @Qualifier("slaveDataSource")
// @ConfigurationProperties(prefix = "spring.datasource.slave")
// public DataSource slaveDataSource(@Qualifier("slaveDataSourceProperties") DataSourceProperties dataSourceProperties) {
// return dataSourceProperties.initializeDataSourceBuilder().build();
// }
}
entity/Student:
package com.mrcoder.sbjdbcmultidb.entity;
public class Student {
private int id;
private String name;
private int age;
private int grade;
public Student() {
}
public Student(String name, int age, int grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", grade=" + grade +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
entity/Teacher:
package com.mrcoder.sbjdbcmultidb.entity;
public class Teacher {
private int id;
private String name;
private String age;
private String course;
public Teacher() {
}
public Teacher(String name, String age, String course) {
this.name = name;
this.age = age;
this.course = course;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", course='" + course + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
}
controller/JdbcMultidbController:
package com.mrcoder.sbjdbcmultidb.controller;
import com.mrcoder.sbjdbcmultidb.entity.Student;
import com.mrcoder.sbjdbcmultidb.entity.Teacher;
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.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class JdbcMultidbController {
//master
@Autowired
@Qualifier("masterJdbcTemplate")
protected JdbcTemplate masterTempleate;
//slave
@Autowired
@Qualifier("slaveJdbcTemplate")
protected JdbcTemplate slaveTempleate;
@RequestMapping(value = "/list")
public void list(){
String studentSql = "select * from student";
RowMapper<Student> studentRowMapper = new BeanPropertyRowMapper<>(Student.class);
List<Student> studentList = masterTempleate.query(studentSql, studentRowMapper);
String teacherSql = "select * from teacher";
RowMapper<Teacher> teacherRowMapper = new BeanPropertyRowMapper<>(Teacher.class);
List<Teacher> teacherList = slaveTempleate.query(teacherSql, teacherRowMapper);
System.out.println(studentList);
System.out.println(teacherList);
}
}
运行
项目地址
https://github.com/MrCoderStack/SpringBootDemo/tree/master/sb-jdbc-multidb
https://gitee.com/MrCoderStack/SpringBootDemo/tree/master/sb-jdbc-multidb
Tip
关于数据源配置文件中的注释部分请查看我的另一篇博文
请关注我的订阅号
转载于:https://my.oschina.net/u/3066875/blog/3055193
相关推荐
Java开发案例-springboot-33-整合sharding-jdbc和多数据源-源代码+文档.rar Java开发案例-springboot-33-整合sharding-jdbc和多数据源-源代码+文档.rar Java开发案例-springboot-33-整合sharding-jdbc和多数据源-源...
Java开发案例-springboot-32-整合sharding-jdbc-源代码+文档.rar Java开发案例-springboot-32-整合sharding-jdbc-源代码+文档.rar Java开发案例-springboot-32-整合sharding-jdbc-源代码+文档.rar Java开发案例-...
hgdb-6.0.0-jdbc42.jar 编程资料 觉得好的资料
赠送源代码:mssql-jdbc-8.2.0.jre8-sources.jar; 赠送Maven依赖信息文件:mssql-jdbc-8.2.0.jre8.pom; 包含翻译后的API文档:mssql-jdbc-8.2.0.jre8-javadoc-API文档-中文(简体)版.zip; Maven坐标:...
赠送源代码:sqlite-jdbc-3.34.0-sources.jar; 赠送Maven依赖信息文件:sqlite-jdbc-3.34.0.pom; 包含翻译后的API文档:sqlite-jdbc-3.34.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.xerial:sqlite-...
开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6....
mssql-jdbc-7.4.1.jre8.jar,来自微软官网。
赠送源代码:mssql-jdbc-6.2.1.jre7-sources.jar; 赠送Maven依赖信息文件:mssql-jdbc-6.2.1.jre7.pom; 包含翻译后的API文档:mssql-jdbc-6.2.1.jre7-javadoc-API文档-中文(简体)版.zip; Maven坐标:...
vertica-jdbc-9.2.1-0.jar) you should be able to run a command something like the following to add it to your local repository: mvn install:install-file -Dfile={/path_to/vertica-jdbc-9.2.1-0.jar} -...
含两个文件hive-jdbc-3.1.2-standalone.jar和apache-hive-3.1.2-bin.tar.gz 含两个文件hive-jdbc-3.1.2-standalone.jar和apache-hive-3.1.2-bin.tar.gz 含两个文件hive-jdbc-3.1.2-standalone.jar和apache-hive-...
JAVAWEB前端课程所需要的五个jar包 - commons-logging-1.2.jar,spring-beans-5.0.0.RELEASE.jar,spring-core-5.0.0.RELEASE.jar,spring-jdbc-5.0.0.RELEASE.jar,spring-tx-5.0.2.RELEASE.jar
├─SpringBoot-05-Data-jdbc.rar ├─SpringBoot-05-Data-jpa.rar ├─SpringBoot-05-Data-Mybatis.rar ├─SpringBoot-06-Runner.rar ├─SpringBoot-07-Starter.rar ├─SpringBoot-ThymeLeaf-Book.rar
finereport 连接clickhouse数据库 jar
sqlite-jdbc-3.20.1.jar 连接Java和sqlite的最新驱动!
hive-jdbc-uber-2.6.5.0-292.jar 驱动下载,hive-jdbc-uber-2.6.5.0-292.jar,hive-jdbc-uber-2.6.5.0-292.jar
Hive连接的jar包——hive-jdbc-3.1.2-standalone.jar,使用数据库连接软件连接数据仓库时需要使用相应的驱动器驱动,希望对大家有所帮助
赠送源代码:mssql-jdbc-8.2.0.jre8-sources.jar; 赠送Maven依赖信息文件:mssql-jdbc-8.2.0.jre8.pom; 包含翻译后的API文档:mssql-jdbc-8.2.0.jre8-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:...
hive-jdbc-uber-2.6.5.0-292.jar驱动
sqlite-jdbc-3.36.0.3.jar 最新吧2021 8月底更新
c3p0-0.9.1.2.jar、cas-server-support-jdbc-4.0.0.jar、mysql-connector-java-5.1.28-bin.jar