本文概述
- JPQL批量数据示例
- JPQL提取
- JPQL更新
- JPQL删除
JPQL批量数据示例 在此示例中, 我们将使用基本实体类(在本例中为StudentEntity.java), 并对它执行不同的操作。
- 在com.srcmini.jpa包下创建一个名为StudentEntity.java的实体类。
package com.srcmini.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文件中映射实体类和其他数据库配置。
<
persistence>
<
persistence-unit name="Student_details">
<
class>
com.srcmini.jpa.StudentEntity<
/class>
<
properties>
<
property name="javax.persistence.jdbc.driver" value="http://www.srcmini.com/com.mysql.jdbc.Driver"/>
<
property name="javax.persistence.jdbc.url" value="http://www.srcmini.com/jdbc:mysql://localhost:3306/studentdata"/>
<
property name="javax.persistence.jdbc.user" value="http://www.srcmini.com/root"/>
<
property name="javax.persistence.jdbc.password" value=""/>
<
property name="eclipselink.logging.level" value="http://www.srcmini.com/SEVERE"/>
<
property name="eclipselink.ddl-generation" value="http://www.srcmini.com/create-or-extend-tables"/>
<
/properties>
<
/persistence-unit>
<
/persistence>
- 现在, 我们可以对StudentEntity.java类执行以下任何操作。
FetchData.java
package com.srcmini.jpa.jpql;
import com.srcmini.jpa.StudentEntity;
import javax.persistence.*;
import java.util.*;
public class FetchData { 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 from StudentEntity s ");
@SuppressWarnings("unchecked")List<
StudentEntity>
list=(List<
StudentEntity>
)query.getResultList( );
System.out.print("s_id");
System.out.print("\t s_name");
System.out.println("\t s_age");
for( StudentEntity s:list ){System.out.print( s.getS_id( ));
System.out.print("\t" +s.getS_name( ));
System.out.print("\t" + s.getS_age( ));
System.out.println();
}em.getTransaction().commit();
em.close();
emf.close();
}}
输出:
文章图片
JPQL更新 在这里, 我们将更新数据库中的记录。
UpdateData.java
package com.srcmini.jpa.jpql;
import javax.persistence.*;
public class UpdataData { public static void main( String args[]) {EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );
EntityManager em = emf.createEntityManager();
em.getTransaction().begin( );
Query query = em.createQuery( "update StudentEntity SET s_age=25 where s_id>
103");
query.executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
}}
输出:
程序执行后, 下面的学生表将在MySQL工作台下生成。要获取数据, 请在MySQL中运行select * from student。
文章图片
JPQL删除 在这里, 我们将从数据库中删除特定的记录。
DeleteData.java
package com.srcmini.jpa.jpql;
import javax.persistence.*;
public class DeleteData { public static void main( String args[]) {EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );
EntityManager em = emf.createEntityManager();
em.getTransaction().begin( );
Query query = em.createQuery( "delete from StudentEntity where s_id=102");
query.executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
}}
输出:
【JPA JPQL批量数据操作】程序执行后, 下面的学生表将在MySQL工作台下生成。要获取数据, 请在MySQL中运行select * from student。
文章图片