Use the ORMLITE CORE framework to achieve durable data
Use the ORMLITE CORE framework to achieve durable data
ORMLITE is a Java -based lightweight object relationship mapping (ORM) library, which can help developers simplify database operations and provide objects to the mapping of relational databases.ORMLITE CORE is the core framework of ORMLITE, which is used to handle the operations such as connection, query, update and deletion between databases.This article will introduce how to use the ORMLITE CORE framework to achieve durable data.
First, we need to add the dependencies of the ORMLITE CORE library.You can use Maven to add the following dependencies:
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-core</artifactId>
<version>5.6</version>
</dependency>
Next, we can create a Java class to indicate that we want to persist.For example, we create a class called Student, which has ID, name, and Age attributes::
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "students")
public class Student {
@DatabaseField(generatedId = true)
private int id;
@DatabaseField
private String name;
@DatabaseField
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//...
}
In the above example, we use the annotation provided by ORMLITE to define the name of the database table and the attributes corresponding to each field.@DataBasetable annotations are used to define the table name, and @DataBaseField annotations are used to define fields.
Next, we need to create a database connection and operate.Here we use the H2 memory database for example.Below is an example code that uses the ORMLITE CORE framework for database operations:
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import java.sql.SQLException;
import java.util.List;
public class Main {
public static void main(String[] args) throws SQLException {
// Create a database connection
ConnectionSource connectionSource = new JdbcConnectionSource("jdbc:h2:mem:test");
// Create a table
Dao<Student, Integer> studentDao = DaoManager.createDao(connectionSource, Student.class);
studentDao.createTableIfNotExists();
// Create a student object
Student Student1 = New Student ("Xiaoming", 18);
Student Student2 = New Student ("Little Red", 20);
// Insert student objects to database
studentDao.create(student1);
studentDao.create(student2);
// Query all student objects
List<Student> students = studentDao.queryForAll();
for (Student student : students) {
System.out.println ("Student Name:" + Student.getName () + "Age:" + Student.getage ());
}
// Update student objects
student1.setAge(20);
studentDao.update(student1);
// Delete student objects
studentDao.delete(student2);
// Close the database connection
connectionSource.close();
}
}
In the above code, we first created a JDBCCONNECTIONSOURCE object to connect the database.Then use Daomanager to create a DAO object for database operations for the Student class.Next, we insert and update student objects through StudentDao's Create and Update methods.Check and delete student objects through the QueryFrall and Delete methods.Finally, we turn off the database connection.
Summarize:
Using the ORMLITE CORE framework can simplify the mapping and operation between the object and the relationship database in Java.By defining the annotations of the class, we can easily create tables and fields and perform CRUD operations through DAO objects.The above example code shows how to use the ORMLITE CORE framework for data persistence operation.