美文网首页
ObjectBox For Kotlin

ObjectBox For Kotlin

作者: 静默的小猫 | 来源:发表于2019-07-10 11:37 被阅读0次

    1.Gradle配置

    buildscript {

     ext.objectboxVersion ='2.2.0'

            dependencies { 

                 classpath"io.objectbox:objectbox-gradle-plugin:$objectboxVersion"

            }

    }

    2.

    dependencies { 

     debugImplementation"io.objectbox:objectbox-android-objectbrowser:$objectboxVersion"

    releaseImplementation"io.objectbox:objectbox-android:$objectboxVersion"

    }

    apply plugin:'io.objectbox'

    // 注意下面的内容放到 dependencies 语句块的后面apply plugin:'io.objectbox'

    3.//数据库统一操作管理类初始化

    DataManager.getInstance().init(this)

    4.实体类

    import io.objectbox.annotation.Entity

    import io.objectbox.annotation.Id

    @Entity

    class UserInfo {

    /**

    * actionType : 2

    * birthday : 2008-06-13 00:00:00

    * email : lupeibo@qq.com

    * id : 1

    * loginId : 12

    * nickname : 陆晓亮

    * phone : 18680503648

    * platform : 2

    * recommendCode : 217823

    * sex : 1

    * storeId : 2111

    * token : 318c84c1594e49c49a586ba5ab8edd18

    * userHeadPic : /pic/udu1232.png

    * userLevel : 1

    * username : 陆培波

    */

        var actionType: Int =1

        var birthday: String? =null

        var email: String? =null

        @Id(assignable = true)

        var id: Long =1

        var loginId: String? =null

        var nickname: String? =null

        var phone: String? =null

        var platform: Int =2

        var recommendCode: String? =null

        var sex: Int =0

        var storeId: Int =0

        var token: String? =null

        var userHeadPic: String? =null

        var userLevel: Int =0

        var username: String? =null

        constructor(actionType: Int, birthday: String?, email: String?, id: Long, loginId: String?, nickname: String?, phone: String?, platform: Int, recommendCode: String?, sex: Int, storeId: Int, token: String?, userHeadPic: String?, userLevel: Int, username: String?) {

    this.actionType = actionType

    this.birthday = birthday

    this.email = email

    this.id = id

    this.loginId = loginId

    this.nickname = nickname

    this.phone = phone

    this.platform = platform

    this.recommendCode = recommendCode

    this.sex = sex

    this.storeId = storeId

    this.token = token

    this.userHeadPic = userHeadPic

    this.userLevel = userLevel

    this.username = username

    }

    constructor()

    }

    5.

    /**

    * 数据库操作统一管理类

    */

    public  class DataManager {

    private static DataManager dataManager;

    public static synchronized DataManager getInstance() {

    if (dataManager ==null) {

    dataManager =new DataManager();

    }

    return dataManager;

    }

    public BoxStore boxStore;

    public Box userInfoBox;

    public void init(BaseApplication baseApplication) {

    boxStore = MyObjectBox.builder().androidContext(baseApplication).build();

    initUserInfoBox();

    }

    private void initUserInfoBox() {

    //对应操作对应表的类

            userInfoBox = boxStore.boxFor(UserInfo.class);

    }

    public void insertUserInfo(UserInfo userInfo) {

    userInfoBox.removeAll();

    userInfoBox.put(userInfo);

    }

    public void clearUserInfo() {

    userInfoBox.removeAll();

    }

    public UserInfo queryUserInfo(){

    return userInfoBox.query().build().findUnique();

    }

    }

    相关文章:https://www.jianshu.com/p/e4ebaa433ae7

    相关文章

      网友评论

          本文标题:ObjectBox For Kotlin

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