/** * Solve the waste of resources and time in entity manager factory * In the form of static code blocks, when a program first accesses this tool class, it creates a common entity manager factory object * * First access to getEntityManager method, create a factory object through static code block, and then call method to create an EntityManager object * Access the getEntityManager method for the second time, and directly create the EntityManager object through the nearly created factory object. */ public class jpaUtils { //jpa tool class private static EntityManagerFactory factory; static { //Load configuration file and create EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa"); } //Get EntityManager entity object public static EntityManager getEntityManager() { return factory.createEntityManager(); } }
Insert operation:
public void useJpaUtil(){ EntityManager entityManager = jpaUtils.getEntityManager(); //Get transaction object, open transaction EntityTransaction tx = entityManager.getTransaction();//Get transaction object tx.begin();//Open transaction // 4. Complete CRUD operation Customer customer = new Customer("20","Zhang Xi","Junior","male"); //Preservation entityManager.persist(customer); // Customer customer1 = entityManager.find(customer); //Submission of affairs tx.commit(); //Release resources entityManager.close(); }
Query operation: (two ways): query by id
Find: the query object is the current customer object itself. When the find method is called, the SQL statement will be sent to query the database
getReference: the object obtained is a dynamic agent object. When this method is called, the SQL statement will not be sent immediately to query the database. When the query result object is called, the SQL statement will be sent to query the database. When and when will the query be used?
public void testFind(){ EntityManager entityManager = jpaUtils.getEntityManager(); EntityTransaction entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); /** * find:Query users by id * class: The result of query data needs byte code of entity type wrapped * id:Primary key value of query * */ Customer customer = entityManager.find(Customer.class, 45); System.out.println(customer); entityTransaction.commit(); entityManager.close(); } public void getReference(){ EntityManager entityManager = jpaUtils.getEntityManager(); EntityTransaction entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); /** * getReference:Query users by id * class: The result of query data needs byte code of entity type wrapped * id:Primary key value of query * */ Customer customer = entityManager.getReference(Customer.class, 45); // List < customer > List = (list < customer >) entitymanager.find (customer. Class, "Zhang Xi"); System.out.println(customer); entityTransaction.commit(); entityManager.close(); }
Query operation: query all
/** * Query all * jpql; */ @Test public void findall(){ EntityManager entityManager = jpaUtils.getEntityManager(); EntityTransaction entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); String jpql = "from domain.Customer"; Query query = entityManager.createQuery(jpql);//Create Query query object Query object is the object to execute jpql //Send query and encapsulate results List list = query.getResultList(); for (Object obj : list){ System.out.println(obj); } entityTransaction.commit(); entityManager.close(); } /** * Sort query * Reverse query all users (sorted by id) * * sql:select * from student order by id desc * jpql:from Customer order by id desc */ @Test public void findalldesc(){ EntityManager entityManager = jpaUtils.getEntityManager(); EntityTransaction entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); String jpql = "from domain.Customer order by id desc"; Query query = entityManager.createQuery(jpql);//Create Query query object Query object is the object to execute jpql //Send query and encapsulate results List list = query.getResultList(); for (Object obj : list){ System.out.println(obj); } entityTransaction.commit(); entityManager.close(); } /** * Total number of customers * sql:select count(id) from student * jpql:select count(id) from Customer */ @Test public void gettotal(){ EntityManager entityManager = jpaUtils.getEntityManager(); EntityTransaction entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); String jpql = "select count(id) from Customer"; Query query = entityManager.createQuery(jpql);//Create Query query object Query object is the object to execute jpql //Send query and encapsulate results /** * getSingleResult:The only result set returned is an Object object. */ System.out.println(query.getSingleResult()); entityTransaction.commit(); entityManager.close(); } /** * Paging query * sql:select * from student limit ?,? * jpql: from Cuntomer */ @Test public void splidpage() { EntityManager entityManager = jpaUtils.getEntityManager(); EntityTransaction entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); String jpql = "from Customer"; Query query = entityManager.createQuery(jpql);//Create Query query object Query object is the object to execute jpql //Send query and encapsulate results query.setFirstResult(0); //Query from 0 does not contain 0 query.setMaxResults(2); //2 queries per time List list = query.getResultList(); for (Object obj : list) System.out.println(obj); /** * Assign values to parameters * Paging parameters * 1.Initial index * 2.Number of queries per page */ entityTransaction.commit(); entityManager.close(); } /** * Conditional query * Query customer name: query the customer name surnamed Zhang * sql:select * from student where name like ? * jpql: from Customer where name like ? */ @Test public void condition() { EntityManager entityManager = jpaUtils.getEntityManager(); EntityTransaction entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); String jpql = "from Customer where name like ?"; Query query = entityManager.createQuery(jpql);//Create Query query object Query object is the object to execute jpql /** * Assign values to parameters * setParameter * The first parameter is the placeholder position, starting from 1. The second parameter is the value. */ query.setParameter(1,"Zhang%"); List list = query.getResultList(); for (Object obj : list) System.out.println(obj); entityTransaction.commit(); entityManager.close(); }
jpql query:
- Create jpql query object
- Copy parameters
- Query and get the return result
Delete user: query before deleting
Query the user first and then transfer it to the user object for deletion
@org.junit.Test public void testremove() { //delete user EntityManager entityManager = jpaUtils.getEntityManager(); EntityTransaction entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); /** * Query the customer according to the id and pass in the user object for deletion. * */ Customer customer = entityManager.find(Customer.class, 1); entityManager.remove(customer); // System.out.println(customer); entityTransaction.commit(); entityManager.close(); }