GreenDao数据库的使用

Project

 buildscript {
    
    repositories {
        google()
        jcenter()
        //add mavenCentral库*******************
        mavenCentral() 
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        // add 插件**************************
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' 

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

Model

 // apply 插件  和转义同级***************
 apply plugin: 'org.greenrobot.greendao'

 //GreenDao数据库*****************
 implementation 'org.greenrobot:greendao:3.2.2' //依赖库

建表

package com.example.kanghuwei.greendao.data;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Transient;
import org.greenrobot.greendao.annotation.Generated;

/**
 * 创建人: 橘子丶
 * 邮箱: [email protected]
 * 创建时间: 2019/2/14 22:23
 */
@Entity
public class User {
    @Id
    private Long id;
    private String name;
    @Generated(hash = 873297011)
    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }
    @Generated(hash = 586692638)
    public User() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

默认路径
GreenDao数据库的使用_第1张图片

APP

package com.example.kanghuwei.greendao.ui;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.example.kanghuwei.greendao.data.DaoMaster;
import com.example.kanghuwei.greendao.data.DaoSession;

/**
 * 创建人: 橘子丶
 * 邮箱: [email protected]
 * 创建时间: 2019/2/14 22:34
 */
public class APP extends Application {

    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        //1.上下文 2.数据库名称 3.游标工厂类
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "juzi", null);
        //获取DataBase对象
        SQLiteDatabase db = openHelper.getWritableDatabase();
        //创建DaoMaster,所需DataBean数据库对象
        DaoMaster daoMaster = new DaoMaster(db);

        //创建DaoSeeion对象
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoSession() {
        return daoSession;
    }
}

在bean类中加入toString方法
MainActivity.this

package com.example.kanghuwei.greendao.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.kanghuwei.greendao.R;
import com.example.kanghuwei.greendao.data.User;
import com.example.kanghuwei.greendao.data.UserDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_add)
    Button btnAdd;
    @BindView(R.id.btn_query)
    Button btnQuery;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.btn_delete)
    Button btnDelete;
    @BindView(R.id.btn_update)
    Button btnUpdate;
    private User cusomeruser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }



    @OnClick({R.id.btn_add, R.id.btn_query, R.id.btn_delete, R.id.btn_update})
    public void onViewClicked(View view) {
        UserDao userDao = APP.getDaoSession().getUserDao();
        switch (view.getId()) {
            case R.id.btn_add:
                //增加数据
                for (int i = 0; i < 10; i++) {
                    User user = new User();
                    user.setName("张三" + i);
                    userDao.insert(user);
                }
                break;
            case R.id.btn_query:
                //查询数据
                List userList = userDao.loadAll();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < userList.size(); i++) {
                    User user = userList.get(i);
                    String userInfo = user.toString();
                    sb.append(userInfo + "------");
                }
                //查询到数据设置到TextView
                String queryAllInfo = sb.toString();
                tvContent.setText(queryAllInfo);
                break;
            case R.id.btn_delete:
                userDao.deleteAll();
                break;
            case R.id.btn_update:
                cusomeruser = new User();
                cusomeruser.setId((long) 1);
                cusomeruser.setName("拉拉啦");
                userDao.update(cusomeruser);
                break;
        }
    }
}

你可能感兴趣的:(GreenDao数据库的使用)