GreenDao


@Entity(nameInDb = "person_table")
public class Person {
    @Id(autoincrement = true)
    public Long id;
    public String name;
    public int age;
    @Transient
    public String sex;

daoMangre

package com.bwie.greendao;

import android.content.Context;

/**
 * Created by dell on 2018/11/1.
 */

public class DaoManager {
    private static DaoManager daoManager;
    private DaoSession daoSession;

    private DaoManager(Context context){
        daoSession = DaoMaster.newDevSession(context,"person.db");
    }
    public static DaoManager getDaoManager(Context context){
        if (daoManager == null){
            synchronized (DaoManager.class){
                if (null == daoManager){
                    daoManager = new DaoManager(context);
                }
            }
        }
        return daoManager;
    }
    public DaoSession getDaoSession(){
        return daoSession;
    }
}
activity

package com.bwie.greendao;

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

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private DaoSession daoSession;
    private int index;
    private PersonDao personDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        daoSession = DaoManager.getDaoManager(this).getDaoSession();
        personDao = daoSession.getPersonDao();
    }

    public void btn_add(View view) {
        index++;
        Person person = new Person(null,"name"+index,index*10);
        personDao.insert(person);
    }

    public void btn_del(View view) {

        Person person = personDao.queryBuilder()
                .where(PersonDao.Properties.Id.eq(2))
                .build()
                .unique();
        if (person != null){
            personDao.delete(person);
        }

    }

    public void btn_update(View view) {
        List<Person> list = personDao.queryBuilder()
                .where(PersonDao.Properties.Age.lt(500))
                .orderDesc(PersonDao.Properties.Age)
                .build()
                .list();
        for (Person person : list) {
            if (person.age == 20){
                person.age = 100;
                personDao.update(person);
            }
        }
    }

    public void btn_query(View view) {
        List<Person> list = personDao.queryBuilder()
                .where(PersonDao.Properties.Age.lt(500))
                .orderDesc(PersonDao.Properties.Age)
                .build()
                .list();
        for (Person person : list) {
            Log.e("tag", "btn_query: "+person.toString() );

        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值