mybatis-plus 实现联合主键批量更新保存
时间: 2023-07-11 07:19:55 浏览: 247
Mybatis-plus可以通过使用`@TableId`注解来实现联合主键的设置。具体操作如下:
1. 在实体类中使用`@TableId`注解来指定联合主键,示例如下:
```java
@Data
@TableName("user_role")
public class UserRole {
@TableId(type = IdType.INPUT)
private Integer userId;
@TableId(type = IdType.INPUT)
private Integer roleId;
private Date createTime;
private Date updateTime;
}
```
在这个示例中,`userId`和`roleId`作为联合主键。
2. 在Mapper接口中,使用`insertOrUpdateBatch`方法来实现批量保存或更新。示例如下:
```java
public interface UserRoleMapper extends BaseMapper<UserRole> {
int insertOrUpdateBatch(@Param("list") List<UserRole> list);
}
```
3. 在Service层中,调用Mapper接口提供的`insertOrUpdateBatch`方法即可实现批量保存或更新。示例如下:
```java
@Service
public class UserRoleService {
@Autowired
private UserRoleMapper userRoleMapper;
public void saveOrUpdateBatch(List<UserRole> list) {
userRoleMapper.insertOrUpdateBatch(list);
}
}
```
在这个示例中,`saveOrUpdateBatch`方法调用了`insertOrUpdateBatch`方法,实现了批量保存或更新。
以上就是使用Mybatis-plus实现联合主键批量保存或更新的方法。注意,在使用`insertOrUpdateBatch`方法时,需要将联合主键的值设置到实体类对应的属性中,并且实体类中必须包含`createTime`和`updateTime`属性。
阅读全文
相关推荐
















