Spring JPA 的jpql复杂查询

Spring JPA 的jpql复杂查询

SQL:查询的是表和表中的字段

jpql:查询的是实体类和实体类中的属性

工具类:
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JpaUtils {
    private static EntityManagerFactory factory;

    static {
        factory = Persistence.createEntityManagerFactory("myJpa");
    }

    //获取实体管理器对象
    public static EntityManager getEntityManager(){
        return factory.createEntityManager();
    }
}
  1. 查询全部

    /*
        * 查询全部
        * jpql: from Customer
        * sql:select * from customer
        * */
    @Test
    public void testFindAll(){
        //获取EntityManager对象
        EntityManager em = JpaUtils.getEntityManager();
        //获取事务
        EntityTransaction tx = em.getTransaction();
        //开启事务
        tx.begin();
        //查询
        String jpql = "from Customer";
        Query query = em.createQuery(jpql);
        //发送查询,并封装结果集
        List list = query.getResultList();
        System.out.println(list);
        //提交事务
        tx.commit();
        em.close();
    }
    
  2. 分页查询

    /*
         * 分页查询
         * jpql:from Customer
         * sql:select * from customer limit ?,?
         * */
    @Test
    public void testPage(){
        //获取EntityManager对象
        EntityManager em = JpaUtils.getEntityManager();
        //获取事务
        EntityTransaction tx = em.getTransaction();
        //开启事务
        tx.begin();
        //根据jpql语句创建Query查询对象
        String jpql = "from Customer";
        Query query = em.createQuery(jpql);
        //对参数进行赋值--分页参数
        //起始索引
        query.setFirstResult(0);
        //每页条数
        query.setMaxResults(2);
    
        //发送查询,
        List result = query.getResultList();
        System.out.println(result);
        //提交事务
        tx.commit();
        em.close();
    
    }
    
  3. 统计查询

    @Test
    public void testCount(){
        //获取EntityManager对象
        EntityManager em = JpaUtils.getEntityManager();
        //获取事务
        EntityTransaction tx = em.getTransaction();
        //开启事务
        tx.begin();
        //查询
        String jpql = "select count(id) from Customer";
        Query query = em.createQuery(jpql);
        //发送查询,获取唯一的结果
        Object result = query.getSingleResult();
        System.out.println(result);
        //提交事务
        tx.commit();
        em.close();
    
    }
    
  4. 条件查询

    /*
         * 条件查询
         * jpql:from Customer where name = ?(name是实体类的属性)
         * sql:select * from customer where name = ?
         * */
    @Test
    public void testCondition(){
        //获取EntityManager对象
        EntityManager em = JpaUtils.getEntityManager();
        //获取事务
        EntityTransaction tx = em.getTransaction();
        //开启事务
        tx.begin();
        //根据jpql语句创建Query查询对象
        String jpql = "from Customer where name = ?";
        Query query = em.createQuery(jpql);
        //对参数进行赋值--对占位符赋值(参数1:占位符的索引,从1开始;参数2:占位符的值)
        query.setParameter(1,"黑马");
    
        //发送查询,
        List result = query.getResultList();
        System.out.println(result);
        //提交事务
        tx.commit();
        em.close();
    
    }
    
    
  5. 排序

    /*
         * 倒序查询(根据id倒序,默认是升序)
         * jpql: from Customer order by id desc(这个id是实体类的id)
         * sql:select * from customer order by id desc
         * */
    @Test
    public void testOrders(){
        //获取EntityManager对象
        EntityManager em = JpaUtils.getEntityManager();
        //获取事务
        EntityTransaction tx = em.getTransaction();
        //开启事务
        tx.begin();
        //查询
        String jpql = "from Customer order by id desc";
        Query query = em.createQuery(jpql);
        //发送查询,并封装结果集
        List list = query.getResultList();
        System.out.println(list);
        //提交事务
        tx.commit();
        em.close();
    
    }
    

你可能感兴趣的:(SpringDataJPA)