Service中包含多个Mapper该怎么调用
时间: 2024-02-01 16:15:42 浏览: 39
在Service中包含多个Mapper时,可以通过在Mapper接口上使用`@Qualifier`注解来指定要使用的Mapper实现类,如下所示:
```java
@Service
public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
private final RoleMapper roleMapper;
public UserServiceImpl(@Qualifier("userMapper") UserMapper userMapper,
@Qualifier("roleMapper") RoleMapper roleMapper) {
this.userMapper = userMapper;
this.roleMapper = roleMapper;
}
// 其他方法省略
}
```
在这个例子中,`UserServiceImpl`包含了`UserMapper`和`RoleMapper`两个Mapper接口,其中`UserMapper`和`RoleMapper`分别有不同的实现类。通过在构造函数中使用`@Qualifier`注解,我们可以指定要使用的Mapper实现类。
接下来,在Mapper接口上,我们可以使用`@Repository`注解指定Mapper实现类的名称,如下所示:
```java
@Repository("userMapper")
public interface UserMapper {
// 方法定义省略
}
@Repository("roleMapper")
public interface RoleMapper {
// 方法定义省略
}
```
这样,在调用`UserService`的方法时,我们就可以直接使用`userMapper`和`roleMapper`调用相应的Mapper方法了。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)