Android Room的学习之关联表

Room学习三部曲

Android Room的学习之基本使用
Android Room的学习之关联表
Android Room的学习之数据库升级

上一篇我们已经简单的学习Room的使用,这一篇我们就来学习一下Room多表关联,让我们对Room有更多的了解。

在了解Room多表关联之前,我们需要学习一下Room里边的注解的意思

实体类注解
@Entity

  tableName 表示表的名称
  @Entity(tableName = "keyvalue_table")

  @Entity 默认表的名称就是类的名称
  public class Student {}

@PrimaryKey

  每个实体必须定义至少1个主键.
  autoGenerate = true,默认为false
  @PrimaryKey(autoGenerate = true)
  public int sId;

@ColumnInfo

  注解设置成员对应的列名。如果你觉得成员变量名就本身就可以作为列名,也可以不设置。
  @ColumnInfo(name = "age")
  public int age;

@Ignore

  Ignore表示不加入表中, 默认全部加入到表中字段
  @Ignore(name = "sex")
  public int sex;

@ForeignKey

 Student实体类的sId和Book实体类的studentId相关联
 注解中包含Delete = CASCADE, 你可以告诉SQLite,如果相应的Student实例被删除,那么删除这个Student下的所有book。
 @Entity(foreignKeys = @ForeignKey(entity = Student.class,
        parentColumns = "sId",
        childColumns = "studentId"
        ,onDelete = CASCADE))
 public class Book {
 
    @ColumnInfo(name = "studentId")
    public String studentId;
    
 }

例子:

实体类Student

@Entity
public class Student {

    @PrimaryKey(autoGenerate = true)
    public int sId;

    @ColumnInfo(name = "name")
    public String name;

    @ColumnInfo(name = "age")
    public int age;

    public int getId() {
        return sId;
    }

    public void setId(int sId) {
        this.sId = sId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

StudentDao.java

@Dao
public interface StudentDao {

    @Query("SELECT * FROM Student")
    List<Student> getAll();

    @Query("SELECT * FROM Student WHERE sId IN (:userIds)")
    List<Student> loadAllByIds(int[] userIds);

    @Query("SELECT * FROM Student WHERE age=:age")
    List<Student> getStudentByAge(int age);

    @Query("SELECT * FROM Student WHERE age=:age LIMIT :max")
    List<Student> getStudentByAge(int max, int... age);

    @Query("SELECT * FROM Student WHERE sId =:sId")
    Student getStudent(int sId);

    @Insert
    void insertAll(Student... users);

    @Update
    void updateStudent(Student... users);

    @Delete
    void delete(Student user);

}

实体类Book 重点重点重点 关于外键看注解

@Entity(foreignKeys = @ForeignKey(entity = Student.class, parentColumns = "sId", childColumns = "studentId",onDelete = CASCADE))
public class Book {

    @PrimaryKey(autoGenerate = true)
    public int bookId;

    @ColumnInfo(name = "bookName")
    public String bookName;

    @ColumnInfo(name = "studentId")
    public String studentId;

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }
}
@Dao
public interface BookDao {

    @Query("SELECT * FROM Book")
    List<Book> getAll();

    @Query("SELECT * FROM Book WHERE studentId IN (:bookId)")
    List<Book> loadAllByIds(int[] bookId);

    @Query("SELECT * FROM Book WHERE studentId =:studentId")
    Book getBook(String studentId);

    @Insert
    void insert(Book... users);

    @Update
    void update(Book... users);

    @Delete
    void delete(Book user);

    //使用内连接查询
    @Query("SELECT studentId,name,bookName,bookId,age  from Student INNER JOIN Book ON Student.sId = Book.studentId")
    List<Combination> getFromCompany();

}

外键查询 组合类

public class Combination {

    public String name;
    public int age;

    public int bookId;

    public String bookName;

    public String studentId;

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
@Database(entities = {Student.class,Book.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static final String DB_NAME = "StudentDatabase.db";
    private static volatile AppDatabase instance;

    static synchronized AppDatabase getInstance(Context context) {
        if (instance == null) {
            instance = create(context);
        }
        return instance;
    }

    private static AppDatabase create(final Context context) {
        return Room.databaseBuilder(
                context,
                AppDatabase.class,
                DB_NAME).build();
    }

    public abstract StudentDao studentDao();

    public abstract BookDao bookDao();
}

添加数据

  StudentDao mStudentDao = db.studentDao();
  Student mStudent = new Student();
  mStudent.setId(i);
  mStudent.setAge(56 + i);
  mStudent.setName("张三" + i);
  mStudentDao.insertAll(mStudent);

  BookDao mBookDao = db.bookDao();
  Book mBook = new Book();
  mBook.setBookName("物理" + i);
  mBook.setStudentId(i + "");
  mBookDao.insert(mBook);

查询数据

 private void findAge2Book() {
        RxJavaUtils.executeAsyncTask(new RxAsyncTask<BookDao, List<Combination>>(db.bookDao()) {

            @Override
            public List<Combination> doInIOThread(BookDao mBookDao) {
                return mBookDao.getFromCompany();
            }

            @Override
            public void doInUIThread(List<Combination> o) {
                for (Combination mCombination : o) {
                    Log.e("doInUIThread", "doInUIThread getAge   " + mCombination.getAge());
                    Log.e("doInUIThread", "doInUIThread getName  " + mCombination.getName());

                    Log.e("doInUIThread", "doInUIThread getBookName   " + mCombination.getBookName());
                    Log.e("doInUIThread", "doInUIThread getStudentId  " + mCombination.getStudentId());
                    Log.e("doInUIThread", "doInUIThread getBookId  " + mCombination.getBookId());
                }

            }
        });
    }

截图
在这里插入图片描述

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值