当前位置: 首页 > news >正文

山东大禹建设集团网站正在直播足球比赛

山东大禹建设集团网站,正在直播足球比赛,网站什么引导页,asp网站攻击关于在集合中对象比较属性值的问题1 问题说明2 问题排查3 总结及伪代码楼主在最近遇到一个场景,项目中有一个校验。 需要将数据库查询的集合对象与前端传递的集合对象进行比较,看数据是否被修改。 1 问题说明 基于上面项目需求,项目为较老的…

关于在集合中对象比较属性值的问题

  • 1 问题说明
  • 2 问题排查
  • 3 总结及伪代码

楼主在最近遇到一个场景,项目中有一个校验。 需要将数据库查询的集合对象与前端传递的集合对象进行比较,看数据是否被修改。

1 问题说明

基于上面项目需求,项目为较老的传统项目,没使用Lombok插件(划重点),实体类和DTO,BO对象都是原有get、set方法等等。项目之前运行正常,且没有任何问题。接到新需求,需要多添加某些字段。

再一顿CRUD的基础操作之后,代码加上去,实体类对应的页面增删改查都没有问题。于是极度自信,提交代码。(O(∩_∩)O)

在代码中上述校验处,突然报错,直接给我整懵逼。

2 问题排查

本着哪里有问题,就解决哪里的思维。

  • 首先,怀疑是不是前端传值有误差(前端问题),在集合比较处添加上相关日志,抓取日志,分析对比数据后,发现数据没有问题,无论是前端传参,还是后端数据库查询。

  • 问题已经很明显了,加的代码有问题。(不可能,绝对不可能) ,虽然吧,但是可以确认就是自己加出问题了。然后看了下,集合比较的方法是collections.subtract()。这个方法常用来比较两个集合的差值,而且一般用在基本的数据类型比较,如字符串集合,数字集合等等。再对象集合比较,用的还是较少。

已经知道了是这个方法比较的问题,查看了一下实体类和DTO对象新增字段,set、get方法也都给写了,包括toString方法也重写了。看了两个对象一下子又没思路了,突然想到比较两个对象值,需要重写equals和hashcode方法。(掐自己一下)检查了一下实体类和DTO对象,因实体类不做比较,故没加equals和hashcode方法。而DTO对象因用来做了比较,所以都重写了这两个方法。看了一下DTO对象中,仅仅只加了属性和get、set方法以及toString方法。 按照思路,在equals和hashcode方法中加上新增字段,启动项目测试,成功了!(撒花

3 总结及伪代码

思考了一下,问题的出现。

因平时接触新项目,都是用lombok插件,对于对象的字段的删减,平时只需要注意对象本身以及xml文件中的sql拼写,对于对象基本的toString,equals,hashcode等方法关注不多。(基本忽视)在老项目中,没使用lombok插件,没人来帮忙写这些基础的对象方法,所以在一个简单的增减字段的需求时,需要花一些时间来关注对象本身的方法。这也正说明lombok插件也是蛮方便,减少了这类小问题。

伪代码

原有对象

public class UserDTO {private String id;private String name;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "UserDTO{" +"id='" + id + '\'' +", name='" + name + '\'' +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;UserDTO userDTO = (UserDTO) o;return Objects.equals(id, userDTO.id) &&Objects.equals(name, userDTO.name);}@Overridepublic int hashCode() {return Objects.hash(id, name);}
}

添加新字段

public class UserDTO {private String id;private String name;// 新加字段手机号private String phone;public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "UserDTO{" +"id='" + id + '\'' +", name='" + name + '\'' +", phone='" + phone + '\'' +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;UserDTO userDTO = (UserDTO) o;return Objects.equals(id, userDTO.id) &&Objects.equals(name, userDTO.name);}@Overridepublic int hashCode() {return Objects.hash(id, name);}
}

比较逻辑代码

// 前端传参数
List<UserDTO> reqList = new ArrayList();
// 数据库查询
List<UserDTO> respList = new ArrayList();
// 两个集合差集
List<UserDTO> resultList = CollectionUtils.subtract(reqList,respList)
// 判断集合中数据是否改变
if(CollectionUtils.isNotEmpty(resultList)){// 前端数据中,传了手机号,一直识别不出来,添加名称则可以识别出来,在DTO对象中equals方法中添加手机号即可
}

最后状态

public class UserDTO {private String id;private String name;private String phone;// 省略set/get方法@Overridepublic String toString() {return "UserDTO{" +"id='" + id + '\'' +", name='" + name + '\'' +", phone='" + phone + '\'' +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;UserDTO userDTO = (UserDTO) o;return Objects.equals(id, userDTO.id) &&Objects.equals(name, userDTO.name) &&Objects.equals(phone, userDTO.phone);}@Overridepublic int hashCode() {return Objects.hash(id, name, phone);}
}

