First, create a Maven project
Second, implement CRUD
Creating Tools
package cn.kgc.utils; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class Jpautil { private static EntityManagerFactory factory; static { //根据Persistence创建 EntityManagerFactory对象 factory = Persistence.createEntityManagerFactory("myjpa"); } //获得EntityManager对象的方法 public static EntityManager getEntityManager(){ EntityManager entityManager = factory.createEntityManager(); return entityManager; } }
1, add data
persist () method for adding a data
// add data @Test public void firstTest () { // get the object by tools EntityTransaction the EntityManager entityManager = Jpautil.getEntityManager (); // get business objects EntityTransaction Transaction = entityManager.getTransaction (); // open things transaction.begin (); // create an entity class object, 10 pieces of information for ( int I = 0; I <10; I ++ ) { the Customer Customer = new new the Customer (); customer.setName ( "Liugao Yang" ); customer.setSex ("Male" ); customer.setAge ( 18 ); customer.setAddres ( "Zhengzhou City" ); // 5 Using Entitymanager objects persist ways to add data to the database EntityManager.persist (the Customer); } // submit things transaction.commit (); // close the connection entityManager.close (); // factory.close (); }
2, based on the id query data
find method
getReference () 方法
// According to query the data id // use the find method to query the database // features: // 1, the object is to query the current object itself itself // 2, sql statement will be sent when you call the find method // This method is called immediately loading @Test public void testFind () { // get entityManager core object is the object to operate the database the entityManager entityManager = Jpautil.getEntityManager (); // get EntityTransaction business objects the EntityTransaction EntityTransaction = entityManager.getTransaction (); // open things entityTransaction.begin (); // execute query method id = EntityManager.find the Customer the Customer (the Customer. Class , 3 ); System.out.println (the Customer); // submit things entityTransaction.commit (); // close the core classes, the release of resources entityManager.close (); } // according to query data id // use getReference method // features: // 1, the method of inquiry is a dynamic proxy object // 2, when performing Reference method does not print sql statement to query the database immediately, but call the query object will call sql statement, // ie, when to send sql statement to query the database with when // the way to becoming lazy loading or lazy loading @Test public void testReference () { // get entityManager of the object to the object is the core operation of the database the EntityManager entityManager = Jpautil.getEntityManager (); // get things entityTransaction target the EntityTransaction entityTransaction = entityManager.getTransaction (); // turn things entityTransaction.begin (); // execution the method id query the Customer the Customer = entityManager.getReference (the Customer. class , 1 ); System.out.println (the Customer); // submit things entityTransaction.commit (); // close the core classes, the release of resources entityManager.close ( ); }
3, the method for modifying data
After the merge () method for modifying data stored database
// modify data merge () save the modified data @Test public void testmerge () { // get entityManager core object is the object to operate the database the EntityManager entityManager = Jpautil.getEntityManager (); // get entityTransaction business objects EntityTransaction = the EntityTransaction entityManager.getTransaction (); // turn things entityTransaction.begin (); // execute query id method according to the Customer the Customer = entityManager.getReference (the Customer. class , 2 ); customer.setName ( "Cao Cao" ); entityManager .merge (customer); System.out.println (the Customer); // submit things entityTransaction.commit (); // close the core classes, the release of resources entityManager.close (); }
4, delete data
remove () method is used to delete database data
// delete data Case @Test public void testRemove () { // get entityManager of the object to the object is the core operation of the database the EntityManager entityManager = Jpautil.getEntityManager (); // get things entityTransaction target the EntityTransaction entityTransaction = entityManager.getTransaction (); // turn things entityTransaction.begin (); // execute query id method according to the Customer Customer1 = EntityManager.find (the Customer. class , 3 ); // delete operation the EntityManager.remove (Customer1); System.out.println ( customer1); // submit things entityTransaction.commit (); // close the core classes, the release of resources entityManager.close (); }
5, all queries based on jpql
// query all based jpql create @Test public void findAll () { // get the object entityManager the EntityManager entityManager = Jpautil.getEntityManager (); // based jpql Query object created from the get entityManager // SQL statement: "select * from Customer" Customer here refers to the table // JPQL statement: "from Customer" means to query all of the data entity class Customer, Customer's worth here is the entity class query query = entityManager.createQuery ( "from Customer" ); // getResultList () method: this method is a method query objects, the query returns a collection of data objects List <the Customer> = resultList query.getResultList (); for (Customer the Customer: resultList) { System.out.println (Customer); } //Close the connection entityManager.close (); }
6, based on jpql paging query
setFirstResult () represents the beginning of a query from the first few
setMaxResults () represents the number of page impressions
// set page information @Test public void fianAllWithPage () { // create entityManager the EntityManager entityManager = Jpautil.getEntityManager (); // get query object // "from Customer" means that all of the data entity class Customer queries, where the Customer entity class is worth query query = entityManager.createQuery ( "Customer from" ); // set page information query.setFirstResult (. 5); // represents a query from the first few start query.setMaxResults (10); // represents each the number of page impressions // get all the data query and returns the set list List <the Customer> = resultList query.getResultList (); for (Customer customer:resultList) { System.out.println(customer); } }
7, according to id query based jpql
// JPQL by id check @Test public void findJpqlid () { the EntityManager entityManager = Jpautil.getEntityManager (); // get query object // "from Customer the WHERE id =?" Representation of the object id of the query Customer Query query = entityManager. the createQuery ( "from the Customer WHERE ID =?" ); // set value placeholder. The first parameter is the position of the placeholder, the placeholder for the second parameter value query.setParameter (1,1 ); // perform queries the Customer custome = (the Customer) query.getSingleResult (); the System.out. the println (custome); // Close the resource entityManager.close (); }
8, jpql fuzzy query-based
// JPQL fuzzy query @Test public void findJpqlByName () { the EntityManager entityManager = Jpautil.getEntityManager (); // "?? From the Customer like Sex and the WHERE name like" means that the fuzzy query by name and gender Query query = entityManager. the createQuery ( "like and from the Customer WHERE name like Sex??" ); // set the parameters for the position of the first parameter placeholder, the placeholder for the second parameter value query.setParameter (1, "% Cao % " ); query.setParameter ( 2, '% M%" ); // execute the query List <the Customer> = resultList query.getResultList (); System.out.println (resultList); // close the resource entityManager.close(); }
9, ascending descending query based jpql
@Test public void findAllByOrder () { the EntityManager entityManager = Jpautil.getEntityManager (); Query Query = entityManager.createQuery ( "Order from the Customer ID by desc"); // desc for descending query, // Query Query entityManager.createQuery = ( "Order from the Customer ID by ASC"); // ASC for ascending query // getResultList () method returns a query List of all data objects set List <the Customer> = resultList query.getResultList (); for (Customer the Customer: resultList) { System.out.println (Customer); } entityManager.close (); }
10, a query based on aggregate functions jpql
// aggregate function @Test public void findAllCount () { the EntityManager entityManager = Jpautil.getEntityManager (); Query Query = entityManager.createQuery ( "SELECT COUNT (*) from Customer"); // lookup table a column how many Customer Information // this method indicates the number of statistics Object Customer = query.getSingleResult (); System.out.println (Customer); entityManager.close (); }