美文网首页
Room学习二:表table的详解

Room学习二:表table的详解

作者: crossroads | 来源:发表于2019-03-01 14:23 被阅读6次

    前言

    Room是对SQLite的封装,官方推荐使用Room。该文档来自google官方文档。该文档里的代码是kotlin。

    一、声明一个table,

    @Entity
    data class User(
        @PrimaryKey var id: Int,
        var firstName: String?,
        var lastName: String?
    )
    

    二、详解

    1. 主键 ,使用@PrimaryKey注解;
    • 每一个表格必须至少定义一个字段为主键
    • 如果有多个字段,则如下
    @Entity(primaryKeys = arrayOf("firstName", "lastName"))
    data class User(
        var firstName: String?,
        var lastName: String?
    )
    
    • 如果需要自动分配ids给表格,则如下
     @PrimaryKey(autoGenerate = true)
    
    1. 一般类名默认为表名,自定义表名这样
    @Entity(tableName = "users")
    data class User (
        // ...
    )
    

    注意:在SQLite中表名对大小写不敏感

    1. 一般字段名默认为列名,自定义列名这样
    @Entity(tableName = "users")
    data class User (
        @PrimaryKey var id: Int,
        @ColumnInfo(name = "first_name") var firstName: String?,
        @ColumnInfo(name = "last_name") var lastName: String?
    )
    
    1. 忽视一些字段不要存储在数据库中
    @Entity
    data class User(
        @PrimaryKey var id: Int,
        var firstName: String?,
        var lastName: String?,
        @Ignore var picture: Bitmap?
    )
    

    或者这样

    open class User {
        var picture: Bitmap? = null
    }
    
    @Entity(ignoredColumns = arrayOf("picture"))
    data class RemoteUser(
        @PrimaryKey var id: Int,
        var hasVpn: Boolean
    ) : User()
    
    1. 添加索引indices,增加查询速度
    @Entity(indices = arrayOf(Index(value = ["last_name", "address"])))
    data class User(
        @PrimaryKey var id: Int,
        var firstName: String?,
        var address: String?,
        @ColumnInfo(name = "last_name") var lastName: String?,
        @Ignore var picture: Bitmap?
    )
    

    7.某些字段必须为唯一的,你可以将使用@Index注解的字段的unique属性设为true,如下

    @Entity(indices = arrayOf(Index(value = ["first_name", "last_name"],
            unique = true)))
    data class User(
        @PrimaryKey var id: Int,
        @ColumnInfo(name = "first_name") var firstName: String?,
        @ColumnInfo(name = "last_name") var lastName: String?,
        @Ignore var picture: Bitmap?
    

    8.多表关联@ForeignKey,如要关联User表:

    @Entity(foreignKeys = arrayOf(ForeignKey(
                entity = User::class,
                parentColumns = arrayOf("id"),
                childColumns = arrayOf("user_id"))
           )
    )
    data class Book(
        @PrimaryKey var bookId: Int,
        var title: String?,
        @ColumnInfo(name = "user_id") var userId: Int
    )
    
    1. @Embedded拆解其他类的字段变为表的字段,如下user表包含Address下的字段:
    data class Address(
        var street: String?,
        var state: String?,
        var city: String?,
        @ColumnInfo(name = "post_code") var postCode: Int
    )
    
    @Entity
    data class User(
        @PrimaryKey var id: Int,
        var firstName: String?,
        @Embedded var address: Address?
    )
    

    如果两个类有相同字段,可以使用 @Embedded 添加前缀。

      @Embedded(prefix = "XX_") var address: Address?
    

    相关文章

      网友评论

          本文标题:Room学习二:表table的详解

          本文链接:https://www.haomeiwen.com/subject/hwtduqtx.html