环境管理开发实战

环境管理开发实战

前言

在上一集我们完成了项目管理开发实战,那么我们可以看到下面的E-R图和标题,我们可以知道,今天我们要完成环境管理开发实战!

可以看到一个项目当中是可以存在n个环境的,所以今天的环境管理开发实战,有的参数会和项目的字段有关系哦!

EnvironmentController

通过上一集,我们同样可以想到环境管理开发也是只需要那些API即可,所以话不多说,马上开始写代码,还是采用RESTful编程风格!

list

java 复制代码
@GetMapping("/list")
    public JsonData list(@RequestParam("projectId")Long projectId) {
        return JsonData.buildSuccess(environmentService.list(projectId));
    }
save
java 复制代码
 @PostMapping("/save")
    public JsonData save(@RequestBody EnvironmentSaveReq req) {
        return JsonData.buildSuccess(environmentService.save(req));
    }
update
java 复制代码
 @PostMapping("/update")
    public JsonData update(@RequestBody EnvironmentUpdateReq req) {
        return JsonData.buildSuccess(environmentService.update(req));
    }

del

java 复制代码
@PostMapping("/del")
    public JsonData delete(@RequestBody EnvironmentDelReq req) {
        return JsonData.buildSuccess(environmentService.delete(req.getId(),req.getProjectId()));
    }

可以看到,第一个api需要传入一个名为projectId的这个参数,这是因为,一个项目是可以有多个环境的,这也符合我们在E-R图当中项目和环境一对多的关系。

完整代码

java 复制代码
@RestController
@RequestMapping("/api/v1/env")
public class EnvironmentController {
    @Resource
    private EnvironmentService environmentService;

    @GetMapping("/list")
    public JsonData list(@RequestParam("projectId")Long projectId) {
        return JsonData.buildSuccess(environmentService.list(projectId));
    }

    @PostMapping("/save")
    public JsonData save(@RequestBody EnvironmentSaveReq req) {
        return JsonData.buildSuccess(environmentService.save(req));
    }

    @PostMapping("/update")
    public JsonData update(@RequestBody EnvironmentUpdateReq req) {
        return JsonData.buildSuccess(environmentService.update(req));
    }

    @PostMapping("/del")
    public JsonData delete(@RequestBody EnvironmentDelReq req) {
        return JsonData.buildSuccess(environmentService.delete(req.getId(),req.getProjectId()));
    }

}

不用多说,完整代码中的自定义请求体类在下面。

EnvironmentSaveReq
java 复制代码
@Data
public class EnvironmentSaveReq {
    private Long projectId;

    private String name;

    private String protocol;

    private String domain;

    private Integer port;

    private String description;

}
EnvironmentUpdateReq
java 复制代码
@Data
public class EnvironmentUpdateReq {
    private Long id;

    private Long projectId;

    private String name;

    private String protocol;

    private String domain;

    private Integer port;

    private String description;

}
EnvironmentDelReq
java 复制代码
@Data
public class EnvironmentDelReq {
    private Long id;

    private Long projectId;
}

可以看到这就是所有的自定义请求体类了。

接下来仅需把Service和它的Impl即可。

EnvironmentService

java 复制代码
public interface EnvironmentService {

    List<EnvironmentDTO> list(Long projectId);

    int save(EnvironmentSaveReq req);

    int update(EnvironmentUpdateReq req);

    int delete(Long id, Long projectId);
}

EnvironmentServiceImpl

list
java 复制代码
public List<EnvironmentDTO> list(Long projectId) {

        LambdaQueryWrapper<EnvironmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EnvironmentDO::getProjectId,projectId);

        List<EnvironmentDO> environmentDOS = environmentMapper.selectList(queryWrapper);

        return SpringBeanUtil.copyProperties(environmentDOS,EnvironmentDTO.class);
    }
save
java 复制代码
public int save(EnvironmentSaveReq req) {
        ProjectDO projectDO = projectMapper.selectById(req.getProjectId());
        if(projectDO != null){
            EnvironmentDO environmentDO = SpringBeanUtil.copyProperties(req, EnvironmentDO.class);
            return environmentMapper.insert(environmentDO);
        }
        return 0;
    }
update
java 复制代码
public int update(EnvironmentUpdateReq req) {
        ProjectDO projectDO = projectMapper.selectById(req.getProjectId());
        if(projectDO != null){
            EnvironmentDO environmentDO = SpringBeanUtil.copyProperties(req, EnvironmentDO.class);
            return environmentMapper.updateById(environmentDO);
        }
        return 0;
    }
delete
java 复制代码
 public int delete(Long id, Long projectId) {

        LambdaQueryWrapper<EnvironmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EnvironmentDO::getId,id);
        queryWrapper.eq(EnvironmentDO::getProjectId,projectId);

        return environmentMapper.delete(queryWrapper);
    }
完整代码
java 复制代码
@Service
@Slf4j
public class EnvironmentServiceImpl implements EnvironmentService {

    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private EnvironmentMapper environmentMapper;
    @Override
    public List<EnvironmentDTO> list(Long projectId) {

        LambdaQueryWrapper<EnvironmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EnvironmentDO::getProjectId,projectId);

        List<EnvironmentDO> environmentDOS = environmentMapper.selectList(queryWrapper);

        return SpringBeanUtil.copyProperties(environmentDOS,EnvironmentDTO.class);
    }

    @Override
    public int save(EnvironmentSaveReq req) {
        ProjectDO projectDO = projectMapper.selectById(req.getProjectId());
        if(projectDO != null){
            EnvironmentDO environmentDO = SpringBeanUtil.copyProperties(req, EnvironmentDO.class);
            return environmentMapper.insert(environmentDO);
        }
        return 0;
    }

    @Override
    public int update(EnvironmentUpdateReq req) {
        ProjectDO projectDO = projectMapper.selectById(req.getProjectId());
        if(projectDO != null){
            EnvironmentDO environmentDO = SpringBeanUtil.copyProperties(req, EnvironmentDO.class);
            return environmentMapper.updateById(environmentDO);
        }
        return 0;
    }

    @Override
    public int delete(Long id, Long projectId) {

        LambdaQueryWrapper<EnvironmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EnvironmentDO::getId,id);
        queryWrapper.eq(EnvironmentDO::getProjectId,projectId);

        return environmentMapper.delete(queryWrapper);
    }
}

看上去还是十分的简单的,没有很复杂的逻辑代码,也就用了一下Mybatis-Plus的LambdaQueryWrapper的类而已,其实也就是构建了SQL查询条件。

当然在最后还是会给出相关的DTO。

EnvironmentDTO
java 复制代码
@Data
public class EnvironmentDTO {
    private Long id;

    private Long projectId;

    private String name;

    private String protocol;

    private String domain;

    private Integer port;

    private String description;

    private Date gmtCreate;

    private Date gmtModified;
}
相关推荐
彭于晏6891 分钟前
Android广播
android·java·开发语言
程序员-珍19 分钟前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
Rookie也要加油32 分钟前
01_SQLite
数据库·sqlite
liuxin3344556637 分钟前
教育技术革新:SpringBoot在线教育系统开发
数据库·spring boot·后端
2401_857297911 小时前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
福大大架构师每日一题1 小时前
23.1 k8s监控中标签relabel的应用和原理
java·容器·kubernetes
金灰1 小时前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5
菜鸟一皓1 小时前
IDEA的lombok插件不生效了?!!
java·ide·intellij-idea
爱上语文1 小时前
Java LeetCode每日一题
java·开发语言·leetcode
看山还是山,看水还是。1 小时前
MySQL 管理
数据库·笔记·mysql·adb