1. @Test
    2. public void testCount(){
    3. //1.获取entityManager对象
    4. EntityManager entityManager = JpaUtils.getEntityManager();
    5. //2.开启事务
    6. EntityTransaction transaction = entityManager.getTransaction();
    7. transaction.begin();
    8. //3.查询全部
    9. //i.根据jpql语句创建查询对象
    10. String jpql = "select count(custId) from Customer";
    11. Query query = entityManager.createQuery(jpql);
    12. //ii.对参数赋值
    13. //iii.发送查询,封装结果集
    14. /**
    15. * getResultList:直接将查询结果封装为list集合
    16. * getSingleResult:得到唯一的结果集
    17. */
    18. Object singleResult = query.getSingleResult();
    19. System.out.println(singleResult);
    20. //4.提交事务
    21. transaction.commit();
    22. //5.释放资源
    23. entityManager.close();
    24. }