Android-Room數(shù)據(jù)庫(kù)(介紹)
Android—Room自定義類型(TypeConverter)
Android—Room?數(shù)據(jù)庫(kù)遷移(Migration)
Android — Room?數(shù)據(jù)庫(kù)跳躍式升級(jí)(Migration)
Android—Room 通用封裝
前言
在SQLite數(shù)據(jù)庫(kù)中咐鹤,我們可以指定對(duì)象之間的關(guān)系陨献,因此我們可以將一個(gè)或多個(gè)對(duì)象與一個(gè)或多個(gè)其他對(duì)象綁定荆几。這就是所謂的一對(duì)多和多對(duì)多的關(guān)系慢宗。
既然要多表查詢训唱,所以表之間就得有關(guān)聯(lián)。這時(shí)候我們就得使用新的注解符@ForeignKey
接下來(lái)的內(nèi)容拐辽,就需要上節(jié)的內(nèi)容了
@Entity
public class Company {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
private int age;
private String address;
private double salary;
public Company(String name, int age, String address, double salary) {
this.name = name;
this.age = age;
this.address = address;
this.salary = salary;
}
//省略了getter/setter方法
}
下面我們?cè)傩陆ㄒ粋€(gè)與之關(guān)聯(lián)的表
@Entity(foreignKeys = @ForeignKey(entity = Company.class,parentColumns = "id",childColumns = "emp_id",onDelete = CASCADE),
indices = @Index(value={"emp_id"},unique = true))
public class Department {
@PrimaryKey(autoGenerate = true)
private int id;
private String dept;
@ColumnInfo(name = "emp_id")
private int empId;
public Department(String dept, int empId) {
this.dept = dept;
this.empId = empId;
}
//省略了getter/setter方法
}
這里我使用了
@ForeignKey
關(guān)聯(lián)了company
表炉奴,主鍵id
,外鍵emp_id
克蚂,緊接著使用了indices
創(chuàng)建了唯一索引闺鲸。
下面就是創(chuàng)建Dao
@Dao
public interface CompanyDao {
@Query("SELECT * FROM company")
List<Company> getAllCompany();
}
@Dao
public interface DepartmentDao {
@Query("SELECT * FROM department")
List<Department> getAllDepartment();
//使用內(nèi)連接查詢
@Query("SELECT emp_id,name,dept from company INNER JOIN department ON Company.id=Department.emp_id")
List<InnerJoinResult> getDepartmentFromCompany();
}
最后就是創(chuàng)建Database
@Database(entities = {Department.class, Company.class}, version = 1, exportSchema = false)
public abstract class DepartmentDatabase extends RoomDatabase {
public static final String DB_NAME = "CompanyDatabase.db";
private static volatile DepartmentDatabase instance;
public static synchronized DepartmentDatabase getInstance(Context context) {
if (instance == null) {
instance = create(context);
}
return instance;
}
private static DepartmentDatabase create(final Context context) {
return Room.databaseBuilder(
context,
DepartmentDatabase.class,
DB_NAME).allowMainThreadQueries().build();
}
public abstract DepartmentDao getDepartmentDao();
public abstract CompanyDao getCompanyDao();
}
這里我想大家經(jīng)過(guò)之前的文章介紹都很熟悉了吧。這里就不多解釋了埃叭,不記得的摸恍,請(qǐng)看之前的文章。
具體使用
List<Company> list = new ArrayList<>();
Company company = new Company("Paul",32,"California",20000.0);
list.add(company);
company = new Company("Allen",25,"Texas",15000.0);
list.add(company);
company = new Company("Teddy",23,"Norway",20000.0);
list.add(company);
company = new Company("Mark",25,"Rich-Mond",65000.0);
list.add(company);
company = new Company("David",27,"Texas",85000.0);
list.add(company);
company = new Company("Kim",22,"South-Hall",45000.0);
list.add(company);
company = new Company("James",24,"Houston",10000.0);
list.add(company);
List<Department> departmentList = new ArrayList<>();
Department department = new Department("IT Billing",1);
departmentList.add(department);
department = new Department("Engineerin",2);
departmentList.add(department);
department = new Department("Finance",7);
departmentList.add(department);
DepartmentDatabase.getInstance(this)
.getCompanyDao().insert(list);
DepartmentDatabase.getInstance(this)
.getDepartmentDao().insert(departmentList);
這樣我們就把需要的數(shù)據(jù)插入到數(shù)據(jù)庫(kù)了赤屋,下面我們查詢一下立镶,看看有沒(méi)有插入成功。
查詢代碼如下:
List<Company> company = DepartmentDatabase.getInstance(this).getCompanyDao().getAllCompany();
LogUtil.debug("Company----->" + company.size());
for (Company result : company) {
LogUtil.debug("result--->" + result.getName() + " " + result.getAge()+" "+result.getAddress()+" "+result.getSalary());
}
List<Department> department = DepartmentDatabase.getInstance(this).getDepartmentDao().getAllDepartment();
LogUtil.debug("Department----->" + department.size());
for (Department result : department) {
LogUtil.debug("result--->" + result.getDept() + " " + result.getEmpId());
}
這樣看來(lái)是插入成功了的类早。媚媒。。
好了涩僻,下面我們要開(kāi)始連表查詢了欣范。
當(dāng)我們查詢的時(shí)候变泄,我們就會(huì)遇到一個(gè)問(wèn)題,那就是我們關(guān)聯(lián)兩個(gè)表查詢后的字段是company
和departemnt
組合之后的恼琼。所以這里我們又需要去再次創(chuàng)建一個(gè)Model
類妨蛹。
public class InnerJoinResult {
@ColumnInfo(name="emp_id")
private int empId;
private String name;
private String dept;
//這里同樣省略了getter/setter方法
}
因?yàn)樯厦嫖覀円呀?jīng)將查詢方法寫(xiě)好了,直接調(diào)用即可晴竞。
List<InnerJoinResult> company1 = DepartmentDatabase.getInstance(this).getDepartmentDao().getDepartmentFromCompany();
LogUtil.debug("InnerJoinResult----->" + company1.size());
for (InnerJoinResult result : company1) {
LogUtil.debug("result--->" + result.getEmpId() + " " + result.getName() + " " + result.getDept());
}
查詢結(jié)果如下蛙卤,說(shuō)明我們多表查詢成功了:
到這里,多表查詢就結(jié)束了噩死。未完待續(xù)