springboot整合mybatis多数据源 并用mapper接口的方式查询数据,sql写在xml中,给出xml配置样例 并给出具体代码
时间: 2023-05-12 09:06:16 浏览: 171
springboot-mdatasource.zip
以下是一个简单的 Spring Boot 整合 MyBatis 多数据源的示例,其中使用了 Mapper 接口的方式查询数据,SQL 语句写在 XML 文件中。
首先,在 pom.xml 文件中添加以下依赖:
```xml
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBatis Starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
```
然后,在 application.properties 文件中配置数据源:
```properties
# 主数据源
spring.datasource.primary.url=jdbc:mysql://localhost:3306/primary_db?useSSL=false&serverTimezone=UTC
spring.datasource.primary.username=root
spring.datasource.primary.password=root
# 从数据源
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/secondary_db?useSSL=false&serverTimezone=UTC
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
```
接下来,创建两个数据源的配置类:
```java
@Configuration
@MapperScan(basePackages = "com.example.mapper.primary", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class PrimaryDataSourceConfig {
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/primary/*.xml"));
return sessionFactoryBean.getObject();
}
@Bean
@Primary
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "com.example.mapper.secondary", sqlSessionTemplateRef = "secondarySqlSessionTemplate")
public class SecondaryDataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/secondary/*.xml"));
return sessionFactoryBean.getObject();
}
@Bean
public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
其中,@MapperScan 注解用于扫描 Mapper 接口,sqlSessionTemplateRef 属性用于指定使用的 SqlSessionTemplate。
最后,创建 Mapper 接口和 XML 文件:
```java
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user")
List<User> findAll();
}
@Mapper
public interface OrderMapper {
@Select("SELECT * FROM `order`")
List<Order> findAll();
}
```
```xml
<mapper namespace="com.example.mapper.primary.UserMapper">
<resultMap id="BaseResultMap" type="com.example.entity.User">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
</resultMap>
<select id="findAll" resultMap="BaseResultMap">
SELECT * FROM user
</select>
</mapper>
<mapper namespace="com.example.mapper.secondary.OrderMapper">
<resultMap id="BaseResultMap" type="com.example.entity.Order">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="price" property="price" jdbcType="DECIMAL"/>
</resultMap>
<select id="findAll" resultMap="BaseResultMap">
SELECT * FROM `order`
</select>
</mapper>
```
这样,就完成了 Spring Boot 整合 MyBatis 多数据源的配置。在需要使用数据源的地方,只需要注入对应的 Mapper 接口即可。
参考资料:
- [Spring Boot 整合 MyBatis 多数据源](https://www.cnblogs.com/zhaozihan/p/10900886.html)
阅读全文