mybatisplus多主键查询
时间: 2023-11-21 22:55:33 浏览: 61
根据提供的引用内容,Mybatis-Plus提供了多种方法来进行查询操作,其中包括多主键查询。下面是一个使用Mybatis-Plus进行多主键查询的例子:
```java
// 定义多主键实体类
@TableName("user_role")
public class UserRole implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "user_id", type = IdType.INPUT)
private Long userId;
@TableId(value = "role_id", type = IdType.INPUT)
private Long roleId;
// 省略getter和setter方法
}
// 在Mapper接口中定义多主键查询方法
public interface UserRoleMapper extends BaseMapper<UserRole> {
List<UserRole> selectByUserAndRole(@Param("userId") Long userId, @Param("roleId") Long roleId);
}
// 在Service中调用多主键查询方法
List<UserRole> userRoleList = userRoleMapper.selectByUserAndRole(1L, 2L);
```
在上面的例子中,我们定义了一个多主键实体类`UserRole`,其中包含了两个主键`userId`和`roleId`。在Mapper接口中,我们定义了一个名为`selectByUserAndRole`的多主键查询方法,该方法接收两个参数`userId`和`roleId`,并返回一个`List<UserRole>`类型的结果集。在Service中,我们调用了该方法,并传入了两个主键值1L和2L,最终得到了符合条件的结果集。