mybatisplus中lambda的使用
一、lambdaUpdate
1.更新某张表的某个字段
/**
*
* 将fly_numbers 字段值+1
**/
boolean success = airRouteService.lambdaUpdate()
.setSql("fly_numbers = fly_numbers + 1")
.eq(AirRoute::getId, routeId)
.update();
2.删除单条数据
/**
*
*删除对应id下的图片
**/
taskImageService.lambdaUpdate()
.eq(TaskImage::getTaskId, taskId)
.remove();
3.删除多条数据
/**
*
*删除对应ids下的图片
**/
taskImageService.lambdaUpdate()
.in(TaskImage::getTaskId, taskIds)
.remove();
二、lambdaQuery
1.lambdaQuery获取数量
/**
*
* 获取轨迹数量
**/
cationCount = taskLocationService.lambdaQuery()
.eq(TaskLocation::getTaskId, id)
.count();
2.lambdaQuery获取集合
taskImageService.lambdaQuery()
.select(TaskImage::getImageUrl) //查询imageurl字段
.eq(TaskImage::getTaskId, taskId) //查询条件
.list() //获得集合
.stream() //转为流
.filter(i -> !StringUtils.isEmpty(i.getImageUrl())) //过滤为空字段
.map(TaskImage::getImageUrl)
.collect(Collectors.toList());
3.lambdaQuery动态查询排序分页
IPage<Task> pageInfo = Condition.getPage(query);
taskService.lambdaQuery()
.eq(!Objects.isNull(task.getProjectId()), Task::getProjectId, task.getProjectId())
// 按创建时间排序
.orderByDesc(Task::getCreateTime)
.page(pageInfo);
4.lambdaQuery查询单个字段的值
Long polygonId = polygonPointService.lambdaQuery()
.eq(PolygonPoint::getId, id)
.one()
.getPolygonId();
5.按照指定格式输出
工具类
/**
* 通用组合框对象
*
* @author pxz
*/
@Data
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "通用组合框对象")
public class ComboBoxVO implements Serializable {
private static final long serialVersionUID = 3431871989555003768L;
/**
* ID
*/
private Long id;
/**
* name
*/
private String name;
public ComboBoxVO(Long id, String name) {
this.id = id;
this.name = name;
}
}
实现代码
@Override
public List<ComboBoxVO> getEquipmentListByUserId(Integer userId) {
List<Equipment> equipments = equipmentService.lambdaQuery()
.select(Equipment::getId, Equipment::getName)
.eq(Equipment::getUserId, userId)
.list();
if (CollectionUtils.isEmpty(equipments)) {
return Collections.emptyList();
}
List<ComboBoxVO> voList = equipments.stream()
.map(e -> new ComboBoxVO(Long.valueOf(e.getId().toString()), e.getName()))
.collect(Collectors.toList());
return voList;
}
}