一,学习MyBatis和MyBatis-plus:
mybatis:官网文档:mybatis – MyBatis 3 | 简介
mybatis-plus:官网文档:MyBatis-Plus
二,MyBatis来实现多数据源的查询:
配置文件的格式:
spring:datasource: db1:driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useUnicode=true&timeZone=UTCusername: rootpassword: rootdb2:driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/test1?characterEncoding=utf8&useUnicode=true&timeZone=UTCusername: rootpassword: root
注意:将数据库配置中的url改为jdbc-url,否则无法配置多数据源。
在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 org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.czf.connect.mapper.db1", sqlSessionTemplateRef = "db1SqlSessionTemplate")
//此处的basePackages指向的是你存放数据库db1的mapper的包
public class DataSource1Config {@Bean(name = "db1DataSource")@ConfigurationProperties(prefix = "spring.datasource.db1")//指向yml配置文件中的数据库配置@Primary //主库加这个注解,修改优先权,表示发现相同类型bean,优先使用该方法。public DataSource dbDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "db1SqlSessionFactory")@Primarypublic SqlSessionFactory dbSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*xml"));//这个的getResources指向的是你的mapper.xml文件,相当于在yml中配置的mapper-locations,此处配置了yml中就不用配置,或者说不会读取yml中的该配置。return bean.getObject();}@Bean(name = "db1TransactionManager")@Primarypublic DataSourceTransactionManager dbTransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "db1SqlSessionTemplate")@Primarypublic SqlSessionTemplate dbSqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}
}
第二个:
@Configuration
@MapperScan(basePackages = "com.czf.connect.mapper.db2", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DataSource2Config {@Bean(name = "db2DataSource")@ConfigurationProperties(prefix = "spring.datasource.db2")public DataSource dbDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "db2SqlSessionFactory")public SqlSessionFactory dbSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*xml"));return bean.getObject();}@Bean(name = "db2TransactionManager")public DataSourceTransactionManager dbTransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "db2SqlSessionTemplate")public SqlSessionTemplate dbSqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}
}
至此,需要修改或查询哪个数据库,只需要在对应的com///mapper/db包中创建对应的mapper类或者编写特定的sql语句即可。
三 ,MyBatis-plus来实现多数据源的查询:
1.配置文件的:
spring:datasource:dynamic:primary: teacher #设置默认的数据源或者数据源组,默认值即为masterstrict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源datasource:teacher:url: jdbc:mysql://localhost:3306/test1?characterEncoding=utf8&useUnicode=true&timeZone=UTCdriver-class-name: com.mysql.jdbc.Driverusername: rootpassword: rootstudent:url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useUnicode=true&timeZone=UTCdriver-class-name: com.mysql.jdbc.Driverusername: rootpassword: root
2.导入依赖包:
<!-- 多数据源 --><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.0</version></dependency>
3.使用:
package com.example.multidatasource.generator.mapper;import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.multidatasource.generator.domain.Teacher;
import org.apache.ibatis.annotations.Mapper;/**
* @author 29291
* @description 针对表【teacher】的数据库操作Mapper
* @createDate 2023-08-07 23:25:22
* @Entity generator.domain.Teacher
*/@Mapper
@DS("slave")
public interface TeacherMapper extends BaseMapper<Teacher> {}