jpa 查询详解

1.1.  一个简单的查询示例

PersonDaoTest.java

package com.morris.test;

 

import static org.junit.Assert.*;

 

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

import javax.persistence.Query;

 

import org.junit.AfterClass;

import org.junit.BeforeClass;

import org.junit.Test;

 

import com.morris.entity.Person;

 

public class PersonDaoTest {

      

       private static EntityManagerFactory entityManagerFactory;

      

       private static EntityManager entityManager;

 

       @BeforeClass

       public static void setUpBeforeClass() throws Exception {

             

              entityManagerFactory = Persistence.createEntityManagerFactory("jpaDemo");

             

              entityManager = entityManagerFactory.createEntityManager();

             

              entityManager.getTransaction().begin();

       }

 

       @Test

       public void findById() {

 

              Query query = entityManager.createQuery("select p from Person p where p.id = ?1");

             

              query.setParameter(1, 1);

             

              Person person = (Person) query.getSingleResult();

             

              System.out.println(person);

       }

      

       @AfterClass

       public static void tearDownAfterClass() throws Exception {

              entityManager.getTransaction().commit();

              entityManager.close();

              entityManagerFactory.close();

       }

}

1.2.  Query方法详解

1.2.1.  getSingleResult()

返回实体的List集合

1.2.2.  getResultList()

查询结果只有一个

1.2.3.  executeUpdate()

返回执行更新或删除影响的记录数

1.2.4.  setFirstResult

设置返回结果的开始记录数,用于分页查询

1.2.5.  setMaxResults

设置查询的最大记录数,用于分页查询.

1.3.  设置参数

1.3.1.  位置设置

Query query = entityManager.createQuery("select p from Person p where p.id = ?1");

      

       query.setParameter(1, 1);

1.3.2.  命名设置

Query query = entityManager.createQuery("select p from Person p where p.id = :id");

      

       query.setParameter(“id”, 1);

1.4.  Jpql查询

Jpql使用面向对象的方式书写sql语句.

Query query = entityManager.createQuery("select p from Person p where p.id = ?1");

      

       query.setParameter(1, 1);

      

       Person person = (Person) query.getSingleResult();

      

       System.out.println(person);

1.5.  本地查询

本地查询直接使用原生态的sql语句.

Query query = entityManager.createNativeQuery("select name from person where id=:id");

      

       query.setParameter("id", 1);

      

       String name = (String) query.getSingleResult();

      

       System.out.println(name);

 

1.6.  命名查询

命名查询得定义在实体中.

@NamedQuery(name="findById",query="select p from Person p where p.id = :id")

使用:

Query query = entityManager.createNamedQuery("findById");

      

       query.setParameter("id", 1);

      

       Person person =  (Person) query.getSingleResult();

      

       System.out.println(person);

1.7.  本地命名查询

本地命名查询跟命名查询一样定义在实体中,两者的name不能相同,因为调用的方式是一样的.

@NamedNativeQuery(name="findById2",query="select name from person where id=:id")

使用:

Query query = entityManager.createNamedQuery("findById2");

      

       query.setParameter("id", 1);

      

       String name = (String) query.getSingleResult();

      

       System.out.println(name);

 

 

你可能感兴趣的:(jpa,查询)