首页 > 代码库 > java easyreport 导入excel、 txt 数据关联集合置入对象(三)

java easyreport 导入excel、 txt 数据关联集合置入对象(三)

接上一节,获取的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匹配失败,对象不能加入集合中。

本文出自 “菜鸟博博” 博客,请务必保留此出处http://zhangxiao2.blog.51cto.com/6342789/1586224

java easyreport 导入excel、 txt 数据关联集合置入对象(三)