一 HQL查询
1 一次存1个客户,10个联系人。执行三次,存3个客户,30个联系人,为分页查询做准备
@Test //来3个客户,30个联系人 public void demo1(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); Customer customer=new Customer(); customer.setCust_name("小白"); for(int i=0;i<10;i++){ LinkMan linkMan=new LinkMan(); linkMan.setLkm_name("白白"+i); linkMan.setCustomer(customer); customer.getSets().add(linkMan); session.save(linkMan); } session.save(customer); tx.commit(); }
2 HQL的简单查询
@Test //HQL简单查询,一个简单的查询 public void demo2(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); Query query = session.createQuery("from Customer c");//这个Customer是类名 List<Customer> list = query.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); }
3 HQL排序查询,降序查询
@Test //HQL排序查询,降序 public void demo3(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); //升序:asc(默认) 降序:desc Query query = session.createQuery("from Customer order by cust_id desc");//这个Customer是类名 List<Customer> list = query.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); }
4 HQL的条件查询
@Test //HQL public void demo4(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); //只有一个条件 /*Query query = session.createQuery("from Customer where cust_name=?");//这个Customer是类名 query.setParameter(0, "小花");*/ //多个条件查询 /*Query query = session.createQuery("from Customer where cust_source=? and cust_name like ?"); query.setParameter(0, "朋友介绍"); query.setParameter(1, "%白");*/ //二 按名称绑定 Query query=session.createQuery("from Customer where cust_source = :a and cust_name like :b"); query.setParameter("a","网络推广"); query.setParameter("b","%紫"); List<Customer> list = query.list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); }
5 HQL的投影查询
@Test /** * 投影查询 */ public void demo5(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); //投影查询 单个属性 /*List<Object> list =session.createQuery("select c.cust_name from Customer c").list(); for (Object object : list) { System.out.println(object); }*/ //这里会包含多种类型,用数组接收,查到两列的值了 /*List<Object[]> list=session.createQuery("select c.cust_name,c.cust_source from Customer c").list(); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); }*/ //查询多个属性,封装到对象里面 List<Customer> list=session.createQuery("select new Customer(cust_id,cust_name,cust_source) from Customer").list(); for (Customer customer : list) { System.out.println(customer); } tx.commit(); }
6 HQL的分页查询
@Test /** * 分页查询 */ public void demo6(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); Query query = session.createQuery("from LinkMan"); query.setFirstResult(0); query.setMaxResults(3);//每页显示的条数 List<LinkMan> list = query.list(); for (LinkMan linkMan : list) { //要在LinkMan实体类中加上toString System.out.println(linkMan); } tx.commit(); }
7 HQL的统计查询
@Test public void demo8(){ //根据客户来源来统计它的个数 Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); //既有String类型又有Long类型,用object类型接收 // List<Object[]> list=session.createQuery("select cust_source,count(*) from Customer group by cust_source").list(); //根据客户来源统计个数大于等于2的 List<Object[]> list=session.createQuery("select cust_source,count(*) from Customer group by cust_source having count(*)>=2").list(); for (Object[] objects:list) { System.out.println(Arrays.toString(objects)); } tx.commit(); }
8 HQL的多表查询
@Test /** * HQL的多表查询 */ public void demo9(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); /*List<Object[]> list = session.createQuery("from Customer c inner join c.sets").list(); for (Object[] obj: list ) { System.out.println(Arrays.toString(obj)); }*/ //迫切内链接 List<Customer> list = session.createQuery("select distinct c from Customer c inner join fetch c.sets").list(); for (Customer customers: list ) { System.out.println(customers); } tx.commit(); }
二 QBC查询
1 QBC的简单查询
@Test /** * 简单查询 */ public void demo1(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); Criteria criteria= session.createCriteria(Customer.class); List<Customer> list = criteria.list(); for(Customer customer:list){ System.out.println(customer); } tx.commit(); }
2 QBC的排序查询
@Test /** * 排序查询 */ public void demo2(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); criteria.addOrder(Order.desc("cust_id"));//asc升序 desc降序 List<Customer> list = criteria.list(); for(Customer customer:list){ System.out.println(customer); } tx.commit(); }
3 QBC的分页查询
@Test /** * 分页查询 */ public void demo3(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); Criteria criteria = session.createCriteria(LinkMan.class); criteria.setFirstResult(0); criteria.setMaxResults(10); List<LinkMan> list = criteria.list(); for(LinkMan linkMan:list){ System.out.println(linkMan); } tx.commit(); }
4 QBC的条件查询
@Test /** * 条件查询 */ public void demo4(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); /** * = eq * > gt * >= ge * < lt * <= le * <> ne * like */ criteria.add(Restrictions.eq("cust_source","广告")); criteria.add(Restrictions.like("cust_name","%白")); List<Customer> list = criteria.list(); for(Customer customer:list){ System.out.println(customer); } tx.commit(); }
5 QBC的统计查询
@Test /** * 统计查询, 一般还是用HQL的,QBC的不常用 */ public void demo5(){ Session session=HibernateUtils.getCurrentSession(); Transaction tx=session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); criteria.setProjection(Projections.rowCount()); Long result= (Long)criteria.uniqueResult(); System.out.println(result); tx.commit(); }
6 QBC的离线条件查询
@Test /** * 离线条件查询,在后期的案例中会省很大的时间(相比原来拼接sql语句) */ public void demo6(){ DetachedCriteria dc = DetachedCriteria.forClass(Customer.class); Session session = HibernateUtils.getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = dc.getExecutableCriteria(session); List<Customer> list = criteria.list(); for (Customer customer:list ) { System.out.println(customer); } tx.commit(); }
三 抓取策略
抓取策略后续更新!