GreenDao

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

    private DaoManager(Context context) {
        daoSession = DaoMaster.newDevSession(context, "my.db");
    }

    public static DaoManager instance(Context context) {
        if (daoManager == null) {
            synchronized (DaoManager.class) {
                if (daoManager == null) {
                    daoManager = new DaoManager(context);
                }
            }
        }
        return daoManager;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }
}
 
@Entity(nameInDb = "person")
public class Person {
    @Id(autoincrement = true)
    private Long id;
    @Property(nameInDb = "name")
    private String name1;
    private int age;
    @Transient
    private int genger;
}
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity---";
    /**
     * 查询
     */
    private Button mBtnQuery;
    /**
     * 添加
     */
    private Button mBtnAdd;
    /**
     * 删除
     */
    private Button mBtnDelete;
    /**
     * 修改
     */
    private Button mBtnUpdate;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mBtnQuery = (Button) findViewById(R.id.btn_query);
        mBtnQuery.setOnClickListener(this);
        mBtnAdd = (Button) findViewById(R.id.btn_add);
        mBtnAdd.setOnClickListener(this);
        mBtnDelete = (Button) findViewById(R.id.btn_delete);
        mBtnDelete.setOnClickListener(this);
        mBtnUpdate = (Button) findViewById(R.id.btn_update);
        mBtnUpdate.setOnClickListener(this);


        daoSession = DaoManager.instance(this).getDaoSession();
        personDao = daoSession.getPersonDao();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_query:
                List<Person> list = personDao.queryBuilder()
                        //.where(PersonDao.Properties.Id.eq(2))
                        .build().list();
                Log.d(TAG, "onClick: " + list.toString());
                break;
            case R.id.btn_add:
                index++;
                personDao.insert(new Person(null, "张三" + index, index));
                break;
            case R.id.btn_delete:
                List<Person> list1 = personDao.queryBuilder()
                        .where(PersonDao.Properties.Id.gt(2))
                        .build().list();
                for (int i = 0; i < list1.size(); i++) {
                    personDao.delete(list1.get(i));
                }

 
                break;
            case R.id.btn_update:
                Person unique1 = personDao.queryBuilder()
                        .where(PersonDao.Properties.Id.eq(2))
                        .build().unique();
                unique1.setAge(19);
                personDao.update(unique1);
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值