Learn how to use the Gerald Jorm framework for rapid development in the Java class library
Learn how to use the Gerald Jorm framework in the Java library for rapid development
Overview:
Gerald Jorm is a lightweight Java object relationship mapping (ORM) framework, which is committed to simplifying the database access and operation of the Java library.This article will introduce how to use the Gerald Jorm framework to develop rapidly in the Java class library.
1. Installation and configuration Gerald Jorm framework
First, the Gerald Jorm framework needs to be introduced into the Java project.It can be implemented by Maven or directly download the jar file.After downloading, add the jar file to the project path.
2. Create a database physical class
Before using the Gerald Jorm framework, you need to create a database entity class, which corresponds to the table structure in the database.For example, create a physical class called User, which contains the user's name, age, and mailbox.The example code is as follows:
import org.gerald.jorm.annotations.Column;
import org.gerald.jorm.annotations.Entity;
import org.gerald.jorm.annotations.Id;
@Entity(tableName = "user")
public class User {
@Id(columnName = "id")
private int id;
@Column(columnName = "name")
private String name;
@Column(columnName = "age")
private int age;
@Column(columnName = "email")
private String email;
// Construct function, Getter, and Setter method omitted ...
}
In the physical class, the name and the mapping relationship of the database table by using the annotation of the Gerald Jorm framework.@Entity annotation is used to specify the table name,@ID annotation is used to specify the primary key column, and@column annotations are used to specify ordinary columns.
3. Database operation
It is very simple to use the Gerald Jorm framework for database operations.First of all, you need to create a database connection configuration file, which contains relevant information of the database connection, such as database URL, username and password.
For query operations, you can use the query sentence method provided by the Gerald Jorm framework to achieve.
import org.gerald.jorm.database.DBConnection;
import org.gerald.jorm.database.query.Query;
import org.gerald.jorm.exceptions.DBConnectionException;
import org.gerald.jorm.exceptions.DBQueryExecutionException;
import org.gerald.jorm.exceptions.DBTransactionException;
import org.gerald.jorm.utils.DBUtils;
import java.util.List;
public class Main {
public static void main(String[] args) {
try {
DBConnection dbConnection = DBUtils.getConnection();
// Query all users
List<User> users = Query.selectAll(dbConnection, User.class);
// Inquiry the user according to the condition
List<User> filteredUsers = Query.selectWhere(dbConnection, User.class, "age > ?", 18);
// Other database operations, such as insertion, update, deletion, etc.
dbConnection.commit();
dbConnection.close();
} catch (DBConnectionException | DBTransactionException | DBQueryExecutionException e) {
e.printStackTrace();
}
}
}
In the above code example, the database query operation is implemented using the Query class of the Gerald Jorm framework.The Selectwhere method allows to query data according to the condition, and the parameters are the placeholder to prevent SQL from being injected.
4. Advanced features
The Gerald Jorm framework also provides some advanced features, such as database management, associated inquiries, etc.You can understand and use these functions through the document and example code of the learning framework.
Summarize:
The Gerald Jorm framework is a simple and easy -to -use Java Orm framework that can help developers develop rapidly in the Java class library.By using the Gerald Jorm framework, database access and operation can be easily performed to improve development efficiency.I hope this article will help you learn how to use the Gerald Jorm framework.