Use of Android GreenDao3.0 operation database

Databases are mostly used in Android development to store data. I have used Android’s native database before. This time I plan to use GreenDao3.0. I also used GreenDao3.0 in a previous project and almost forgot about it after a long time.

1: Introduce the benefits of GreenDao3.0:

It is an object-relational mapping framework that provides an interface to operate the database in the same way as objects, making it easier and more convenient for you to operate the database.

Fast response, low memory usage, small library file, less than 100k, supports encryption, simple and easy-to-use API

2: Usage steps:

1. Introduce build.gradle under the project's project, and then synchronize the project

classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'

2. Introduce it under build.gradle of module and synchronize the project

apply plugin: 'org.greenrobot.greendao'   //是一个插件

3.Introduce under dependencies

compile 'org.greenrobot:greendao:3.0.1'

4.Introduced under android closure

greendao {
    /**
     * 版本号
     */
    schemaVersion 1
    /**
     * greendao输出dao的数据库操作实体类文件夹(相对路径 包名+自定义路径名称,包将创建于包名的直接路径下)
     */
    daoPackage 'wansun.visit.android.greendao.gen'
    /**
     * greenDao实体类包文件夹
     */
    targetGenDir 'src/main/java'
}

5. Create an entity class UserBean

/**
 * Created by User on 2019/6/5.
 */
@Entity
public class UserBean {
    @Id
    public Long id;
     public String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Generated(hash = 2024802960)
    public UserBean(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    @Generated(hash = 1203313951)
    public UserBean() {
    }

    public String getName() {
        return name;
    }

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

Note that after completing the above steps, you must compile the project and you will find that a file greendao.gen is automatically generated under src/main/java. As shown in the figure:

   3 folders were automatically generated

6. Customize an application to initialize the relevant configuration of GreenDao3.0 

/**
 * Created by User on 2019/5/15.
 */

public class LocklBleApplication extends Application {
    //提供全局的上下文环境
    public  static  Context  mContext;
    private DaoMaster.DevOpenHelper dbHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    prvivate static LockBleApplication;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext=getApplicationContext();
        app=this;
        initDatabass();   //初始化的相关配置
    }
    public static Context getInstanceContext(){

        return mContext;
    }
public static LocklBleApplication getInstance(){
    return  app;
}
    private void initDatabass() {
        //这里之后会修改,关于升级数据库
        dbHelper = new DaoMaster.DevOpenHelper(this, "lockBle-db", null);
        db = dbHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }


    public DaoSession getSession(){
        return mDaoSession;
    }
    public SQLiteDatabase getDb(){
        return db;
    }
}

7. The next step is to operate the database, which is CRUD, and add a piece of data

/**
 * GreenDao3.0测试
 * Created by User on 2019/6/10.
 */

public class GreenDaoActivity extends BaseActivity {
    Button but_add,but_qury,but_delect;
    UserBeanDao dao;

    @Override
    public int getLayoutId() {
        return R.layout.activity_green_dao;
    }

    @Override
    public void initView() {
          dao = LocklBleApplication.getInstance().getSession().getUserBeanDao();
          but_add= (Button) findViewById(R.id.but_add);
           but_qury= (Button) findViewById(R.id.but_qury);
          but_delect= (Button) findViewById(R.id.but_delect);


    }

    @Override
    public void initEvent() { 
        but_qury.setOnClickListener(new View.OnClickListener() {
        /** 
         * Add data 
         */ 
        but_add.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                for (int i = 0; i < 5; i++) { //Insert 5 pieces of data 
                    UserBean bean=new UserBean(); 
                    bean.setId((long) i); 
                    bean.setName("Zhang Fei"+i); 
                    dao.insert(bean); //Insert a piece of data 
                } 


            } 
        }); 
        /** 
         * Query data 
         */ 
            @Override 
            public void onClick(View v) {
                List<UserBean> userBeen = dao.loadAll(); //Query all data 
                for (UserBean us: userBeen ){ 
                    Log.d("TAG","Query data"+us.getName()); 
                } 

            } 
        }); 
        /** 
         * Delete data 
         * Methods to delete data 1. Delete all instances 2. According to id 3. Delete all 
         */ 
        but_delect.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                dao.deleteByKey ((long) 0); //Delete according to id 
                dao.deleteByKey((long) 1); 
                dao.deleteByKey((long) 2); 
                 
            }
        }); 
    } 

    @Override
    public void initData() {

    }
}

As shown in the figure: 5 pieces of data were added, 3 pieces were deleted, and 2 pieces of data were found.

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/beautifulYuan/article/details/90898879