@Data @ApiModel(value = "项目管理") public class BisProjectVO { @ApiModelProperty(value = "id") private Integer id; @ApiModelProperty(value = "项目名称") private String projectName; } 根据id查询项目名称,用mybatis-plus写,不要写mapper和sql
时间: 2023-12-14 11:36:10 浏览: 79
可以使用 Mybatis-Plus 提供的便捷方法 `selectById`,代码如下:
```
@Service
public class BisProjectService extends ServiceImpl<BisProjectMapper, BisProjectVO> {
public String getProjectNameById(Integer id){
BisProjectVO bisProjectVO = this.baseMapper.selectById(id);
return bisProjectVO == null ? null : bisProjectVO.getProjectName();
}
}
```
其中,`BisProjectMapper` 是继承了 Mybatis-Plus 的 `BaseMapper` 的自定义 Mapper 接口。`this.baseMapper.selectById(id)` 就是使用 Mybatis-Plus 提供的 `selectById` 方法查询 id 对应的项目记录,并将结果封装成 `BisProjectVO` 对象返回。最后,返回 `BisProjectVO` 对象中的项目名称属性即可。
相关问题
@Data @ApiModel(value = "塔站") public class BisTowerVO { private Integer id; private Integer projectId; private String towerNo; } @Data @ApiModel(value = "项目管理") public class BisProjectVO { @ApiModelProperty(value = "id") private Integer id; @ApiModelProperty(value = "项目名称") private String projectName; } 这是两个VO类,BisTower类的projectId关联BisProject类的id,帮我写个查询,只显示towerNo字段的值和projectName 用mybtais-plus写,只写servcie ,不要写mapper和sql
好的,可以参考下面的代码实现:
```java
@Service
public class BisTowerService {
@Autowired
private BisTowerMapper towerMapper;
@Autowired
private BisProjectMapper projectMapper;
public List<Map<String, Object>> listTowerWithProjectName() {
List<BisTower> towerList = towerMapper.selectList(null);
List<Map<String, Object>> resultList = new ArrayList<>();
for (BisTower tower : towerList) {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("towerNo", tower.getTowerNo());
BisProject project = projectMapper.selectById(tower.getProjectId());
resultMap.put("projectName", project == null ? null : project.getProjectName());
resultList.add(resultMap);
}
return resultList;
}
}
```
在这个实现中,我们注入了 BisTowerMapper 和 BisProjectMapper,分别用来查询 BisTower 和 BisProject 对象。在 `listTowerWithProjectName()` 方法中,我们首先查询出所有的 BisTower 对象,然后遍历每个对象,对于每个对象,我们都创建一个 `Map<String, Object>` 对象,将 `towerNo` 和 `projectName` 两个属性加入到这个 Map 对象中,最后将这个 Map 对象加入到结果列表中。在查询 projectName 时,我们利用 BisTower 对象中的 `projectId` 属性,查询出对应的 BisProject 对象,然后从 BisProject 对象中取出 `projectName` 属性的值。需要注意的是,如果查询出来的 BisProject 对象为 null,则说明该 BisTower 对象中的 projectId 属性没有对应的 BisProject 对象,此时 `projectName` 属性的值应该为 null。
@Data @ApiModel(value = "飞行任务管理-后端列表页vo") public class BisTaskVO extends PageReqVO { @ApiModelProperty(value = "id") private Integer id; @ApiModelProperty(value = "逻辑删除 1是 0否") private Integer isDel; } 这是个VO类,用mybatis-plus写个批量逻辑删除
可以使用 MyBatis-Plus 提供的 `IService` 接口中的 `removeByIds` 方法来实现批量逻辑删除。具体实现步骤如下:
1. 在 `BisTaskMapper` 接口中新增一个方法 `deleteBatchLogicByIds`,用于批量逻辑删除任务。方法定义如下:
```java
public interface BisTaskMapper extends BaseMapper<BisTask> {
int deleteBatchLogicByIds(@Param("ids") List<Integer> ids);
}
```
2. 在 `BisTaskServiceImpl` 实现类中,注入 `BisTaskMapper` 接口,并实现 `deleteBatchLogicByIds` 方法。方法定义如下:
```java
@Service
public class BisTaskServiceImpl extends ServiceImpl<BisTaskMapper, BisTask> implements BisTaskService {
@Autowired
private BisTaskMapper bisTaskMapper;
@Override
public boolean deleteBatchLogicByIds(List<Integer> ids) {
return bisTaskMapper.deleteBatchLogicByIds(ids) > 0;
}
}
```
3. 在控制器中调用 `deleteBatchLogicByIds` 方法实现批量逻辑删除。示例代码如下:
```java
@RestController
@RequestMapping("/task")
public class BisTaskController {
@Autowired
private BisTaskService bisTaskService;
@PostMapping("/deleteBatch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Result.fail("参数错误");
}
// 批量逻辑删除任务
boolean success = bisTaskService.deleteBatchLogicByIds(ids);
if (success) {
return Result.success();
}
return Result.fail("删除失败");
}
}
```
注意事项:
- `BisTask` 实体类中需要有 `isDel` 属性用于记录逻辑删除状态。
- 执行批量逻辑删除时,需要传入待删除任务的 id 列表。
- 执行成功时,返回 `Result.success()`,否则返回 `Result.fail()`。
阅读全文