JPA插入實體

在JPA中,我們可以通過實體輕鬆地將數據插入到數據庫中。 EntityManager提供persist()方法來插入數據記錄。

JPA實體插入示例

在這裏,我們將演示如何使用實現插入學生的記錄。

創建一個JPA項目,其項目目錄結構如下所示 -
JPA插入實體

這個例子包含以下步驟 -

第1步:com.yiibai.jpa.student包下創建一個名爲StudentEntity.java的實體類,這個類包含以下幾個屬性:s_ids_names_age

文件:StudentEntity.java 的代碼如下 -

package com.yiibai.jpa.student;

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;
    }

}

第2步: 現在,將實體類和其他數據庫配置映射到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.student.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>

第3步:com.yiibai.jpa.persist包下創建一個名爲PersistStudent.java的持久化類,以便將實體對象與數據保持一致。

文件:PersistStudent.java 的代碼如下 -

package com.yiibai.jpa.student;

import javax.persistence.*;

public class PersistStudent {

    public static void main(String args[]) {

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

        em.getTransaction().begin();

        StudentEntity s1 = new StudentEntity();
        s1.setS_id(1001);
        s1.setS_name("Maxsu");
        s1.setS_age(26);

        StudentEntity s2 = new StudentEntity();
        s2.setS_id(1102);
        s2.setS_name("Yiibai");
        s2.setS_age(23);

        StudentEntity s3 = new StudentEntity();
        s3.setS_id(1003);
        s3.setS_name("James");
        s3.setS_age(28);

        em.persist(s1);
        em.persist(s2);
        em.persist(s3);

        em.getTransaction().commit();

        em.close();
        emf.close();

        System.out.println("All job done.");

    }
}

執行上面程序後,student表在MySQL工作臺下生成。此表包含學生的詳細信息。要獲取數據,請在MySQL控制檯執行select * from student查詢。得到以下結果 -

JPA插入實體

0 條評論,你可以發表評論,我們會進行改進
Comment author placeholder