GreenDao的封装和使用
首先先集成GreenDao,在本人博客里有GreenDao3.2的集成步骤,可参考
MainActivity
package com.example.greendaodemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.example.greendaodemo.bean.User;
import com.example.greendaodemo.dao.MyUserDao;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyUserDao dao = new MyUserDao(this);
// User u = new User();
// u.setAge(10);
// u.setName("22");
// u.setId("001");
// dao.insertUser(this,);
for (int i = 20; i < 25; i++) {
User user = new User();
user.setId(Long.valueOf(i));
user.setAge(i * 3);
user.setName("第" + i + "人");
dao.insertUser(user);
}
List userList = dao.queryUserList();
for (User user : userList) {
Log.e("TAG", "queryUserList--before-->" + user.getId() + "--" + user.getName() +"--"+user.getAge());
if (user.getId() == 10) {
dao.deleteUser(user);
}
if (user.getId() == 13) {
user.setAge(25);
dao.updateUser(user);
}
}
userList = dao.queryUserList();
for (User user : userList) {
Log.e("TAG", "queryUserList--after--->" + user.getId() + "---" + user.getName()+"--"+user.getAge());
}
}
}
GreenDao封装
package com.example.greendaodemo.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.example.greendaodemo.bean.DaoMaster;
/**
* Created by yfeng on 2017/11/23.
* DBManager 数据库管理者
*
* 获取可读或者可写的数据库对象
*/
public class DBManager {
private final static String dbName = "test_db";
private static DBManager mInstance;
private DaoMaster.DevOpenHelper openHelper;
private Context context;
private DBManager(Context context) {
this.context = context;
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
/**
* 获取单例引用
*
* @param context
* @return
*/
public static DBManager getInstance(Context context) {
if (mInstance == null) {
synchronized (DBManager.class) {
if (mInstance == null) {
mInstance = new DBManager(context);
}
}
}
return mInstance;
}
public SQLiteDatabase getReadableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
SQLiteDatabase db = openHelper.getReadableDatabase();
return db;
}
public SQLiteDatabase getWritableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
SQLiteDatabase db = openHelper.getWritableDatabase();
return db;
}
}
package com.example.greendaodemo.bean;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;
/**
* Created by yfeng on 2017/11/23.
*/
@Entity
public class User {
@Id
private Long id;
@Property
private String name;
@Property
private int age;
@Generated(hash = 1309193360)
public User(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Generated(hash = 586692638)
public User() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
//下面省去了 setter/getter
}
package com.example.greendaodemo.dao;
import android.content.Context;
import com.example.greendaodemo.db.DBManager;
import com.example.greendaodemo.bean.DaoMaster;
import com.example.greendaodemo.bean.DaoSession;
import com.example.greendaodemo.bean.User;
import com.example.greendaodemo.bean.UserDao;
import org.greenrobot.greendao.query.QueryBuilder;
import java.util.List;
/**
* Created by yfeng on 2017/11/23.
*/
public class MyUserDao {
Context context;
public MyUserDao(Context context) {
this.context = context;
}
/**
* 插入一条记录
*
* @param user
*/
public void insertUser(User user) {
DaoMaster daoMaster = new DaoMaster(DBManager.getInstance(context).getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.insert(user);
}
/**
* 插入用户集合
*
* @param users
*/
public void insertUserList(List users) {
if (users == null || users.isEmpty()) {
return;
}
DaoMaster daoMaster = new DaoMaster(DBManager.getInstance(context).getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.insertInTx(users);
}
/**
* 查询用户列表
*/
public List queryUserList() {
DaoMaster daoMaster = new DaoMaster(DBManager.getInstance(context).getReadableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
QueryBuilder qb = userDao.queryBuilder();
List list = qb.list();
return list;
}
/**
* 查询用户列表
*/
public List queryUserList(int age) {
DaoMaster daoMaster = new DaoMaster(DBManager.getInstance(context).getReadableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
QueryBuilder qb = userDao.queryBuilder();
qb.where(UserDao.Properties.Age.gt(age)).orderAsc(UserDao.Properties.Age);
List list = qb.list();
return list;
}
/**
* 更新一条记录
*
* @param user
*/
public void updateUser(User user) {
DaoMaster daoMaster = new DaoMaster(DBManager.getInstance(context).getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.update(user);
}
/**
* 删除一条记录
*
* @param user
*/
public void deleteUser(User user) {
DaoMaster daoMaster = new DaoMaster(DBManager.getInstance(context).getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.delete(user);
}
}