Master the best practice of Gerald Jorm framework in the Java class library
Gerald Jorm is an object relationship mapping (ORM) framework for the Java library.It helps developers to easily map and interact with data in the Java object with the data in the relationship database.This article will explore the best practice of Gerald Jorm in the Java class library and provide some Java code examples.
1. Introduce Gerald Jorm framework
First, we need to introduce the Gerald Jorm framework in the code.It can be achieved by adding corresponding dependencies to the project construction file.The following is an example configuration that introduces the Gerald Jorm framework in a Maven project:
<dependencies>
<dependency>
<groupId>com.geraldjorm</groupId>
<artifactId>geraldjorm</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
Second, define data model
Next, we need to define the Java class corresponding to the database table.Each class represents a list of a table, and the attributes of the class correspond to the list.By adding annotations to attributes, we can specify the mapping relationship.The following is a Java class definition of an example:
import com.geraldjorm.Column;
import com.geraldjorm.Entity;
import com.geraldjorm.Id;
@Entity(tableName = "users")
public class User {
@Id
private int id;
@Column(columnName = "name")
private String name;
@Column(columnName = "age")
private int age;
// Construct function, Getter, and Setter method omitted
}
In the above example, we use the@Entity` annotation to associate the `user` class with the database table` users`, and use the@column` annotation to specify which column of each attribute, `@ID`The annotation specifies the primary key column.
Third, perform database operations
After completing the definition of the data model, we can use Gerald Jorm for database operations.Here are some example operations:
1. Insert data:
import com.geraldjorm.GeraldJorm;
public class Main {
public static void main(String[] args) {
// Create Geraldjorm instance
GeraldJorm geraldJorm = new GeraldJorm();
// Create User object
User user = new User();
user.setName ("Zhang San");
user.setAge(25);
// Insert data
geraldJorm.insert(user);
}
}
2. Query data:
import com.geraldjorm.GeraldJorm;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create Geraldjorm instance
GeraldJorm geraldJorm = new GeraldJorm();
// Query all users
List<User> userList = geraldJorm.selectAll(User.class);
// Traversing output results
for (User user : userList) {
System.out.println ("id: + user.getid () +", name: " + user.getName () +", age: " + user.getage ());
}
}
}
3. Update data:
import com.geraldjorm.GeraldJorm;
public class Main {
public static void main(String[] args) {
// Create Geraldjorm instance
GeraldJorm geraldJorm = new GeraldJorm();
// Query the user with ID 1
User user = geraldJorm.selectById(User.class, 1);
// Update user information
user.setAge(30);
geraldJorm.update(user);
}
}
4. Delete data:
import com.geraldjorm.GeraldJorm;
public class Main {
public static void main(String[] args) {
// Create Geraldjorm instance
GeraldJorm geraldJorm = new GeraldJorm();
// Delete users with ID
geraldJorm.deleteById(User.class, 1);
}
}
Fourth, summary
By using the Gerald Jorm framework, we can easily implement the mapping and operation between the Java object and the database.This article introduces the best practice of Gerald Jorm in the Java class library and provides some example code, hoping to help readers.