Room Library: https://developer.android.com/training/data-storage/room
1. Implement thư viện
1 2 3 |
def room_version = "2.2.5" implementation "androidx.room:room-runtime:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version" |
2. Tạo Entity: Student.Class
- Class này tương ứng với các trường trong một bảng.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.example.roomtest; import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; @Entity public class Student { @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo private String name; @ColumnInfo private double point; public Student(String name, double point) { this.name = name; this.point = point; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPoint() { return point; } public void setPoint(double point) { this.point = point; } @Override public String toString() { return "\n" + getId() + " - " + getName() + " - " + getPoint(); } } |
3. Tạo DAO: StudentDao.Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.example.roomtest; import androidx.room.Dao; import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import java.util.List; @Dao public interface StudentDao { @Query("SELECT * FROM Student") List<Student> getAll(); @Insert void insert(Student student); @Query("DELETE FROM Student WHERE id = :id") void delete(int id); @Query("UPDATE Student SET name=:name, point =:point WHERE id=:id") void update(int id, String name, double point); } |
4. Tạo Database: StudentDatabase.Class
1234567891011121314151617181920212223242526
package com.example.roomtest; import android.content.Context; import androidx.room.Database;import androidx.room.Room;import androidx.room.RoomDatabase; @Database(entities = {Student.class}, version = 1)public abstract class StudentDatabase extends RoomDatabase { private static StudentDatabase studentDatabase; public static StudentDatabase getInstance(Context context){ if (studentDatabase == null){ studentDatabase = Room.databaseBuilder( context, StudentDatabase.class, "student_manager" ) .allowMainThreadQueries() .build(); } return studentDatabase; } public abstract StudentDao getStudentDao();}
5. Sử dụng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.example.roomtest; import android.content.Context; import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; @Database(entities = {Student.class}, version = 1) public abstract class StudentDatabase extends RoomDatabase { private static StudentDatabase studentDatabase; public static StudentDatabase getInstance(Context context){ if (studentDatabase == null){ studentDatabase = Room.databaseBuilder( context, StudentDatabase.class, "student_manager" ) .allowMainThreadQueries() .build(); } return studentDatabase; } public abstract StudentDao getStudentDao(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//1. Get List Student List<Student> studentList = new ArrayList<>(); studentList = StudentDatabase.getInstance(this).getStudentDao().getAll(); //2. Insert Student Student newStudent = new Student("Duy", 9.5); StudentDatabase.getInstance(this).getStudentDao().insert(newStudent); //3. Delete Student int numberDel = 10; StudentDatabase.getInstance(this).getStudentDao().delete(numberDel); //4. Update Student int numberUpdate = 5; StudentDatabase.getInstance(this).getStudentDao().update(numberUpdate ,"Trang",8.5); |