GreenDao简单使用

// 写一个bean类 添加get/set方法 属性加入注解 然后build--->make project
@Entity
public class Person {
    @Id(autoincrement = true)//可以设置主键自增
    private Long id;
    @NotNull//可以设置不为空
    private String name;
    private String sex;
    private int age;

    @Generated(hash = 705769785)
    public Person(Long id, @NotNull String name, String sex, int age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
}

// application中
public class MyApplication extends Application {

    private static DashApplication instances;//整个应用的上下文
    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;

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

        instances = this;

        setDatabase();//设置greenDao
    }

    /**
     * 设置greenDao
     *
     * 1.获取helper对象 目的是为了拿到数据库
     * 2.拿到数据库 目的创建DaoMaster对象
     * 3.使用daoMaster创建seesion对象,会话....使用会话对象拿到personDao
     *
     */
    private void setDatabase() {

        mHelper = new DaoMaster.DevOpenHelper(this, "bawei", null);//string便是数据库的名字
        db = mHelper.getWritableDatabase();
        // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();

    }

    /**
     * 对外提供获取seesion的方法
     * @return
     */
    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    /**
     * 对外提供获取数据库的方法
     * @return
     */
    public SQLiteDatabase getDb() {
        return db;
    }


    /**
     * 对外地宫返回上下文的方法
     * @return
     */
    public static DashApplication getInstances(){
        return instances;
    }
}

// 使用
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.text_note)
    TextView textNote;
    private PersonDao personDao;

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

        //butterKinife
        ButterKnife.bind(this);

        //获取dao对象
        personDao = DashApplication.getInstances().getDaoSession().getPersonDao();

    }

    @OnClick({R.id.button_add, R.id.button_delete, R.id.button_update, R.id.button_qurey})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_add:

                Person person = new Person();
                person.setAge(23);
                person.setName("张三");
                person.setSex("男");
                personDao.insert(person);//添加一个实体对象

                //personDao.insertInTx();//添加多个实体对象,,,,可变参数

                break;
            case R.id.button_delete:

                //personDao.deleteAll();//删除所有
                //personDao.delete(删除一个人);
                personDao.deleteByKey(1L);//根据主键进行删除

                break;
            case R.id.button_update:

                personDao.update(new Person(1L,"lisi","girl",24));

                break;
            case R.id.button_qurey:

                /*List<Person> list = personDao.loadAll();//查询全部
                if (list.size() != 0) {

                    Log.e("---",list.get(0).getName());
                }

                Person person1 = personDao.load(1L);//根据主键进行查询
                Log.e("----", person1.toString());*/

                //select * from person where id = ? and name= ?
                List<Person> list1 = personDao.queryRaw("where _id = ? and name =?", "3","张三");
                Log.e("---","list集合长度是:"+list1.size());
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wanderer_ankey/article/details/79534570