JPQL全称Java Persistence Query Language
package com.ytkj.test; com.ytk.utils.JpaUtils amount; import org.junit.Test; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; import javax.persistence.Query; import java.util.List; /** * Jpql test */ public class JpqlTest { /** * All inquiries */ @Test public void findAll(){ EntityManager entityManager = JpaUtils.getEntityManager(); // open affairs EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); // Create a query object manipulation entity classes instead of directly taking operation database String jpql="from Customer"; Query query = entityManager.createQuery(jpql); //Inquire List list = query.getResultList(); for (Object o : list) { System.out.println(o); } // 5. commit the transaction transaction.commit(); // 6 Release resources entityManager.close(); } /** * Sorting Query */ @Test public void findAllOrderBy(){ EntityManager entityManager = JpaUtils.getEntityManager(); // open affairs EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); // Create a query object manipulation entity classes instead of directly taking operation database String jpql="from Customer order by custId desc"; Query query = entityManager.createQuery(jpql); //Inquire List list = query.getResultList(); for (Object o : list) { System.out.println(o); } // 5. commit the transaction transaction.commit(); // 6 Release resources entityManager.close(); } /** * The total number of queries */ @Test public void getCount(){ EntityManager entityManager = JpaUtils.getEntityManager(); // open affairs EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); // Create a query object manipulation entity classes instead of directly taking operation database String jpql="select count(custId) from Customer "; Query query = entityManager.createQuery(jpql); //Inquire Object result = query.getSingleResult(); System.out.println(result); // 5. commit the transaction transaction.commit(); // 6 Release resources entityManager.close(); } /** * Paging query */ @Test public void getByPage(){ EntityManager entityManager = JpaUtils.getEntityManager(); // open affairs EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); // Create a query object manipulation entity classes instead of directly taking operation database pageno int = 0; int pageSize=2; String jpql=" from Customer"; Query query = entityManager.createQuery(jpql); // paging parameter assignment query.setFirstResult (pageno); query.setMaxResults(pageSize); //Inquire List list = query.getResultList(); for (Object o : list) { System.out.println(o); } // 5. commit the transaction transaction.commit(); // 6 Release resources entityManager.close(); } /** * Conditions inquiry */ @Test public void getByCondition(){ EntityManager entityManager = JpaUtils.getEntityManager(); // open affairs EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); // Create a query object manipulation entity classes instead of directly taking operation database String jpql=" from Customer where custName like ?"; Query query = entityManager.createQuery(jpql); // placeholders for parameter assignment query.setParameter(1,"zhe%"); //Inquire List list = query.getResultList(); for (Object o : list) { System.out.println(o); } // 5. commit the transaction transaction.commit(); // 6 Release resources entityManager.close(); } }