转载地址:https://blog.youkuaiyun.com/tuesdayma/article/details/81081666
gitHub:https://github.com/wait-love/learngit.git
如果是主从复制-那么就是应用到分包的形式,也就是下面的这一种,主数据库负责数据的增删改(master),从数据库负责数据的查询
下面就是pom文件的导入:
<!--web应用的依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot 模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- mysql驱动的jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springboot整合mybaites依赖的jar包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!--lombok依赖包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
第二步就是配置好连接:
server.port=8090
#记住其中的要加上jdbc-url负责是会报错的
#mybaties -learn dataSource
spring.datasource.learn.username=root
spring.datasource.learn.password=123456
spring.datasource.learn.driverClassName=com.mysql.jdbc.Driver
spring.datasource.learn.jdbc-url=jdbc:mysql://localhost:3306/learn?serverTimezone=UTC
#mybaties -loex dataSource
spring.datasource.loex.username=root
spring.datasource.loex.password=123456
spring.datasource.loex.driverClassName=com.mysql.jdbc.Driver
spring.datasource.loex.jdbc-url=jdbc:mysql://localhost:3306/loex?serverTimezone=UTC
第三就是编写配置类:
package com.example.jason.source.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
* @Author: Jason
* @Create: 2020/2/25 10:10
* @Description learn数据源的配置文件类
*/
@Configuration
@MapperScan(basePackages = "com.example.jason.source.mapper.learn", sqlSessionFactoryRef = "learnSqlSessionFactory")
public class DataSourceConfigLearn{
@Bean(name = "learnDataSource") //将对象注入到ioc容器中
@ConfigurationProperties(prefix = "spring.datasource.learn")
public DataSource getDataSource1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "learnSqlSessionFactory")
public SqlSessionFactory loexSqlSessionFactory(@Qualifier("learnDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/learn/*.xml"));
return bean.getObject();
}
@Bean("learnSqlSessionTemplate")
public SqlSessionTemplate test1sqlsessiontemplate(@Qualifier("learnSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第二个数据源的配置类:
package com.example.jason.source.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
* @Author: Jason
* @Create: 2020/2/25 9:48
* @Description 数据源的配置文件类
* 表示第一个数据源的配置文件类
* MapperScan是指代mapper中的loex包接口的类路径
*/
@Configuration
@MapperScan(basePackages = "com.example.jason.source.mapper.loex", sqlSessionFactoryRef = "loexSqlSessionFactory")
public class DataSourceConfigLoex {
@Bean(name = "loexDataSource") //将对象注入到ioc容器中
@Primary //表示这是一个基本的数据源-默认数据源
@ConfigurationProperties(prefix = "spring.datasource.loex") //prefix表示参数的前缀
public DataSource getDataSource1(){
return DataSourceBuilder.create().build();
}
@Bean(name = "loexSqlSessionFactory")
@Primary //Qualifier表示查找spring ioc容器中名为loexDataSource的数据源
public SqlSessionFactory loexSqlSessionFactory(@Qualifier("loexDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/loex/*.xml"));
return bean.getObject();
}
@Bean("loexSqlSessionTemplate")
@Primary //表示默认数据源
public SqlSessionTemplate test1sqlsessiontemplate(@Qualifier("loexSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第四步就是:测试数据源是否成功:测试出数据已经查询出来了
2020-02-25 16:00:00.944 INFO 13608 --- [ main] com.example.jason.SourceApplication : Started SourceApplication in 1.72 seconds (JVM running for 2.33)
2020-02-25 16:00:04.836 INFO 13608 --- [nio-8090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-02-25 16:00:04.836 INFO 13608 --- [nio-8090-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-02-25 16:00:04.841 INFO 13608 --- [nio-8090-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
2020-02-25 16:00:04.865 INFO 13608 --- [nio-8090-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-02-25 16:00:04.866 WARN 13608 --- [nio-8090-exec-1] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2020-02-25 16:00:05.145 INFO 13608 --- [nio-8090-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
Score(id=1, name=张三, score=69, course=数学)
Score(id=2, name=张三, score=70, course=语文)
Score(id=3, name=李四, score=80, course=语文)
Score(id=4, name=李四, score=90, course=数学)
Score(id=5, name=王五, score=69, course=语文)
Score(id=6, name=王五, score=79, course=数学)
2020-02-25 16:00:05.177 INFO 13608 --- [nio-8090-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Starting...
2020-02-25 16:00:05.177 WARN 13608 --- [nio-8090-exec-1] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2020-02-25 16:00:05.185 INFO 13608 --- [nio-8090-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Start completed.