标签:style blog io color ar os 使用 java sp
1、JPQL 语句查询,代码如下:
@Test public void query(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); // 命名参数查询或位参数查询 Query query = em.createQuery("select p from Person p p.id = ?1"); query.setParameter(1, 1); // 调用 getSingleResult() 方法,必须要有结果(也就是说数据库必须要有对应的数据),不然会保存 Person person = (Person)query.getSingleResult(); System.out.println(person.getName()); em.close(); factory.close(); }
2、删除查询,代码如下:
@Test public void deletequery(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // 命名参数查询或位参数查询 Query query = em.createQuery("delete from Person p where p.id = ?1"); query.setParameter(1, 1); query.executeUpdate(); em.getTransaction().commit(); em.close(); factory.close(); }
3、更新查询,代码如下:
@Test public void updatequery(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // 命名参数查询 Query query = em.createQuery("update Person p set name=:name where p.id = :id"); query.setParameter("name", "hwl"); query.setParameter("id", 3); query.executeUpdate(); em.getTransaction().commit(); em.close(); factory.close(); }
4、详细代码:
package learn.jpa.junit.test; import static org.junit.Assert.*; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.Query; import learn.jpa.bean.Person; import org.junit.Test; public class PersonTest { @Test public void save(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // 开启事务 em.persist(new Person("hwl")); em.getTransaction().commit(); em.close(); factory.close(); } /** * 查询 */ @Test public void getPerson(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); // 第一个参数表示 实体类 第二个参数表示 实体标识符 Person person = em.find(Person.class, 1); System.out.println(person.getName()); em.close(); factory.close(); } @Test public void getPerson2(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); // 第一个参数表示 实体类 第二个参数表示 实体标识符 // 调用这个方法时,并不会立即从数据库中得到数据,只是返回一个代理对象 Person person = em.getReference(Person.class, 1); // 当我们对代理对象的属性进行访问的时候,才会从数据库中获取数据 // 如果执行以上方法,数据库中没有对应的数据,是不会报错的,会在执行 person.getName() 时才异常 System.out.println(person.getName()); em.close(); factory.close(); } @Test public void updatePerson(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); // 实体管理器已于事务关联 em.getTransaction().begin(); // 查找出来后 person 对象在 CPU 时处于托管状态 // 这时调用实体类的 set 方法,可以对数据进行更改,但并不是立即更改,是放在 JDBC 的批处理交易库里面 Person person = em.find(Person.class, 1); person.setName("zxx"); // 调用事务提交后,会将数据同步到数据库中 em.getTransaction().commit(); em.close(); factory.close(); // 实体在 CPU 中有四种状态 //第一种 新建(new) 第二种 托管(manage) 第三种 游离 第四种 删除 } @Test public void updatePerson2(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Person person = em.find(Person.class, 1); em.clear(); // 将实体管理器中的所有实体状态变成游离状态 person.setName("zxx"); em.merge(person); // 将游离状态的数据更新同步到数据库 em.getTransaction().commit(); em.close(); factory.close(); } @Test public void delete(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Person person = em.find(Person.class, 1); em.remove(person); em.getTransaction().commit(); em.close(); factory.close(); } @Test public void query(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); // 命名参数查询或位参数查询 Query query = em.createQuery("select p from Person p p.id = ?1"); query.setParameter(1, 1); // 调用 getSingleResult() 方法,必须要有结果(也就是说数据库必须要有对应的数据),不然会保存 Person person = (Person)query.getSingleResult(); System.out.println(person.getName()); em.close(); factory.close(); } @Test public void deletequery(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // 命名参数查询或位参数查询 Query query = em.createQuery("delete from Person p where p.id = ?1"); query.setParameter(1, 1); query.executeUpdate(); em.getTransaction().commit(); em.close(); factory.close(); } @Test public void updatequery(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // 命名参数查询 Query query = em.createQuery("update Person p set name=:name where p.id = :id"); query.setParameter("name", "hwl"); query.setParameter("id", 3); query.executeUpdate(); em.getTransaction().commit(); em.close(); factory.close(); } }
标签:style blog io color ar os 使用 java sp
原文地址:http://www.cnblogs.com/hwlsniper/p/4083081.html