数据:nation(id,name)
需求:通过重写equals和hashCode获取nation的id
1.nation实体类
package com.example.server.pojo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.util.Objects;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* <p>
*
* </p>
*
* @author jiangsanjin
* @since 2021-02-03
*/
@TableName("t_nation")
@ApiModel(value="Nation对象", description="")
public class Nation implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "id")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "民族")
@Excel(name = "民族")
private String name;
public Nation() {
}
public Nation(Integer id, String name) {
this.id = id;
this.name = name;
}
public Nation(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Nation)) return false;
Nation nation = (Nation) o;
return name.equals(nation.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Nation{" +
"id=" + id +
", name=" + name +
"}";
}
}
2.nation实体类重写hashCode和equals分析
public Nation() {
}
//传入name
public Nation(String name) {
this.name = name;
}
//根据name判断是否相等
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Nation)) return false;
Nation nation = (Nation) o;
return name.equals(nation.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
3.导入员工数据代码
@ApiOperation(value = "导入员工数据")
@ApiImplicitParams({@ApiImplicitParam(name="file",value = "上传文件",dataType = "MultipartFile")})
@PostMapping("/import")
public RespBean importEmployee(MultipartFile file){
ImportParams params = new ImportParams();
//去掉标题行
params.setTitleRows(1);
List<Nation> nations = nationService.list();
List<PoliticsStatus> politicsStatuses = politicsStatusService.list();
List<Position> positions = positionService.list();
List<Department> departments = departmentService.list();
List<Joblevel> joblevels = joblevelService.list();
try {
List<Employee> list = ExcelImportUtil.importExcel(file.getInputStream(), Employee.class, params);
list.forEach(employee ->{
//民族id
System.out.println(new Nation(employee.getNation().getName()));
employee.setNationId(nations.get(nations.indexOf(new Nation(employee.getNation().getName()))).getId());
//政治面貌id
employee.setPoliticId(politicsStatuses.get(politicsStatuses.indexOf(new PoliticsStatus(employee.getPoliticsStatus().getName()))).getId());
//部门id
employee.setDepartmentId(departments.get(departments.indexOf(new Department(employee.getDepartment().getName()))).getId());
//职称id
employee.setJobLevelId(joblevels.get(joblevels.indexOf(new Joblevel(employee.getJoblevel().getName()))).getId());
//职位id
employee.setPosId(positions.get(positions.indexOf(new Position(employee.getPosition().getName()))).getId());
});
if (employeeService.saveBatch(list)){
return RespBean.success("导入成功");
}
return RespBean.error("导入失败");
} catch (Exception e) {
e.printStackTrace();
}
return RespBean.error("导入失败");
}
4.分析
List<Nation> nations = nationService.list();//获取nation的所有数据
List<Employee> list = ExcelImportUtil.importExcel(file.getInputStream(), Employee.class, params);
list.forEach(employee ->{
employee.getNation().getName()//根据employee获取nation的名字
new Nation(employee.getNation().getName())//根据nation的名字创建nation对象
nations.indexOf(new Nation(employee.getNation().getName())))//hashCode和equals进行比较拿到下标
nations.get(nations.indexOf(new Nation(employee.getNation().getName())))//根据下标获取对应nation的数据
nations.get(nations.indexOf(new Nation(employee.getNation().getName()))).getId()//获取nation的id
employee.setNationId(nations.get(nations.indexOf(new Nation(employee.getNation().getName()))).getId());//存入employee对象
}