最近项目,一个后端代码被两个前端页面调用,代码数据库都冗余了,写着很难受感觉跟吃狗屎一样。因此决定先把数据库分开,然后之后把项目分成多模块化得。
想把数据库分开其实就只是需要实现多数据源操作,我考虑得不多,因此实现起来也比较简单。
首先是改yml配置文件
spring:datasource:exam:username: usernamepassword: passwordjdbcUrl: jdbc:mysql://ip地址/exam?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceaiexam:username: usernamepassword: passwordjdbcUrl: jdbc:mysql://ip地址/aiexam?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource
先把单个数据库连接配置改为这个样子,需要注意的是需要字段不是url而是jdbcUrl
配置写好后就开始写配置
第一个:
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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 javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.exam.mapper.aiexam",sqlSessionFactoryRef = "aiExamSqlSessionFactory")
public class AIExamDataSourceConfig {@Bean(name = "aiExamDataSource")@ConfigurationProperties("spring.datasource.aiexam")public DataSource masterDataSource(){return DataSourceBuilder.create().build();}@Bean(name = "aiExamSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("aiExamDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();sessionFactoryBean.setDataSource(dataSource);return sessionFactoryBean.getObject();}}
第二个:
package com.exam.conf;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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 javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.exam.mapper.exam",sqlSessionFactoryRef = "examSqlSessionFactory")
public class ExamDataSourceConfig {@Primary@Bean(name = "examDataSource")@ConfigurationProperties("spring.datasource.exam")public DataSource masterDataSource(){return DataSourceBuilder.create().build();}@Primary@Bean(name = "examSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("examDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();sessionFactoryBean.setDataSource(dataSource);return sessionFactoryBean.getObject();}}
我把第二个配置,也就是exam当作了默认配置源,因此需要在配置类上面每个方法中都需要加入
@Primary这个注解
我是没有xml文件得,这样写就行了,如果有xml文件还需要一个映射,参考下面代码:
@Configuration
@MapperScan(basePackages = "com.multiple.mapper.dev",sqlSessionFactoryRef = "devSqlSessionFactory")
public class DevDataSourceConfig {@Primary@Bean(name = "devDataSource")@ConfigurationProperties("spring.datasource.dev")public DataSource masterDataSource(){return DataSourceBuilder.create().build();}@Bean(name = "devSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("devDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();sessionFactoryBean.setDataSource(dataSource);sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/dev/*.xml"));return sessionFactoryBean.getObject();}
}
配置类写好后,需要在启动类上面加入
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
配置完成后就可以了。com.exam.mapper.aiexam包下的mapper都采用得AIExamDataSourceConfig 这个配置,com.exam.mapper.exam则采用的ExamDataSourceConfig。
关键点其实就是用配置类文件上面得@MapperScan进行了映射。同时如果不行,需要自己注意那些地方还有@MapperScan,是不是扫描范围过大给你覆盖了。
分库就到此位置,还是小菜一碟,分成多模块就比较麻烦了。
SpringBoot+MyBatis项目中同时操作多个数据库_springboot mybatis连接多个数据库-CSDN博客