Java Class Libraries use the Jakarta Persistence API for data access (Example of Data Access with Jakarta Persistence in Java Class Libraries)
Use the Jakarta Persistence API in the Java class library for data access examples
Java Class Libraries (Java class libraries) are commonly used codes commonly used in Java development.The Jakarta Persistence API (Jakarta persistence API) is a standard API for data persistence in Java applications.This article will show how to use the Jakarta Persistence API in the Java class library to achieve data access.
First, we need to introduce the relevant dependence of the Jakarta Persistence API in the project.You can get the following dependencies to get the following dependencies by adding the following dependencies in the Maven or Gradle construction file of the project.
Maven dependency configuration:
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
Gradle dependency configuration:
groovy
implementation 'jakarta.persistence:jakarta.persistence-api:2.2.3'
implementation 'org.postgresql:postgresql:42.2.5'
After completing the dependency configuration, we can start using the Jakarta Persistence API for data access.
First of all, create a physical class to map the database table and field, and use the annotations of ``@Entity`,@Table`, and `@column` and other annotations:
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Id;
@Entity
@Table(name = "users")
public class User {
@Id
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
// omit the getter and setter method
}
Then create a DAO (DATA Access Object) class to process data access logic:
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
public class UserDao {
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence-unit");
public void saveUser(User user) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(user);
em.getTransaction().commit();
em.close();
}
public User getUserById(Long id) {
EntityManager em = emf.createEntityManager();
User user = em.find(User.class, id);
em.close();
return user;
}
// omit other data access methods
}
In the above code, you can obtain the `EntityManager` object by creating the` EntityManagerFactory` instance, and then use the transaction to perform the data access operation.
Finally, use the above DAO class to perform data access operations in the application:
public class MainClass {
public static void main(String[] args) {
UserDao userDao = new UserDao();
User user = new User();
user.setId(1L);
user.setName ("Zhang San");
userDao.saveUser(user);
User retrievedUser = userDao.getUserById(1L);
System.out.println("Retrieved user: " + retrievedUser.getName());
}
}
In the above code, we first create an `user` object and set the relevant attributes, and then save it into the database.Then, by calling the `GetuserByid` method, we can retrieve the corresponding user objects from the database and print it out.
Through the above example, we can understand how to use the Jakarta Persistence API in the Java library for data access.By using the Jakarta Persistence API reasonably, we can easily handle interactive operations with the database to improve the development efficiency and maintenance of applications.