Java:List集合结合实战需求分析

我是 ABin-阿斌:写一生代码,创一世佳话,筑一览芳华。 如果小伙伴们觉得我的文章不错,记得一键三连,感谢~
在这里插入图片描述

添加操作

  • 在开发中我们往往会遇到如下图中一样添加多行多个或一个的需求,初学者的小伙伴可能一下不知道如何去构思。下面我就来讲讲遇到这种需求时如何去分析,有兴趣的伙伴可以看看。

在这里插入图片描述

问题分析

  • 从上图中我们可以看到前端可以进行无限添加的,那么这个之后他们传进来的参数就是说是可以多份的
  • 这时我们可以集合 Java 当中所学的 List 集合方案来解决这一问题。我们知道 List就是一个盒子,那么盒子里面放的就是多条数据。这样一想那么这个问题就很好解决了。
  • 如果对 List 集合不熟悉的小伙伴可以看我的这篇文章:Java基础:集合:List接口的介绍与使用
  • 下面我们就用代码来实现这个思想,看看是否成立

代码演示

入参:Param

  • 这里我们是一个对像里面嵌套了一个 List集合,然后这个List集合是多个字段,所以我们用一个对象来存,直接使用的是一个内部类的方式,具体可以根据自己项目需求来变化。
  • 对于内部类不熟悉的小伙伴可以看我这篇文章:Java基础:面向对象:代码块与内部类的介绍与使用
@AllArgsConstructor
@NoArgsConstructor
@Data
@Accessors(chain = true)
public class AddSecurityGroupParam {

    @ApiModelProperty("id(数据库中的id)")
    private String id;

    @ApiModelProperty("用户ID")
    private Long userId;

    @ApiModelProperty("安全组名称")
    private String securityGroupName;
    
    @ApiModelProperty("安全组备注")
    @Length(max = 30,message = "安全组备注不能超过30个字符")
    private String securityGroupRemark;
	
	
    @ApiModelProperty("下行规则列表")
    private List<Rules> inboundRules;

    @ApiModelProperty("上行规则列表")
    private List<Rules> outboundRules;

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @Accessors(chain = true)
    public static class Rules {

        @ApiModelProperty("安全组规则id")
        private Long ruleId;

        @NotNull
        private Integer protocolType;

        @ApiModelProperty("起始端口")
        @Pattern(regexp = PORT_PATTERN, message = "端口输入不正确,端口范围:1~65535")
        private String startPort;

        @ApiModelProperty("结束端口")
        @Pattern(regexp = PORT_PATTERN, message = "端口输入不正确,端口范围:1~65535")
        private String endPort;

        @ApiModelProperty("远端类型")
        private Integer remoteType;

        @ApiModelProperty("远端安全组id")
        private Long remoteGroupId;

        @ApiModelProperty("源IP")
        private String srcIp;

        @ApiModelProperty("TAG")
        private String tag;

        @ApiModelProperty("备注")
        private String remark;

        @ApiModelProperty("状态")
        @NotNull
        private Integer status;

    }

具体实现:ServiceImpl

  • 从上面代码中的入参参数,我们就可以轻松的写出业务代码了
  • 下面代码只提供具体 List 集合思路,其余的业务就不写了,你们可以根据实际业务改。

细节分析:

