我记得在《Effictive Java》这本书中,作者给出了建议,当重写equal时需要重写hashCode。具体原因忘了,这个几下,下次补上。今天主要经冲写equal和hashCode的场景。
使用MyBatis进行数据库的处理,在MyBatis中的sql如下:
SELECT
<include refid="Base_Column_List" />
FROM DM_INTEGRAIN_ZJXT.LSZL_YJYS
LEFT JOIN DM_INTEGRAIN_ZJXT.LSZL_YPZT ON LSZL_YJYS_KDH = LSZL_YPZT_KDH
WHERE LSZL_YJYS_CYYNM=#{userId} AND
LSZL_YJYS_ZZNM=#{zznm} AND
LSZL_YPZT_WCZT = #{wczt}
DM_INTEGRAIN_ZJXT这个东西是达梦数据库的模式名。貌似可以配置,暂时先不管了。
改sql执行结果如下:
很明显主从表格式,一对多,结果就会查出两主表数据相同的集合。由于只需要一条数据,所以需要处理下。
对此需要重写主表的equal和hashCode,如下:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((lszlZcysCfsj == null) ? 0 : lszlZcysCfsj.hashCode());
result = prime * result
+ ((lszlZcysCph == null) ? 0 : lszlZcysCph.hashCode());
result = prime * result
+ ((lszlZcysCyynm == null) ? 0 : lszlZcysCyynm.hashCode());
result = prime * result
+ ((lszlZcysLxfs == null) ? 0 : lszlZcysLxfs.hashCode());
result = prime * result
+ ((lszlZcysLxr == null) ? 0 : lszlZcysLxr.hashCode());
result = prime * result
+ ((lszlZcysNm == null) ? 0 : lszlZcysNm.hashCode());
result = prime * result
+ ((lszlZcysWczt == null) ? 0 : lszlZcysWczt.hashCode());
result = prime * result
+ ((lszlZcysYpfs == null) ? 0 : lszlZcysYpfs.hashCode());
result = prime * result
+ ((lszlZcysZznm == null) ? 0 : lszlZcysZznm.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Zcys other = (Zcys) obj;
if (lszlZcysCfsj == null) {
if (other.lszlZcysCfsj != null)
return false;
} else if (!lszlZcysCfsj.equals(other.lszlZcysCfsj))
return false;
if (lszlZcysCph == null) {
if (other.lszlZcysCph != null)
return false;
} else if (!lszlZcysCph.equals(other.lszlZcysCph))
return false;
if (lszlZcysCyynm == null) {
if (other.lszlZcysCyynm != null)
return false;
} else if (!lszlZcysCyynm.equals(other.lszlZcysCyynm))
return false;
if (lszlZcysLxfs == null) {
if (other.lszlZcysLxfs != null)
return false;
} else if (!lszlZcysLxfs.equals(other.lszlZcysLxfs))
return false;
if (lszlZcysLxr == null) {
if (other.lszlZcysLxr != null)
return false;
} else if (!lszlZcysLxr.equals(other.lszlZcysLxr))
return false;
if (lszlZcysNm == null) {
if (other.lszlZcysNm != null)
return false;
} else if (!lszlZcysNm.equals(other.lszlZcysNm))
return false;
if (lszlZcysWczt == null) {
if (other.lszlZcysWczt != null)
return false;
} else if (!lszlZcysWczt.equals(other.lszlZcysWczt))
return false;
if (lszlZcysYpfs == null) {
if (other.lszlZcysYpfs != null)
return false;
} else if (!lszlZcysYpfs.equals(other.lszlZcysYpfs))
return false;
if (lszlZcysZznm == null) {
if (other.lszlZcysZznm != null)
return false;
} else if (!lszlZcysZznm.equals(other.lszlZcysZznm))
return false;
return true;
}
代码评审说业务逻辑应该写在Service层,这里先写在Controller中了,这里仅附上关键代码:
//这样遍历,效率比较低,有待改善
List<Zcys> zcys = zcysService.getZcysList(userId, zznm, wczt);//这个是上边的sql返回的结果 zcys.empty(),判空
//System.out.println("专车运输的结果"+zcys);
//这里面的结果是不重复的。
List zcysList = new ArrayList<>();
for (Zcys zcysIterator:zcys){
if (inZcysList(zcysList,zcysIterator))
{
continue;
}
zcysList.add(zcysIterator);
}
private boolean inZcysList(List<Zcys> zcysList, Zcys zcysIterator) {
for (Zcys zcys:zcysList){
if (zcys.equals(zcysIterator)){
return true;
}
}
return false;
}