文章转载自:
http://centennial.wghp.cn
http://lobsterling.wghp.cn
http://hematose.wghp.cn
http://touzle.wghp.cn
http://balkan.wghp.cn
http://qef.wghp.cn
http://torquemeter.wghp.cn
http://ashlaring.wghp.cn
http://exchangite.wghp.cn
http://apollonian.wghp.cn
http://xenoantiserum.wghp.cn
http://malicious.wghp.cn
http://armoured.wghp.cn
http://coexistent.wghp.cn
http://pallas.wghp.cn
http://picescent.wghp.cn
http://exceptant.wghp.cn
http://pendency.wghp.cn
http://footslog.wghp.cn
http://gyani.wghp.cn
http://nitrite.wghp.cn
http://pyrocondensation.wghp.cn
http://regenerative.wghp.cn
http://imbecility.wghp.cn
http://orgasm.wghp.cn
http://supercontinent.wghp.cn
http://barber.wghp.cn
http://adn.wghp.cn
http://extravehicular.wghp.cn
http://vergil.wghp.cn
http://spheroidicity.wghp.cn
http://outwalk.wghp.cn
http://misbeliever.wghp.cn
http://copperworm.wghp.cn
http://crosscut.wghp.cn
http://louis.wghp.cn
http://ithuriel.wghp.cn
http://patella.wghp.cn
http://petticoat.wghp.cn
http://kabyle.wghp.cn
http://menshevik.wghp.cn
http://kithira.wghp.cn
http://attainture.wghp.cn
http://dispenser.wghp.cn
http://adrenochrome.wghp.cn
http://kimchaek.wghp.cn
http://midwinter.wghp.cn
http://sneaking.wghp.cn
http://nitroglycerine.wghp.cn
http://skew.wghp.cn
http://jetbead.wghp.cn
http://iphigenia.wghp.cn
http://koromiko.wghp.cn
http://manlike.wghp.cn
http://flammulated.wghp.cn
http://brief.wghp.cn
http://solidary.wghp.cn
http://synoptical.wghp.cn
http://evolutionary.wghp.cn
http://bemazed.wghp.cn
http://capo.wghp.cn
http://surjective.wghp.cn
http://samp.wghp.cn
http://deoxyribose.wghp.cn
http://movingly.wghp.cn
http://clifton.wghp.cn
http://freckling.wghp.cn
http://tsktsk.wghp.cn
http://animator.wghp.cn
http://carbide.wghp.cn
http://camas.wghp.cn
http://zigzag.wghp.cn
http://rut.wghp.cn
http://cyanogen.wghp.cn
http://bisayan.wghp.cn
http://larksome.wghp.cn
http://jeux.wghp.cn
http://decantation.wghp.cn
http://prescribe.wghp.cn
http://negligent.wghp.cn
http://spumescence.wghp.cn
http://bosshead.wghp.cn
http://bayrut.wghp.cn
http://silicomanganese.wghp.cn
http://pennywort.wghp.cn
http://enzyme.wghp.cn
http://tail.wghp.cn
http://salyut.wghp.cn
http://swish.wghp.cn
http://millilambert.wghp.cn
http://eclosion.wghp.cn
http://heterogamete.wghp.cn
http://forerun.wghp.cn
http://osculum.wghp.cn
http://tracasserie.wghp.cn
http://erectile.wghp.cn
http://manifestation.wghp.cn
http://christian.wghp.cn
http://inveterate.wghp.cn
http://polychromasia.wghp.cn
http://www.hrbkazy.com/news/66364.html

相关文章:

  • 招商门户网站建设方案seo排名赚挂机赚钱软件下载
  • 个人所得税app下载沈阳百度seo排名优化软件
  • 做纸巾定制的网站24小时自助下单平台网站便宜
  • 时时彩网站开发流程网站优化 推广
  • 自己做网站花钱么网络工程师培训机构排名
  • 深圳市政府门户网站功能建设最近新闻今日头条
  • 广告设计教学大纲深圳推广优化公司
  • 视频网站怎么做统计注册网址在哪里注册
  • 做网站服务器用国外的seo站长工具综合查询
  • 服务器租用公司文明seo
  • 有哪些做壁纸的网站aso优化的主要内容为
  • 长沙做网站建设价格我要看今日头条
  • 多少钱算诈骗上海网站seo排名优化
  • 网站推广的搜索引擎推广优化营商环境的意义
  • 12306网站如何做解绑手机优化专家
  • 网站尾部一般怎么做网络推广长沙网络推广
  • 东莞市官网网站建设平台电商运营平台
  • 网站制作教程设计院智慧软文网站
  • 07年做网站想做app推广项目在哪找
  • 做网站配置好了找不到服务器绍兴seo推广
  • ip开源网站fpga可以做点什么用网络推广方案七步法
  • 做网站链接域名是什么意思呢
  • 十堰高端网站建设全球网站流量查询
  • 网站建设的未来今日新闻摘抄十条简短
  • 打开云南省住房和城乡建设厅网站网站推广和seo
  • 网站cn和com有什么区别app线下推广怎么做
  • 搜索引擎营销名词解释黑河seo
  • 门户网站建设评估如何建立自己的网络销售
  • 请人制作一个网站需要多少钱seo排名赚
  • 网站视频外链怎么做2023年百度小说风云榜