  • 这个 param.getInboundRules() 就是上面代码中的集合参数
//下行规则列表
 if (ObjectUtil.isNotEmpty(param.getInboundRules())) {
     for (AddSecurityGroupParam.Rules inboundRule : param.getInboundRules()) {
         if (ObjectUtil.isEmpty(inboundRule.getRuleId())) {
             securityGroupEntity.setSecurityGroupUuId(groupEntity.getSecurityGroupUuId());
             //这里我抽取了一个方法
             addSecurityGroupRule(inboundRule, INBOUND_RULE, groupEntity, loginUser);
         }
     }
 }

//抽取方法
 private void addSecurityGroupRule(AddSecurityGroupParam.Rules inboundRule, Integer type,
                                      SecurityGroupEntity securityGroupEntity, UserInfoEntity loginUser) {
        if (GroupAgreementEnum.ICMP.getStatus().equals(inboundRule.getProtocolType())) {
            inboundRule.setStartPort("1");
            inboundRule.setEndPort("1");
        } else if (!GroupAgreementEnum.ANY.getStatus().equals(inboundRule.getProtocolType())) {
            Assert.isTrue(Pattern.matches(PORT_PATTERN, inboundRule.getStartPort()), "端口为1-65535的正整数");
            Assert.isTrue(Pattern.matches(PORT_PATTERN, inboundRule.getEndPort()), "端口为1-65535的正整数");
            Assert.isTrue(Integer.parseInt(inboundRule.getEndPort()) >= Integer.parseInt(inboundRule.getStartPort()), "起始端口不能大于结束端口");
        } else {
            inboundRule.setStartPort("1");
            inboundRule.setEndPort("65535");
        }
		//这里就开始正式添加了
        SecurityGroupRulesEntity securityGroupRulesEntity = new SecurityGroupRulesEntity()
                .setSecurityGroupId(securityGroupEntity.getId())
                .setType(type)
                .setProtocolType(inboundRule.getProtocolType())
                .setStartPort(Integer.parseInt(inboundRule.getStartPort()))
                .setEndPort(Integer.parseInt(inboundRule.getEndPort()))
                .setRemoteType(inboundRule.getRemoteType())
                .setRemoteGroupId(inboundRule.getRemoteGroupId())
                .setSrcIp(inboundRule.getSrcIp())
                .setTag(inboundRule.getTag())
                .setRemark(inboundRule.getRemark())
                .setCreateTimeStamp(DateUtil.date().getTime());
       //保存即可         
       securityGroupRulesService.save(securityGroupRulesEntity);

查询操作

  • 上面的是添加操作,那么添加完之后我们要查询出来,由于是添加的多个那么这个时候我们怎么把这个:上下行规则 List集合 查询出来呢,可以看下面操作

原型

  • 当用户点击详情时,需要返回这样的一个格式页面

  • 返回的Vo其实跟我们上面添加进去的参数是一样的

在这里插入图片描述

代码演示

  • 这里只做核心代码演示
//1、因为我们要返回的上下新规则是一个List集合,所以我们只要把这List集合new 出来就好
 public SecurityGroupDetailsVo getSecurityGroupDetails(@NotEmpty String securityGroupId) {
   SecurityGroupDetailsVo securityGroupDetailsVo = new SecurityGroupDetailsVo();
   
  	List<SecurityGroupDetailsVo.Rules> ruleList = new ArrayList<>();
//2、new 出来的这个List是个空的,那么我们只需要把数据查出来,set到这个list中去就可以了
List<SecurityGroupRulesEntity> outboundRulelist = securityGroupRulesService.lambdaQuery()
                .eq(SecurityGroupRulesEntity::getSecurityGroupId, Long.parseLong(securityGroupId))
                .eq(SecurityGroupRulesEntity::getType, type)
                .eq(SecurityGroupRulesEntity::getDeleted, NOT_DELETED)
                .list();
  //3、把上面查出来的List进行遍历,逐个set到我们要返回的这个:ruleList中去
 for (SecurityGroupRulesEntity rules : outboundRulelist) {
  SecurityGroupDetailsVo.Rules rule = new SecurityGroupDetailsVo.Rules()
                        .setRuleId(String.valueOf(rules.getId()))
                        .setProtocolType(rules.getProtocolType())
                        .setStartPort(String.valueOf(rules.getStartPort()))
                        .setEndPort(String.valueOf(rules.getEndPort()))
                        .setRemoteType(rules.getRemoteType());

 }
  			ruleList.add(rule);

//4、最后我们只需要将第一步这个List的东西set到我们返回的Vo对应参数就可以了
 securityGroupDetailsVo.setInboundRules(ruleList)

返回出去的JSON格式

{
    "data": {
        "securityGroupId": "",
        "securityGroupIdName": "",
        "securityGroupName": "",
        "createTimeStamp": "",
        "rulesNum": 7,
        "remark": "",
        "outboundRules": [
            {
                "ruleId": "",
                "protocolType": 1,
                "startPort": "1",
                "endPort": "",
                "remoteType": 0,
                "remoteGroupId": "0",
                "srcIp": "",
                "status": 0
            },
            {
                "ruleId": "",
                "protocolType": 3,
                "startPort": "",
                "endPort": "12",
                "remoteType": 1,
                "remoteGroupId": "",
                "srcIp": "",
                "status": 0
            }
        ]
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值