zoukankan      html  css  js  c++  java
  • jp QL入门案例-----查询所有

    注意:首先要引入依赖,然后要创建配置文件,最后创建实体类,这三项和jpa操作数据库是一样的

    一、查询所有

    @Test
    public void findall() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
    EntityManager entityManager = factory.createEntityManager();
    Query query = entityManager.createQuery("from Customer");
    List<Customer> resultList = query.getResultList();
    for (Customer result : resultList) {
    System.out.println(result);
    }
    }

    二、分页查询
    //分页查询
    @Test
    public void findByPage() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
    EntityManager entityManager = factory.createEntityManager();
    Query query = entityManager.createQuery("from Customer");
    query.setFirstResult(0);
    query.setMaxResults(5);
    List<Customer> resultList = query.getResultList();
    for (Customer result : resultList) {
    System.out.println(result);
    }
    }

    三、模糊查询
    //模糊查询
    @Test
    public void findByname(){
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
    EntityManager entityManager = factory.createEntityManager();
    Query query = entityManager.createQuery("from Customer where cust_name like ?");
    query.setParameter(1,"%金%");
    List<Customer> resultList = query.getResultList();
    for (Customer result:resultList){
    System.out.println(result);
    }

    }
    四、排序查询
    //排序查询
    @Test
    public void findByOrder(){
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
    EntityManager entityManager = factory.createEntityManager();
    Query query = entityManager.createQuery("from Customer order by cust_id desc");//asc是升序
    List<Customer> resultList = query.getResultList();
    for (Customer result:resultList){
    System.out.println(result);
    }
    }

    五、聚合函数查询
    //聚合函数查询
    @Test
    public void findBycount(){
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
    EntityManager entityManager = factory.createEntityManager();
    //查询总条数
    Query query = entityManager.createQuery("select count(*) from Customer");
    //查询id的总和
    entityManager.createQuery("select sum(cust_id) from Customer");
    Object singleResult = query.getSingleResult();
    System.out.println(singleResult);

    }

    六、条件查询
    //条件查询
    @Test
    public void findById() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
    EntityManager entityManager = factory.createEntityManager();
    Query query = entityManager.createQuery("from Customer where cust_id=?");
    query.setParameter(1, 3L);
    Customer singleResult = (Customer) query.getSingleResult();
    System.out.println(singleResult);
    entityManager.close();

    }
  • 相关阅读:
    SQL语句 基本查询
    NHibernate 映射基础(第三篇) 简单映射、联合主键
    NHibernate 数据查询之Linto to NHibernate (第八篇)
    NHibernate 组件基础 (第六篇)
    SQL Server聚合函数
    NHibernate 集合映射深入 (第五篇) <set>,<list>,<map>,<bag>
    2020年10月笔记
    亚马逊云服务器aws配置ssl https证书
    namecheap mx记录配置邮箱
    为 PHPer 准备的 Go 入门知识
  • 原文地址:https://www.cnblogs.com/Hubert-dzl/p/11640076.html
Copyright © 2011-2022 走看看