GreenDao简单使用

//Person

@Entity(nameInDb = "person")
public class Person {

    @Id(autoincrement = true)
    private Long id;
    @Property(nameInDb = "name")
    private String name1;
    private int age;
    @Transient
    private int gender;
    @Generated(hash = 159489363)
    public Person(Long id, String name1, int age) {
        this.id = id;
        this.name1 = name1;
        this.age = age;
    }
    @Generated(hash = 1024547259)
    public Person() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName1() {
        return this.name1;
    }
    public void setName1(String name1) {
        this.name1 = name1;
    }
    public int getAge() {
        return this.age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name1='" + name1 + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                '}';
    }
}



//DaoManager

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;
    }
}



//MainActivity

public class MainActivity extends AppCompatActivity {

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

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

    public void add(View view) {
        index++;
        personDao.insert(new Person(null, "wang" + index, index));
    }

    public void delete(View view) {
        Person person = personDao.queryBuilder()
                .where(PersonDao.Properties.Id.eq(1))
                .build().unique();
        personDao.delete(person);
    }

    public void update(View view) {
        Person person = personDao.queryBuilder()
                .where(PersonDao.Properties.Id.eq(1))
                .build().unique();
        person.setAge(1000);
        personDao.update(person);
    }

    public void query(View view) {
        List list = personDao.queryBuilder()
                .where(PersonDao.Properties.Id.ge(5))
                .build().list();
        Log.e("tag", "query" + list.get(0).getId());
    }
}


你可能感兴趣的:(GreenDao简单使用)