JPA JPQL基本操作
JPQL允许我们创建静态和动态查询。 现在,我们将在下表中使用这两种查询来执行一些基本的JPQL操作。
JPQL动态查询示例
在这个例子中,我们将使用createQuery()
方法从数据库中获取单个列的值。
完整的目录结构如下 -
文件:StudentEntity.java -
package com.zyiz.jpa; import javax.persistence.*; @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.zyiz.jpa.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/testdb?serverTimezone=UTC" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="123456" /> <property name="eclipselink.logging.level" value="SEVERE" /> <property name="eclipselink.ddl-generation" value="create-or-extend-tables" /> </properties> </persistence-unit> </persistence>
文件:FetchColumn.java -
package com.zyiz.jpa.jpql; import javax.persistence.*; import java.util.*; 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("Student Name :"); for (String s : list) { System.out.println(s); } em.close(); emf.close(); } }
执行上面示例代码,得到以下结果 -
Student Name : Gaurav Rahul Chris Ronit Roy
JPQL静态查询示例
在这个例子中,我们将使用createNamedQuery()
方法从数据库中获取单个列的值。
文件:StudentEntity.java -
package com.zyiz.jpa; import javax.persistence.*; @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.java -
package com.zyiz.jpa.jpql; import javax.persistence.*; import java.util.*; import javax.persistence.*; import com.zyiz.jpa.StudentEntity; import java.util.*; 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("Student Name :"); for (StudentEntity s : list) { System.out.println(s.getS_name()); } em.close(); emf.close(); } }
执行上面示例代码,得到以下结果 -
上一篇:JPA JPQL简介
下一篇:JPA JPQL批量数据操作
分类导航
- Java教程
- Vim教程
- Swing教程
- Spring教程
- Spring Web Services教程
- Spring MVC教程
- Spring JDBC教程
- Spring Cloud教程
- Spring Boot教程
- Spring Boot CLI教程
- Spring Batch教程
- Spring AOP教程
- PDFBox教程
- JSP教程
- JSF教程
- JPA教程
- Java面向对象设计
- Java设计模式
- Java虚拟机教程
- Java泛型教程
- Java正则表达式教程
- Java数据类型教程
- Java并发编程教程
- Java密码学教程
- Java多线程教程
- Java国际化(i18n)教程
- JavaFX教程
- Java9教程
关注微信小程序
扫描二维码
程序员编程王