Hibernate之Query接口的uniqueResult 方法

Hibernate之Query接口的uniqueResult 方法Hibernate之Query接口的uniqueResult方法

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

如果查询返回多个值用list()方法

public void testQuery(){  Configuration config = new Configuration().configure();  SessionFactory factory = config.buildSessionFactory(); //创建SessionFactory  Session session = factory.openSession();        //创建Session  session.beginTransaction();                     //开始事务    Query query = session.createQuery("from Student");  List list = query.list();  // 用list方法返回多个值  //List list = session.createQuery("from Student").list();  for(int i=0;i<list.size();i++){   Student student = (Student)list.get(i);   System.out.print(student.getId()+" ");   System.out.print(student.getName()+" ");   System.out.print(student.getSex()+" ");   System.out.print(student.getBirthday());   System.out.println();  }      session.getTransaction().commit();         //提交事务   session.close();                           //关闭Session }

只听到从架构师办公室传来架构君的声音:
都道晚凉天气好,有明月、怕登楼。有谁来对上联或下联?

当确定返回的实例只有一个或者null时 用uniqueResult()方法

此代码由Java架构师必看网-架构君整理
public void testGet(){  Configuration config = new Configuration().configure();  SessionFactory factory = config.buildSessionFactory();  Session session = factory.openSession();  session.beginTransaction();    Query query = session.createQuery("from Student s where s.id=?");  query.setString(0, "2");  Student student = (Student)query.uniqueResult();  //当确定返回的实例只有一个或者null时 用uniqueResult()方法  //Student student = (Student)session.createQuery("from Student s where s.id=?").setString(0,"5").uniqueResult();    System.out.println(student.getName());  System.out.println(student.getBirthday());     session.getTransaction().commit();  session.close(); }

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述
架构君码字不易,如需转载,请注明出处:https://javajgs.com/archives/209944
0
   

发表评论