jpa (java persistence api) 是java ee 5.0引入的一个orm规范,目的是为了简化对象和关系数据库的映射,帮助java开发者更轻松的将java对象持久化到关系数据库中。jpa通过抽象数据的概念,将java对象和关系数据库之间的映射隐藏起来,开发者可以专注于编写业务代码,而不需要关注数据的存储细节。
在本篇文章中,我们将介绍如何使用JPA技术将Java对象持久化到MySQL数据库中进行存储。
首先,我们需要先定义一个数据模型,例如一个简单的学生类。
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Integer age;
// 省略构造函数、getter和setter方法
}上述代码中,我们使用了JPA的注解来定义一个实体类,其中@Entity注解用于标注这个类是一个JPA实体类,@Id注解表示该字段为主键,@GeneratedValue注解表示该字段为自动生成的主键,@Column注解用于指定该字段对应的数据库列信息。
接下来,在我们的项目中需要使用JPA提供的EntityManager实例来进行实体类的操作。在使用JPA前,我们需要先在配置文件中指定数据源和JPA相关的配置信息。
立即学习“Java免费学习笔记(深入)”;
org.hibernate.ejb.HibernatePersistence com.example.Student
上述配置文件中,我们使用的是Hibernate提供的实现,并指定了我们的数据源和JPA相关的配置信息。
然后,在我们的Java代码中,通过EntityManager实例来进行实体类的操作。
public class StudentManager {
private EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU");
public void save(Student student) {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
em.persist(student);
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
}
public Student getById(Long id) {
EntityManager em = emf.createEntityManager();
try {
return em.find(Student.class, id);
} finally {
em.close();
}
}
public List getAll() {
EntityManager em = emf.createEntityManager();
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Student.class);
Root rootEntry = cq.from(Student.class);
CriteriaQuery all = cq.select(rootEntry);
TypedQuery allQuery = em.createQuery(all);
return allQuery.getResultList();
} finally {
em.close();
}
}
public void update(Student student) {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
em.merge(student);
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
}
public void deleteById(Long id) {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Student student = em.find(Student.class, id);
em.remove(student);
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
}
} 上述代码中,我们创建了一个StudentManager类来进行实体类的操作,其中通过EntityManagerFactory实例创建了EntityManager实例,并通过操作该实例来实现实体类的C(R)UD操作。
最后,我们可以通过如下的代码来测试我们的代码:
public static void main(String[] args) {
StudentManager studentManager = new StudentManager();
Student s1 = new Student("Tom", 18);
studentManager.save(s1);
Student s2 = studentManager.getById(1L);
System.out.println(s2.getName()); // 输出:Tom
List students = studentManager.getAll();
System.out.println(students.size()); // 输出:1
s2.setName("Jerry");
studentManager.update(s2);
s2 = studentManager.getById(1L);
System.out.println(s2.getName()); // 输出:Jerry
studentManager.deleteById(1L);
students = studentManager.getAll();
System.out.println(students.size()); // 输出:0
} 通过上述代码,我们可以看到JPA在持久化Java对象时确实提供了非常方便和易用的接口。开发者只需要通过简单的注解来定义实体类,配置数据源和JPA信息,就能够直接将Java对象持久化到关系数据库中进行存储。这大大降低了开发者的编码量,提高了开发效率,也避免了可能因为手写SQL语句带来的安全隐患。











