OID查询
/** * OID 查询 * 根据ID查询 * 一对多查询 */ @Test public void test() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); // 查询客户所拥有的联系人;先查询客户 ID=1的客户;根据客户查询联系人 User user = session.get(User.class, 1); //获得联系人的set集合 Set<Link> link = user.getLink(); // 输出联系人个数 System.out.println(link.size()); ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } }
HQL查询所有
/** *查询所有 *hql */ @Test public void test2() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); // 查询所有客户 from 客户实体类名称 Query query = session.createQuery("from User"); List<User> list = query.list(); for (User user : list) { System.out.println(user.getuId()+"\t"+user.getuName()); } System.out.println("-------------------------"); Query query2 = session.createQuery("from Link"); List<Link> list2 = query2.list(); for (Link link : list2) { System.out.println(link.getcId()+"\t"+link.getcName()); } ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } }
Hql条件查询
@Test public void test2() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //创建query对象 from Cuastomer where user_id=? and user_name=? Query query = session.createQuery("from User c where c.uId=? and c.uName=?"); /** * 向占位符里填写数据;占位符下标从0开始 * 第一个参数为下标 * 第二个参数为值 */ query.setParameter(0, 1); query.setParameter(1, "张三"); List<User> list = query.list(); for (User user : list) { System.out.println(user.getuTel()); } ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } }
hql模糊查询
@Test public void test2() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //创建query对象 Query query = session.createQuery("from User c where c.uName like ?"); /** * 向占位符里填写数据;占位符下标从0开始 * 第一个参数为下标 * 第二个参数为值 */ query.setParameter(0, "%张%"); List<User> list = query.list(); for (User user : list) { System.out.println(user.getuTel()); } ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } }
hql排序
@Test public void test2() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //创建query对象 desc 倒序 asc正序 Query query = session.createQuery("from User order by uId asc"); List<User> list = query.list(); for (User user : list) { System.out.println(user.getuId()); } ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } }
HQL分页
@Test public void test2() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //创建query对象 Query query = session.createQuery("from User"); query.setFetchSize(0);//开始位置 query.setMaxResults(3); //每页显示个数 List<User> list = query.list(); for (User user : list) { System.out.println(user.getuId()); } ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } }
hql查询某个字段
@Test public void test2() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //创建query对象 Query query = session.createQuery("select uId from User"); List<Object> list = query.list(); for (Object object : list) { System.out.println(object); } ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } }
hql函数查询
@Test public void test2() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //创建query对象 Query query = session.createQuery("select count(*) from User"); Object obj = query.uniqueResult(); Long lbj = (long) obj; int count = lbj.intValue(); System.out.println(count); ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } }
时间: 2024-11-07 00:49:04