• HQL_返回全部列+部分列+单例_数据时、List中封装的不同



    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
    ****************************************************************
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  • 相关阅读:
    ObjectARX 打印常见问题
    【动态规划】数字游戏(game)
    容斥原理在错排问题中的应用
    在某宝上用python抢茅台
    【笔记】Vue ElementPlus Rule 数字验证
    分布式中间件Nginx(一)
    20192419万腾阳 汇编程序设计 前四章学习笔记
    20192419 202120222 《网络与系统攻防技术》实验一实验报告
    reset.css 和 normalize.css
    reset.css 和 normalize.css
  • 原文地址:https://www.cnblogs.com/qintangtao/p/2744813.html
Copyright © 2020-2023  润新知