1、返回所有列的数据
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//检索的学生名字和所在系
List<Student> list = session.createQuery("from Student").list();
for (Student student : list) {
//System.out.println(student.getSname()+"选了"+student.getStudcourses().size()+"门课");
if(student.getStudcourses().size()==0){
System.out.println(student.getSname()+"没有选课");
}else{
Set<Studcourse> set = student.getStudcourses();
for (Studcourse studcourse : set) {
System.out.println(student.getSname()+"选了"+studcourse.getCourse().getCname());
}
}
}
****************************************************************
①hql语句:from Student ===>Student是类名 , 并且返回所有列数据
②.list(); : 返回的是List,并且把每一条数据 封装成 对象 存储在 List 中
****************************************************************
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2、返回部分列的数据
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//检索的学生名字和所在系
List list = session.createQuery("select sname,sdept from Student").list();
for(int i=0; i<list.size(); i++){
/**
* 当只取部分属性时 不会封装成相应的domain对象
* 而是封装成了Object[]
* */
Object[] obj = (Object[]) list.get(i);
System.out.println(obj[0].toString() +" "+obj[1].toString());
}
System.out.println("***************");
//使用Iterator
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Object[] obj = (Object[]) iterator.next();
System.out.println(obj[0].toString() +" "+obj[1].toString());
}
****************************************************************
①select sname,sdept from Student ===> sname,sdept 是Student类的属性 , 返回部分列的数据
②当只取部分属性时 不会封装成相应的domain对象, 而是封装成了Object[]
****************************************************************
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3、返回单列的数据
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*当查询单列数据时、List里面存放的是Object*/
List<Object> list = session.createQuery("select sname from Student").list();
for (Object object : list) {
System.out.println("sname="+object.toString());
}
****************************************************************
①select sname from Student ===> sname 是Student类的属性 , 返回单列的数据
②当只取部分属性时 不会封装成相应的domain对象, 而是封装成了Object
****************************************************************
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++