欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GreenDao数据库

程序员文章站 2024-03-21 10:08:34
...

GreenDao数据库


配置
object:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.1’
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2’ // add plugin
}
}

    app:
    apply plugin: 'com.android.application'
    apply plugin: 'org.greenrobot.greendao' // apply plugin

    dependencies {
    implementation 'org.greenrobot:greendao:3.2.2' // add library
    }

Bean类

package com.example.greendao;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;

@Entity
public class GoodsBean {
    @Id(autoincrement = true)
    long Id;

    @Property(nameInDb = "title")
    String title;

    @Property(nameInDb = "price")
    int price;

    @Property(nameInDb = "count")
    int count;

    @Property(nameInDb = "describe")
    String describe;
    //锤一下自动生成

    @Generated(hash = 733595413)
    public GoodsBean(long Id, String title, int price, int count, String describe) {
        this.Id = Id;
        this.title = title;
        this.price = price;
        this.count = count;
        this.describe = describe;
    }

    @Generated(hash = 1806305570)
    public GoodsBean() {
    }

    public long getId() {
        return this.Id;
    }

    public void setId(long Id) {
        this.Id = Id;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getPrice() {
        return this.price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getCount() {
        return this.count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getDescribe() {
        return this.describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }
}

Application

package com.example.greendao;

import android.app.Application;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class App extends Application {
    private DaoSession daoSession;

    public DaoSession getDaoSession() {
        return daoSession;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"stop.db");
        DaoMaster master = new DaoMaster(helper.getWritableDb());
        daoSession = master.newSession();
    }
    //增加
    public void add(GoodsBean bean){
        daoSession.getGoodsBeanDao().insert(bean);
    }
    //删除
    public void delete(long id){
        daoSession.getGoodsBeanDao().deleteByKey(id);
    }
    //更新
    public void update(GoodsBean bean){
        daoSession.getGoodsBeanDao().update(bean);
    }
    //按姓名查询
    public List<GoodsBean> query(String name){
        QueryBuilder<GoodsBean> builder = daoSession.getGoodsBeanDao().queryBuilder();
        return builder.where(GoodsBeanDao.Properties.Title.eq(name)).orderAsc(GoodsBeanDao.Properties.Id).build().list();
    }
    //查询全部数据
    public List<GoodsBean> queryAll(){
        return daoSession.getGoodsBeanDao().loadAll();
    }
}

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    app = (App) getApplication();
    textView = findViewById(R.id.title_id);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.add_id:
            app.add(new GoodsBean((long) 1,"华为",3999,1,"9折"));
            break;
        case R.id.delete_id:
            app.delete(0);
            break;
        case R.id.query_id:
            List<GoodsBean> goodsBeans = app.queryAll();
            textView.setText(goodsBeans.toString());
            break;
    }
}