JPA中的复杂查询

JPQL全称Java Persistence Query Language

基于首次在EJB2.0中引入的EJB查询语言(EJB QL),Java持久化查询语言(JPQL)是一种可移植的查询语言,旨在以面向对象表达式语言的表达式,将SQL语法和简单查询语义绑定在一起,使用这种语言编写的查询是可移植的,可以被编译成所有主流数据库服务器上的SQL。

其特征与原生SQL语句类似,并且完全面向对象,通过类名和属性访问,而不是表名和表的属性。

查询全部:

/**
     * 查询全部
     */
    @Test
    public void testFindAll() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJPA");
        EntityManager em = factory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();

        Query query = em.createQuery("from Customer");
        List list = query.getResultList();
        for (Object o : list) {
            System.out.println(o);
        }

        tx.commit();
        em.close();
        factory.close();
    }
倒序查询客户:
/**
     * 根据id倒序查询客户
     */
    @Test
    public void testFindAllDesc() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJPA");
        EntityManager em = factory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();

        Query query = em.createQuery("from Customer order by custId desc");
        List list = query.getResultList();
        for (Object o : list) {
            System.out.println(o);
        }

        tx.commit();
        em.close();
        factory.close();
    }

统计客户数量:

/**
     * 统计客户数量
     */
    @Test
    public void testFindCount() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJPA");
        EntityManager em = factory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();

        Query query = em.createQuery("select count(custId) from Customer");
        Object o = query.getSingleResult();
        System.out.println(o);

        tx.commit();
        em.close();
        factory.close();
    }

分页查询:

/**
     * 分页查询
     */
    @Test
    public void testFindPage() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJPA");
        EntityManager em = factory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();

        Query query = em.createQuery("from Customer");
        query.setFirstResult(0);
        query.setMaxResults(2);
        List list = query.getResultList();
        for (Object o : list) {
            System.out.println(o);
        }

        tx.commit();
        em.close();
        factory.close();
    }

条件查询:

/**
     * 条件查询
     */
    @Test
    public void testCondition() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJPA");
        EntityManager em = factory.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();

        Query query = em.createQuery("from Customer where custName like ?");
        query.setParameter(1, "%王%");
        List list = query.getResultList();
        for (Object o : list) {
            System.out.println(o);
        }

        tx.commit();
        em.close();
        factory.close();
    }

 

posted @ 2020-02-27 13:30  糖不甜,盐不咸  阅读(235)  评论(0编辑  收藏  举报