package
com.demo;
import
org.springframework.context.ApplicationContext;
import
com.abc.mapper.StudentMapper;
import
com.abc.mapper.TeacherMapper;
import
com.abc.domain.Teacher;
import
com.abc.domain.Student;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
java.util.List;
public
class
CollectionDemo
{
private
static
ApplicationContext ctx;
static
{
//在类路径下寻找resources/beans.xml文件
ctx =
new
ClassPathXmlApplicationContext(
"resources/beans.xml"
);
}
public
static
void
main(String[] args)
{
//从Spring容器中请求映射器
TeacherMapper mapper =
(TeacherMapper)ctx.getBean(
"myTeacherMapper"
);
Teacher teacher =
null
;
//查询教师分页信息
List<Teacher> teachers =
//以name字段升序排序,从第0条记录开始查询。
//查询2条记录
mapper.findTeacherByPage(
"name"
,
"asc"
,
0
,
2
);
if
(teachers ==
null
)
{
System.out.println(
"未找到相关教师信息。"
);
}
else
{
Object[] t = teachers.toArray();
System.out.println(
"**********************************************"
);
for
(
int
i =
0
; i < t.length; i++)
{
teacher = (Teacher)t[i];
System.out.println(
"教师姓名:"
+
" "
+ teacher.getName());
System.out.println(
"教师职称:"
+
" "
+ teacher.getTitle());
System.out.println(
"指导学生信息:"
);
//遍历指导的学生
for
(Student s : teacher.getSupStudents())
{
System.out.println( s.getName() +
" "
+ s.getGender()
+
" "
+ s.getGrade()
+
" "
+ s.getMajor());
}
System.out.println(
"**********************************************"
);
}
}
}
}
来源: http://www.bubuko.com/infodetail-2001010.html