接上一节,获取的Student关联的Teacher集合有且只有一个,如何置入多个呢。
自定义StudentMatcher实现MatchAbled方法
package matcher;
import java.util.List;
import java.util.Map;
import model.Student;
import com.easyReport.read.MatchAbled;
public class StudentMatcher implements MatchAbled<Student>{
@Override
public boolean matchRowBean(Student student, List<Student> students) {
if(!students.contains(student)){
return true;
}
Student stu = students.get(students.indexOf(student));
stu.getTeachers().addAll(student.getTeachers());
return false;
}
@Override
public boolean matchRowMap(Map<String, Object> studentMap,
List<Map<String, Object>> studentMaps) {
return true;
}
}
Student类generator hashcode和equals方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.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;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
导入数据
姓名 | 年龄 | 科目 | 分数 | 教师姓名 | 教师性别 |
王老五 | 12 | 语文 | 80 | 张三丰 | 男 |
王老五 | 12 | 语文 | 80 | 王重阳 | 男 |
王老五 | 12 | 语文 | 80 | 华佗 | 男 |
李老六 | 15 | 语文 | 76 | 华佗 | 男 |
李老六 | 15 | 语文 | 76 | 张无忌 | 男 |
BasicTest类
实现方法
//matcher匹配器
protected MatchAbled<T> refMatcher(){
return null;
}
AbstractExcelTest类构造Reader修改
protected ConvertReader buildReader() {
return new ExcelReader(new ReaderContext(bulidReadTemp()),refMatcher());
}
Excel导入Demo TestExcelMatchAble
package example;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import matcher.StudentMatcher;
import model.Student;
import model.Teacher;
import test.AbstractExcelTest;
import com.easyReport.read.MatchAbled;
import com.easyReport.read.temp.PropertyTemp;
import com.easyReport.read.temp.ReadTemp;
import formater.SexFormater;
public class TestExcelMatchAble extends AbstractExcelTest<Student> {
@Override
public void test() {
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(new File("testFile/test3.xls"));
testReadBean(fileInputStream);
// testReadMap(fileInputStream);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//重写方法实现学生匹配
@Override
protected MatchAbled<Student> refMatcher() {
return new StudentMatcher();
}
@Override
protected ReadTemp bulidReadTemp() {
ReadTemp readTemp = new ReadTemp(1);
readTemp.addPropertyTemp(new PropertyTemp("姓名", "name"));
readTemp.addPropertyTemp(new PropertyTemp("年龄", "age"));
readTemp.addPropertyTemp(new PropertyTemp("科目", "course.name"));
readTemp.addPropertyTemp(new PropertyTemp("分数", "course.score"));
readTemp.addPropertyTemp(new PropertyTemp("教师姓名", "teachers.name"));
readTemp.addPropertyTemp(new PropertyTemp("教师性别", "teachers.sex",new SexFormater()));
return readTemp;
}
//迭代并输出教师信息
@Override
protected void writeBeanList(List<Student> modelLists) {
System.out.println("--------------bean---------------");
for (Student s : modelLists) {
System.out.println(s.getName() + "|"
+ s.getAge()+s.getCourse().getName()+"|"+s.getCourse().getScore());
System.out.println("---------Teacher-------------------");
for(Teacher t : s.getTeachers()){
System.out.println(t);
}
}
}
@Override
protected void writeMapList(List<Map<String, Object>> mapLists) {
System.out.println("--------------map---------------");
for (Map<String, Object> m : mapLists) {
System.out.println(m.get("name") + "|" + m.get("age")+"|" + m.get("course.name")+"|" + m.get("course.score")+
"|"+m.get("teachers.name")+"|"+m.get("teachers.sex"));
}
}
}
运行结果:
--------------bean---------------
王老五|12语文|80.0|
---------Teacher-------------------
张三丰|M
王重阳|M
华佗|M
李老六|15语文|76.0|
---------Teacher-------------------
华佗|M
张无忌|M
MatchAble接口用于匹配对象,返回true匹配成功,对象加入到集合中,返回false匹配失败,对象不能加入集合中。