QBC  参考:(Hibernate的QBC查询)

    //is empty and is not empty
@Test
public void testQBC(){
Session session = sf.getCurrentSession();
session.beginTransaction();
//criterion 标准/准则/规范
Criteria c = session.createCriteria(Topic.class)//from Topic
.add(Restrictions.gt("id", 2)) //greater than = id > 2
.add(Restrictions.lt("id", 8)) //less than = id < 8
.add(Restrictions.like("title", "t_"))
.createCriteria("category")
.add(Restrictions.between("id", 0, 5)) //category.id >= 0 and category.id <= 5
;
//DetachedCriteria for(Object o : c.list()){
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle() );
}
session.getTransaction().commit();
}

QBE   参考:(hibernate的QBE和QBC) 和 (Hibernate 查询方式(HQL/QBC/QBE)汇总)

     //is empty and is not empty
//QBC Query By Criteria
//QBE Query By Example
@Test
public void testQBE(){
Session session = sf.getCurrentSession();
session.beginTransaction(); Topic tExample = new Topic();
tExample.setTitle("T_");
Example e = Example.create(tExample)
.ignoreCase().enableLike();//构建 example Criteria c = session.createCriteria(Topic.class)
.add(Restrictions.gt("id", 3))
.add(Restrictions.lt("id", 8))
.add(e)
; for(Object o : c.list()){
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle() );
}
session.getTransaction().commit();
}

jar包链接: https://pan.baidu.com/s/1kVbVPmZ 密码: 384f

QBC链接: https://pan.baidu.com/s/1dFEHIYt 密码: nfms

QBE链接: https://pan.baidu.com/s/1i4JJa0d 密码: 2zby

05-08 08:18