def paging_version = "3.1.1"// //google分页库 无感知预加载implementation "androidx.paging:paging-runtime:$paging_version"// alternatively - without Android dependencies for tests//testImplementation "androidx.paging:paging-common:$paging_version"def lifecycle_version = "2.6.0-alpha01"// ViewModelimplementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"// ViewModel utilities for Compose//implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"// Saved state module for ViewModelimplementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"def room_version = "2.4.3"implementation "androidx.room:room-runtime:$room_version"annotationProcessor "androidx.room:room-compiler:$room_version"implementation "androidx.room:room-paging:$room_version"
2. 数据库管理
2.1 Student.java
@Entity(tableName = "student_table")
public class Student {@PrimaryKey(autoGenerate = true)private int id;@ColumnInfo(name = "student_number")private int studentNumber;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getStudentNumber() {return studentNumber;}public void setStudentNumber(int studentNumber) {this.studentNumber = studentNumber;}
}
2.2 StudentDao.java
@Dao
public interface StudentDao {@Insertvoid insertStudents(Student... students);@Query("DELETE FROM student_table")void deleteAllStudents();@Query("SELECT * FROM student_table ORDER BY id")PagingSource getAllStudents();// DataSource.Factory getAllStudents();
}