Demystifying the technical principles of the Genormous framework in the Java class library
Demystifying the technical principles of the Genormous framework in the Java class library
Genormous is a class library and framework for Java, which aims to simplify database access and operation.The framework is based on the Java Persistence API (JPA) and Hibernate. It provides a simpler and more convenient way to manage database and perform CRUD operations.This article will reveal the technical principles of the Genormous framework, and bring some Java code examples to help readers better understand.
1. Dependent configuration
To use the Genormous framework, we first need to add it to our project dependence.It can be achieved by adding the following dependencies in the construction file (such as Maven's pom.xml) to achieve:
<dependency>
<groupId>com.genormous</groupId>
<artifactId>genormous</artifactId>
<version>1.0.0</version>
</dependency>
After adding the dependencies, we can start using the Genormous framework function.
2. Sports definition
In the Genormous framework, we need to define the physical class corresponding to the database table.In these physical classes, we can use annotations to specify the name, list name and association relationship.
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
// omit other attributes and methods
}
In the above example, the annotation of `@Entity` is used to identify that this class is a physical class,`@table` is used to specify the database table name of the physical class, `@id` and@generatedvalue`Define the primary key and generate strategies.Similarly, we can specify the name of the naming with the `@column` annotation.
3. Database access operation
It is very simple to use the Genormous framework for database access operations.Here are some example code:
public class UserDao {
public User getUserById(Long id) {
return Genormous.findByPK(User.class, id);
}
public void saveUser(User user) {
Genormous.save(user);
}
public void deleteUser(User user) {
Genormous.delete(user);
}
}
In the above code, we use the `geenormous.findbypk` method to find the user based on the primary key.
4. Query operation
The Genormous framework also provides a more powerful query function, which can easily achieve complex query operations.The following is an example:
public class UserDao {
public List<User> getUsersByName(String name) {
return Genormous.createNamedQuery(User.class, "SELECT u FROM User u WHERE u.username LIKE :name")
.setParameter("name", name)
.getResultList();
}
}
In the above code, we use the `geenormous.createnamedQuery` method to create a naming query with parameters, and set the query parameters using the` setparameter` method.
Through the above code example, we have a certain understanding of the technical principles of the Genormous framework.The framework is based on JPA and Hibernate. Using simple annotations and method calls can achieve database access and operation.Through Genormous, developers can focus more on the realization of business logic and improve development efficiency.
Summarize:
Genormous is a process of Java libraries and frameworks based on JPA and Hibernate, which is used to simplify database access and operation.It provides easy -to -use API and annotations, which can easily perform physical class definition, database access and query operations.Through Genormous, developers can develop and maintain database -related applications more efficiently.
Reference link: https://github.com/genormous/genormousous