Spring Boot中整合MyBatis多数据源不使用JPA的完整攻略
1. 添加依赖
首先,在pom.xml
文件中添加MyBatis和数据库驱动的依赖。例如,如果你使用MySQL数据库,可以添加以下依赖:
<dependencies>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>最新版本</version>
</dependency>
</dependencies>
2. 配置数据源
在application.properties
或application.yml
文件中配置多个数据源的连接信息。例如,配置两个数据源datasource1
和datasource2
:
spring:
datasource:
datasource1:
url: jdbc:mysql://localhost:3306/db1
username: root
password: password1
datasource2:
url: jdbc:mysql://localhost:3306/db2
username: root
password: password2
3. 创建数据源配置类
创建两个数据源的配置类,分别用于配置datasource1
和datasource2
。在配置类中,使用@Configuration
注解标记为配置类,并使用@Bean
注解创建对应的数据源。
@Configuration
@MapperScan(basePackages = \"com.example.mapper1\", sqlSessionTemplateRef = \"sqlSessionTemplate1\")
public class DataSource1Config {
@Bean
@ConfigurationProperties(prefix = \"spring.datasource.datasource1\")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory1(@Qualifier(\"dataSource1\") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate1(@Qualifier(\"sqlSessionFactory1\") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = \"com.example.mapper2\", sqlSessionTemplateRef = \"sqlSessionTemplate2\")
public class DataSource2Config {
@Bean
@ConfigurationProperties(prefix = \"spring.datasource.datasource2\")
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory2(@Qualifier(\"dataSource2\") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2(@Qualifier(\"sqlSessionFactory2\") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
4. 创建Mapper接口和对应的XML文件
在com.example.mapper1
和com.example.mapper2
包下分别创建Mapper接口和对应的XML文件。在接口中定义SQL语句的方法,XML文件中编写SQL语句。
// com.example.mapper1.UserMapper1.java
@Mapper
public interface UserMapper1 {
List<User> getAllUsers();
}
// com.example.mapper2.UserMapper2.java
@Mapper
public interface UserMapper2 {
List<User> getAllUsers();
}
<!-- com.example.mapper1.UserMapper1.xml -->
<mapper namespace=\"com.example.mapper1.UserMapper1\">
<select id=\"getAllUsers\" resultType=\"com.example.model.User\">
SELECT * FROM users
</select>
</mapper>
<!-- com.example.mapper2.UserMapper2.xml -->
<mapper namespace=\"com.example.mapper2.UserMapper2\">
<select id=\"getAllUsers\" resultType=\"com.example.model.User\">
SELECT * FROM users
</select>
</mapper>
5. 创建Service层
创建Service层,注入对应的Mapper接口,并编写业务逻辑。
@Service
public class UserService {
private final UserMapper1 userMapper1;
private final UserMapper2 userMapper2;
public UserService(UserMapper1 userMapper1, UserMapper2 userMapper2) {
this.userMapper1 = userMapper1;
this.userMapper2 = userMapper2;
}
public List<User> getAllUsersFromDataSource1() {
return userMapper1.getAllUsers();
}
public List<User> getAllUsersFromDataSource2() {
return userMapper2.getAllUsers();
}
}
6. 测试
在测试类中注入UserService,并调用对应的方法进行测试。
@SpringBootTest
class UserServiceTest {
@Autowired
private UserService userService;
@Test
void testGetAllUsersFromDataSource1() {
List<User> users = userService.getAllUsersFromDataSource1();
// 执行断言或其他操作
}
@Test
void testGetAllUsersFromDataSource2() {
List<User> users = userService.getAllUsersFromDataSource2();
// 执行断言或其他操作
}
}
通过以上步骤,你就可以在Spring Boot中成功整合MyBatis多数据源,并且不使用JPA。
希望这个攻略对你有所帮助!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:springboot 中整合mybatis多数据源不使用JPA - Python技术站