Hibernate框架的查询方式

2017-04-29  本文已影响16人  Michael_林

Hibernate框架的查询方式


Query查询接口

  1. 具体的查询代码如下
    1.查询所有记录
    /Query query = session.createQuery("from Customer");
    List<Customer> list = query.list();
    System.out.println(list);
    /

    2.条件查询:
    /Query query = session.createQuery("from Customer where name = ?");
    query.setString(0, "李健");
    List<Customer> list = query.list();
    System.out.println(list);
    /

    3.条件查询:
    /Query query = session.createQuery("from Customer where name = :aaa and age = :bbb");
    query.setString("aaa", "李健");
    query.setInteger("bbb", 38);
    List<Customer> list = query.list();
    System.out.println(list);
    /


Criteria查询接口(做条件查询非常合适)

  1. 具体的查询代码如下
    1.查询所有记录
    /Criteria criteria = session.createCriteria(Customer.class);
    List<Customer> list = criteria.list();
    System.out.println(list);
    /

    2.条件查询
    /Criteria criteria = session.createCriteria(Customer.class);
    criteria.add(Restrictions.eq("name", "李健"));
    List<Customer> list = criteria.list();
    System.out.println(list);
    /

    3.条件查询
    /Criteria criteria = session.createCriteria(Customer.class);
    criteria.add(Restrictions.eq("name", "李健"));
    criteria.add(Restrictions.eq("age", 38));
    List<Customer> list = criteria.list();
    System.out.println(list);
    /


上一篇下一篇

猜你喜欢

热点阅读