(安卓) GreenDao 数据库的增删改查!


    

    黑色的箭头的代码是 纯依赖生成的      ,    蓝色箭头里的代码里 有一部分是依赖生成的  并且里面数据的类型 (需要注意的是id 是 Long类型

    生成代码需要点击这:   如图    但是之前得添加依赖


 



依赖  

      可以参考GitHub    中  的依赖

在Project中导入:

buildscript {
    repositories {
        jcenter()
        mavenCentral() // add repository
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
    }
}
在App中导入:

apply plugin: 'org.greenrobot.greendao' // apply plugin

 compile 'org.greenrobot:greendao:3.2.2' // add library
greendao {
    schemaVersion 1
    daoPackage 'bwie.com.day_greendao.gen'  //自己的项目包名    生成gen包的
    targetGenDir 'src/main/java'
}
其中 还用到了butterknife  注解的依赖:

     

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'


接下来就上代码  

MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;
import bwie.com.day_greendao.gen.PersonDao;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private PersonDao dao;

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

         //拿到要操作的对象
        dao = DbHelper.getInstance(this).getPersonDao();

    }

     //用butterknife  实现的点击事件
    @OnClick({R.id.btn_insert, R.id.btn_delete, R.id.btn_update, R.id.btn_query})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_insert:
                //插入
                Person p = new Person();
                p.setName("zhangsan");
                p.setAge(22);
                long insert = dao.insert(p);
                Log.i(TAG,"插入了"+insert);
                break;
            case R.id.btn_delete:
                //删除
                Person p1 = new Person();
                p1.setId(5L);
                dao.delete(p1);
                Log.i(TAG,"删除了");
                break;
            case R.id.btn_update:
                //修改
                Person p2 = new Person(2L,"lisi",24);
                dao.update(p2);
                break;
            case R.id.btn_query:
                //查询
                //selsct * from aa where id=2 and name=lisi
                List<Person> lisi = dao.queryRaw("where _id=? and name=?", "2", "lisi");
                Log.i(TAG,"数据:"+lisi.get(0).toString());
                break;
        }
    }
}

Person  数据类型  类:
 
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

/**
 * Created by 迷人的脚毛!! on 2017/11/30.
 */
@Entity
public class Person {
    @Id
    private Long id;
    private String name;
    private int age;
    @Generated(hash = 1145075130)
    public Person(Long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Generated(hash = 1024547259)
    public Person() {
    }
    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;
    }
    public int getAge() {
        return this.age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
   
DbHelper   数据库封装类:  (就如解析okhttp的封装类)

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import bwie.com.day_greendao.gen.DaoMaster;
import bwie.com.day_greendao.gen.DaoSession;
import bwie.com.day_greendao.gen.PersonDao;
 

public class DbHelper {
    private final DaoMaster daoMaster;
    private final DaoSession daoSession;

    //定义单例模式
    private static volatile DbHelper instance;

    public DbHelper(Context context) {

        //初始化数据库的一些配置        第一个参数上下文, 二 :数据库名
        DaoMaster.DevOpenHelper user = new DaoMaster.DevOpenHelper(context, "aa", null);
//        //获取数据库操作对象
        SQLiteDatabase db = user.getWritableDatabase();
//        //获取DaoMaster对象
        daoMaster = new DaoMaster(db);
//       //获取DaoSession对象
        daoSession = daoMaster.newSession();
    }
    public static DbHelper getInstance(Context context){
        if (null==instance){
            synchronized (DbHelper.class){
                if (instance==null){
                    instance=new DbHelper(context);
                }
            }
        }
        return instance;
    }
    //对外定义方法
    public PersonDao getPersonDao(){
        return daoSession.getPersonDao();
    }
}

这就是基本的GreenDao 的增删改查



已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页