bean 存放数据库的表
BDDaoPresenter (逻辑层,相当于web中的P层)
gen放greendao生成的类。
BDDaoPresenter 显示对数据库的管理的工具类
DBManager:用于对数据库的开启关闭,加密等操作。
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.vpubaopos.base.BaseActivity;
import com.vpubaopos.offline.greendao.gen.DaoMaster;
import com.vpubaopos.utils.Util;
import org.greenrobot.greendao.database.Database;
/**
* Created by 51644 on 2018/1/9.
* greendao 工具类
*/
public class DBManager {
private final static String dbName = "VpubaoOfflineDataDB";
private static DBManager mInstance;
private DaoMaster.DevOpenHelper openHelper;
private Context context;
public 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 Database getReadableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
Database db = openHelper.getEncryptedWritableDb(Util.getImei(context, BaseActivity.currentDevice));
return db;
}
/**
* 获取可写数据库
*/
public Database getWritableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
Database db = openHelper.getEncryptedWritableDb(Util.getImei(context, BaseActivity.currentDevice));
// SQLiteDatabase db = openHelper.getWritableDatabase();
return db;
}
}
每张表建立一个管理类(在此类中封装对数据库的操作)
import android.content.Context;
import com.vpubaopos.offline.greendao.bean.DBCategory;
import com.vpubaopos.offline.greendao.bean.DBGood;
import com.vpubaopos.offline.greendao.gen.DBCategoryDao;
import com.vpubaopos.offline.greendao.gen.DaoMaster;
import com.vpubaopos.offline.greendao.gen.DaoSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by 51644 on 2018/1/9.
* 分类表管理类
*/
public class PCategoryDB {
private static PCategoryDB mInstance;
private Context context;
DBCategoryDao dbCategoryDao;
public PCategoryDB(Context context) {
this.context = context;
DBManager dbManager = DBManager.getInstance(context);
DaoMaster daoMaster = new DaoMaster(dbManager.getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
dbCategoryDao = daoSession.getDBCategoryDao();
}
/**
* 获取单例引用
*
* @param context
* @return
*/
public static PCategoryDB getInstance(Context context) {
if (mInstance == null) {
mInstance = new PCategoryDB(context);
}
return mInstance;
}
//批量插入
public void createCategory(String content) {
List list = DBCategory.getDBCategoryList(content);
dbCategoryDao.insertInTx(list);
}
//批量更新
public void updataCategory(String content) {
if (content == null || content.equals("")) {
return;
}
List list = DBCategory.getDBCategoryList(content);
for (DBCategory c : list) {
doUpDataCategoryByText(c);
}
}
//更新
private void doUpDataCategoryByText(DBCategory c) {
DBCategory o = selectByID(c.getCat_id() + "");
if (o == null) {
dbCategoryDao.insert(c);
} else {
o.setLevel(c.getLevel());
....
dbCategoryDao.update(o);
}
}
//查询所有
public List selectAll() {
return dbCategoryDao.loadAll();
}
//通过cat_id查
public DBCategory selectByID(String cat_id) {
return dbCategoryDao.queryBuilder().limit(1).where(DBCategoryDao.Properties.Cat_id.eq(cat_id)).unique();
}
//获取购物列表的显示分类
public ArrayList selectCategoryForGoodListShow() {
ArrayList showCategory = new ArrayList<>();
DBCategory dbCategory = new DBCategory();
dbCategory.setCat_name("全部分类");
showCategory.add(dbCategory);
List allGoods = PGoodDB.getInstance(context).selectAll();
....
return showCategory;
}
}
使用:
dbCategories = PCategoryDB.getInstance(getActivity()).selectCategoryForGoodListShow();
欢迎关注我Github : @ https://github.com/yuzelli