1.依赖包
implementation 'com.google.code.gson:gson:2.6.2'
//加密
implementation "net.zetetic:android-database-sqlcipher:4.3.0@aar"
implementation "androidx.sqlite:sqlite:2.0.1"
2.model
@Entity
public class GoodsModel implements Parcelable {
@Id(autoincrement = true)
private Long id;
private Integer goodsId;
private String name;
private String icon;
private String info;
private String type;
build --> make module
生成DaoMaster,DaoSession,GoodsModelDao
3.myApplication
public class MyApplication extends Application {
public static DaoSession mDaoSession;
@Override
public void onCreate() {
super.onCreate();
initDb();
}
public void initDb() {
//1.获取需要连接的数据库
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "goods2.db");
//SQLiteDatabase database = devOpenHelper.getWritableDatabase();
//加密
Database database = devOpenHelper.getEncryptedWritableDb("123456");
//2.创建数据库连接
DaoMaster daoMaster = new DaoMaster(database);
//3.创建数据库会话
mDaoSession = daoMaster.newSession();
}
}
manifest 注册
4.GreenDaoManager
public class GreenDaoManager {
private Context mContext;
private GoodsModelDao goodsModelDao;
public GreenDaoManager(Context mContext) {
this.mContext = mContext;
goodsModelDao = MyApplication.mDaoSession.getGoodsModelDao();
}
//添加所有数据到数据库
public void insertGoods() {
String goodsJson = DataUtil.getJSon("goods.json", mContext);
ArrayList<GoodsModel> goodsModels=DataUtil.getGoodList(goodsJson);
goodsModelDao.insertInTx(goodsModels);
}
//查询所有数据
public ArrayList<GoodsModel> queryGoods(){
QueryBuilder<GoodsModel> queryBuilder=goodsModelDao.queryBuilder()
.orderAsc(GoodsModelDao.Properties.GoodsId);
return (ArrayList<GoodsModel>) queryBuilder.list();
}
//查询所有水果
public ArrayList<GoodsModel> queryFruits(){
QueryBuilder<GoodsModel> queryBuilder=goodsModelDao.queryBuilder()
.where(GoodsModelDao.Properties.Type.eq(0))
.orderAsc(GoodsModelDao.Properties.GoodsId);
return (ArrayList<GoodsModel>) queryBuilder.list();
}
//查询所有零食
public ArrayList<GoodsModel> querySnacks(){
QueryBuilder<GoodsModel> queryBuilder=goodsModelDao.queryBuilder()
.where(GoodsModelDao.Properties.Type.eq(1))
.orderAsc(GoodsModelDao.Properties.GoodsId);
return (ArrayList<GoodsModel>) queryBuilder.list();
}
//删除一条数据
public void deleteGood(GoodsModel model){
goodsModelDao.delete(model);
}
//修改数据
public void updateGood(GoodsModel model){
goodsModelDao.update(model);
}
}
5.使用
private GreenDaoManager greenDaoManager;
greenDaoManager = new GreenDaoManager(this);
public void myClick(View view) {
switch (view.getId()) {
case R.id.add_goods_btn:
greenDaoManager.insertGoods();
break;
case R.id.all_goods_btn:
goodsModels=greenDaoManager.queryGoods();
adapter.setGoodsList(goodsModels);
break;
case R.id.fruit_goods_btn:
goodsModels=greenDaoManager.queryFruits();
adapter.setGoodsList(goodsModels);
break;
case R.id.snack_goods_btn:
goodsModels=greenDaoManager.querySnacks();
adapter.setGoodsList(goodsModels);
break;
}
}
private void addEvents() {
updateGoodsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String info=goodDetalEt.getText().toString();
goods.setInfo(info);
greenDaoManager.updateGood(goods);
onBackPressed();
}
});
removeGoodsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(GoodsDetailActivity.this);
builder.setMessage("确认删除?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
greenDaoManager.deleteGood(goods);
finish();
}
});
builder.setNegativeButton("取消",null);
builder.show();
}
});
}