Android GreenDao数据库使用

一、先在项目中进行配置
1> 在项目的build的文件中添加,在dependencies里

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

2>在module里的配置
在最上面添加

 apply plugin: 'org.greenrobot.greendao'

3>在Android中添加

greendao{
        schemaVersion 1 //指定数据库schema版本号,迁移等操作会用到
        //包名是活的这是变动的
        daoPackage 'dao的包名,包名默认是entity所在的包' (改成自己的dao包路径)
        targetGenDir 'src/main/java'//生成数据库文件的目录(不用改)
    }

4>添加依赖

implementation 'org.greenrobot:greendao:3.2.2'

二、创建数据库

@Entity
public class DataDao {
    @Id(autoincrement = true)//添加注解,自增
    private Long id;
    private String name;//数据库中的一些字段
    private String sex;
    private int age;
}

写完这些以后,使用锤子锤一下,锤完后就会自动创建三个类

Android GreenDao数据库使用_第1张图片

三、创建一个类继承Application

public class MyApp extends Application {
    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        initGreenDao();
    }

    private void initGreenDao() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "Student");//上下文,数据库名
        SQLiteDatabase writableDatabase = helper.getWritableDatabase();
        DaoMaster master = new DaoMaster(writableDatabase);
        daoSession = master.newSession();
    }

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

记得要在清单文件中注册

四、Activity中的逻辑代码,简单的增删改查操作

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button send_insert;
    private Button send_delete;
    private Button send_update;
    private Button send_select;
    private TextView send_text;
    private DataDaoDao dataDaoDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        dataDaoDao = MyApp.getDaoSession().getDataDaoDao();
    }

    private void initView() {
        send_insert = (Button) findViewById(R.id.send_insert);
        send_delete = (Button) findViewById(R.id.send_delete);
        send_update = (Button) findViewById(R.id.send_update);
        send_select = (Button) findViewById(R.id.send_select);
        send_text = (TextView) findViewById(R.id.send_text);

        send_insert.setOnClickListener(this);
        send_delete.setOnClickListener(this);
        send_update.setOnClickListener(this);
        send_select.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_insert:
                insertSql();
                break;
            case R.id.send_delete:
                deleteSql();
                break;
            case R.id.send_update:
                updateSql();
                break;
            case R.id.send_select:
                selectSql();
                break;
        }
    }

    //查询
    private void selectSql() {
        send_text.setText("");
        List as = dataDaoDao.loadAll();//查询全部
        send_text.setText(as.toString());
    }

    //修改
    private void updateSql() {
        DataDao load = dataDaoDao.load(1l);//根据id修改,因为id为long类型,所以是1l
        load.setAge(66);
        load.setName("李白");
        load.setSex("男");
        dataDaoDao.update(load);//修改
    }

    //删除
    private void deleteSql() {
        dataDaoDao.deleteByKey(1l);//删除同样是根据id
        Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
    }

    //添加
    private void insertSql() {
        DataDao a = new DataDao("杜甫", "男", 24);//添加时,先创建对象,写入参数,调用insert方法
        long insert = dataDaoDao.insert(a);
        if (insert > 0) {//当返回值大于0是,说明成功
            Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
        }
    }
}

五、对应的xml布局文件



    

你可能感兴趣的:(功能总结)