Spring Boot整合MyBatis多数据源配置:开启数据处理新境界 在软件开发的世界里,数据就如同城市中的居民,有着不同的来源和去处。有时候,我们需要管理多个数据源,就像城市管理者要照顾不同区域的居民一样。Spring Boot作为一个强大的开发框架,就像是一位神通广大的魔法师,而MyBatis则是一把精准的宝剑。当Spring Boot与MyBatis结合,并且实现多数据源配置时,就如同魔法师挥舞着宝剑,能在数据的江湖中披荆斩棘。那么,如何实现Spring Boot整合MyBatis多数据源配置呢?接下来就为大家详细道来。
准备工作:搭建基础框架 要进行Spring Boot整合MyBatis多数据源配置,就如同建造一座大厦,首先要打好地基。这里的地基就是我们的项目基础框架。 第一步,创建Spring Boot项目。可以使用Spring Initializr这个便捷的工具,它就像是一个高效的建筑设计师,能快速为我们勾勒出项目的大致轮廓。打开Spring Initializr的官网,选择好项目的相关信息,如项目类型、语言、Spring Boot版本等。就像设计师根据客户的需求,确定大厦的风格、层数等基本参数。然后添加必要的依赖,如Spring Web、MyBatis Framework、MySQL Driver等,这些依赖就像是建造大厦所需的各种建筑材料。 第二步,配置项目的基本信息。在项目的配置文件application.properties或application.yml中,配置好数据库的连接信息。这就好比为大厦接通水电等基础设施,让项目能够与数据库顺利通信。例如,在application.yml中配置: yaml spring: datasource: primary: url: jdbc:mysql://localhost:3306/primary_db username: root password: password driver-class-name: com.mysql.cj.jdbc.Driver secondary: url: jdbc:mysql://localhost:3306/secondary_db username: root password: password driver-class-name: com.mysql.cj.jdbc.Driver
这里配置了两个数据源,primary和secondary,就像是为大厦准备了两个不同的物资仓库,分别存放不同类型的物资。
数据源配置:打造数据通道 有了基础框架后,接下来要做的就是配置数据源,这就像是为大厦建造不同的通道,让数据能够顺畅地进出不同的仓库。
- 创建数据源配置类。在项目中创建一个数据源配置类,例如DataSourceConfig。在这个类中,使用@Configuration注解将其标记为配置类,就像给这个类贴上一个“配置专家”的标签。然后使用@Bean注解创建数据源实例。 java 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 public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
这里创建了两个数据源实例,primaryDataSource和secondaryDataSource,就像建造了两条不同的通道,分别通往不同的物资仓库。@Primary注解表示这个数据源是主数据源,就像在大厦中,有一个主要的物资入口。 2. 配置MyBatis的SqlSessionFactory。为每个数据源创建对应的SqlSessionFactory,这就像是为每条通道配备专门的运输工具,让数据能够高效地在通道中运输。 java 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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource;
@Configuration @MapperScan(basePackages = "com.example.primary.mapper", sqlSessionFactoryRef = "primarySqlSessionFactory") public class PrimaryMyBatisConfig {
@Bean(name = "primarySqlSessionFactory")
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/primary/*.xml"));
return sessionFactory.getObject();
}
}
@Configuration @MapperScan(basePackages = "com.example.secondary.mapper", sqlSessionFactoryRef = "secondarySqlSessionFactory") public class SecondaryMyBatisConfig {
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/secondary/*.xml"));
return sessionFactory.getObject();
}
}
这里分别为primaryDataSource和secondaryDataSource创建了对应的SqlSessionFactory,并且指定了Mapper接口的扫描路径和Mapper XML文件的位置。就像www.ysdslt.com为不同的运输工具,规定了运输的路线和目的地。
Mapper接口和XML文件:编排数据指令 有了数据源和SqlSessionFactory后,接下来要编写Mapper接口和对应的XML文件,这就像是为运输工具编写详细的运输指令,告诉它们要运输什么货物,从哪里运到哪里。
- 创建Mapper接口。在项目中创建Mapper接口,例如PrimaryMapper和SecondaryMapper。这些接口就像是运输指令的大纲,定义了数据操作的方法。 java public interface PrimaryMapper { List> getAllData(); }
public interface SecondaryMapper { List> getAllData(); }
这里定义了两个Mapper接口,分别用于从主数据源和次数据源获取数据。 2. 创建Mapper XML文件。在resources目录下创建对应的Mapper XML文件,如mapper/primary/PrimaryMapper.xml和mapper/secondary/SecondaryMapper.xml。在XML文件中编写具体的SQL语句,这就像是为运输指令添加详细的内容,告诉运输工具具体要执行的操作。 xml
SELECT * FROM primary_table
SELECT * FROM secondary_table
这里分别编写了从主数据源和次数据源获取数据的SQL语句。
服务层和控制器:调度数据运输 最后,在服务层和控制器中使用Mapper接口,实现数据的查询和展示。这就像是调度中心,指挥运输工具按照指令运输货物,并将货物展示给需要的人。
- 创建服务层类。在项目中创建服务层类,例如PrimaryService和SecondaryService。在服务层类中注入对应的Mapper接口,调用Mapper接口的方法获取数据。 java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map;
@Service public class PrimaryService {
@Autowired
private PrimaryMapper primaryMapper;
public List> getAllData() {
return primaryMapper.getAllData();
}
}
@Service public class SecondaryService {
@Autowired
private SecondaryMapper secondaryMapper;
public List> getAllData() {
return secondaryMapper.getAllData();
}
}
这里分别创建了PrimaryService和SecondaryService,用于从主数据源和次数据源获取数据。 2. 创建控制器类。在项目中创建控制器类,例如DataController。在控制器类中注入服务层类,调用服务层类的方法,将数据返回给客户端。 java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map;
@RestController public class DataController {
@Autowired
private PrimaryService primaryService;
@Autowired
private SecondaryService secondaryService;
@GetMapping("/primary-data")
public List> getPrimaryData() {
return primaryService.getAllData();
}
@GetMapping("/secondary-data")
public List> getSecondaryData() {
return secondaryService.getAllData();
}
}
这里创建了DataController,提供了两个接口,分别用于获取主数据源和次数据源的数据。
通过以上步骤,我们就完成了Spring Boot整合MyBatis多数据源配置。就像建造了一座功能强大的大厦,能够高效地管理和处理多个数据源的数据。在实际开发中,根据项目的需求,可以灵活调整数据源的配置和数据操作的逻辑,让项目在数据的海洋中自由航行。