JPA 数据批量操作

在上一节中,我们只获取了单列。现在,我们将学习如何处理批量数据并执行相应的操作。

JPQL 批量数据操作示例

项目结构如下:

在本例中,我们将采用一个基本实体类(在本例中为 StudentEntity.java)并对其执行不同的操作。

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>

JPQL获取表所有数据

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 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 ");

          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 更新数据操作

下面的案例,我们将更新数据库中的记录。

package com.yiidian.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

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。

JPQL 删除数据操作

下面的案例,我们将从数据库中删除特定记录。

package com.yiidian.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

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。

热门文章

优秀文章