JPA插入实体

在JPA中,我们可以通过实体轻松地将数据插入到数据库中。 EntityManager提供persist()方法来插入数据记录。

JPA实体插入示例

在这里,我们将演示如何使用实现插入学生的记录。

创建一个JPA项目,其项目目录结构如下所示 -

这个例子包含以下步骤 -

第1步:com.zyiz.jpa.student包下创建一个名为StudentEntity.java的实体类,这个类包含以下几个属性:s_ids_names_age

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

package com.zyiz.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.zyiz.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.zyiz.jpa.persist包下创建一个名为PersistStudent.java的持久化类,以便将实体对象与数据保持一致。

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

package com.zyiz.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("zyiz");
        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创建实体

下一篇:JPA查找实体

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程