JPA JPQL基本操作

JPQL 允许我们创建静态和动态查询。现在,我们将使用下表中的两种类型的查询执行一些基本的 JPQL 操作。

JPQL 动态查询示例

在这个例子中,我们将使用createQuery()方法从数据库中获取单列。

项目结构如下:

StudentEntity实体类

package com.yiidian.pojo;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class StudentEntity {  
  
    @Id
    private int s_id;  
    private String s_name;  
    private int s_age;  
      
    public StudentEntity(int s_id, String s_name, int s_age) {  
        super();  
        this.s_id = s_id;  
        this.s_name = s_name;  
        this.s_age = s_age;  
    }  
  
    public StudentEntity() {  
        super();  
    }  
  
    public int getS_id() {  
        return s_id;  
    }  
  
    public void setS_id(int s_id) {  
        this.s_id = s_id;  
    }  
  
    public String getS_name() {  
        return s_name;  
    }  
  
    public void setS_name(String s_name) {  
        this.s_name = s_name;  
    }  
  
    public int getS_age() {  
        return s_age;  
    }  
  
    public void setS_age(int s_age) {  
        this.s_age = s_age;  
    }  
      
}  

persistence.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="Student_details">
        <class>com.yiidian.pojo.StudentEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.driver"
                      value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:mysql://localhost:3306/jpa?serverTimezone=UTC&amp;useSSL=false" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="eclipselink.logging.level" value="SEVERE" />
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
        </properties>
    </persistence-unit>

</persistence>

FetchColumn测试类

package com.yiidian.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.List;

public class FetchColumn {  
      
     public static void main( String args[]) {  
             
          EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );  
          EntityManager em = emf.createEntityManager();  
          em.getTransaction().begin( );  
                    
          Query query = em.createQuery("Select s.s_name from StudentEntity s");  
          @SuppressWarnings("unchecked")
          List<String> list =query.getResultList();  
          System.out.println("学生姓名 :");  
          for(String s:list) {
             System.out.println(s);
          }  
          
          em.close();  
          emf.close();    
     }
} 

输出结果为:

JPQL 静态查询示例

在这个例子中,我们将使用createNamedQuery()方法从数据库中获取单列。

StudentEntity实体类

package com.yiidian.pojo;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name="student")
@NamedQuery(name = "find name" , query = "Select s from StudentEntity s")
public class StudentEntity {  
  
    @Id
    private int s_id;  
    private String s_name;  
    private int s_age;  
      
    public StudentEntity(int s_id, String s_name, int s_age) {  
        super();  
        this.s_id = s_id;  
        this.s_name = s_name;  
        this.s_age = s_age;  
    }  
  
    public StudentEntity() {  
        super();  
    }  
  
    public int getS_id() {  
        return s_id;  
    }  
  
    public void setS_id(int s_id) {  
        this.s_id = s_id;  
    }  
  
    public String getS_name() {  
        return s_name;  
    }  
  
    public void setS_name(String s_name) {  
        this.s_name = s_name;  
    }  
  
    public int getS_age() {  
        return s_age;  
    }  
  
    public void setS_age(int s_age) {  
        this.s_age = s_age;  
    }  
      
}  

FetchColumn测试类

package com.yiidian.test;

import com.yiidian.pojo.StudentEntity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.List;

public class FetchColumn {

     public static void main( String args[]) {

          EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );
          EntityManager em = emf.createEntityManager();
          em.getTransaction().begin( );

         Query query = em.createNamedQuery("find name");
         @SuppressWarnings("unchecked")
         List<StudentEntity> list =query.getResultList();
         System.out.println("学生姓名 :");
         for(StudentEntity s:list) {
             System.out.println(s.getS_name());
         }
         em.close();
          emf.close();
     }
} 

输出结果为:

热门文章

优秀文章