JPA JPQL批量數據操作
在上一節中,我們只提取了單列的值。 現在,我們將學習如何處理批量數據並執行相應的操作。
假設當前student
表有如下數據 -
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`S_ID` int(11) NOT NULL,
`S_NAME` varchar(255) DEFAULT NULL,
`S_AGE` int(11) DEFAULT NULL,
PRIMARY KEY (`S_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('101', 'Gaurav', '24');
INSERT INTO `student` VALUES ('102', 'Rahul', '22');
INSERT INTO `student` VALUES ('103', 'Chris', '20');
INSERT INTO `student` VALUES ('104', 'Ronit', '26');
INSERT INTO `student` VALUES ('105', 'Roy', '21');
JPQL批量數據示例
在這個例子中,我們將採用一個基本的實體類(StudentEntity.java
)並對其執行不同的操作。
在com.yiibai.jpa
包下創建一個名爲StudentEntity.java
的實體類。
文件:StudentEntity.java -
package com.yiibai.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.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.yiibai.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>
在 StudentEntity.java 類上執行以下操作。
1. JPQL提取記錄
在這裏,我們將從數據庫中提取所有記錄。
文件:FetchData.java -
package com.yiibai.jpa.jpql;
import com.yiibai.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();
}
}
執行上面示例代碼,得到以下結果 -
s_id s_name s_age
101 Gaurav 24
102 Rahul 22
103 Chris 20
104 Ronit 26
105 Roy 21
2. JPQL更新記錄
在這裏,我們將更新數據庫中的記錄。
文件:UpdateData.java -
package com.yiibai.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工作臺下生成以下student
表。 要查看數據,請在MySQL中運行select * from student
。
mysql> select * from student;
+------+--------+-------+
| S_ID | S_NAME | S_AGE |
+------+--------+-------+
| 101 | Gaurav | 24 |
| 102 | Rahul | 22 |
| 103 | Chris | 20 |
| 104 | Ronit | 25 |
| 105 | Roy | 25 |
+------+--------+-------+
5 rows in set
3. JPQL刪除記錄
在這裏,我們將從數據庫中刪除特定的記錄。
文件:DeleteData.java -
package com.yiibai.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();
}
}
執行上面示例代碼後,在MySQL工作臺下生成以下student
表。 要查看數據,請在MySQL中運行select * from student
。如下結果 -
mysql> select * from student;
+------+--------+-------+
| S_ID | S_NAME | S_AGE |
+------+--------+-------+
| 101 | Gaurav | 24 |
| 103 | Chris | 20 |
| 104 | Ronit | 25 |
| 105 | Roy | 25 |
+------+--------+-------+
4 rows in set