最近想搞一下基于Bmob平台的应用开发,发现确实挺方便的,很好的解决了服务器后台部署的难题,
但是也有一些弊端,数据架构的可扩展性不强,做一些数据结构简单的应用还是可以的。
package com.bmob.utils; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import android.content.Context; import android.util.Log; import android.widget.Toast; import cn.bmob.v3.BmobObject; import cn.bmob.v3.BmobQuery; import cn.bmob.v3.listener.DeleteListener; import cn.bmob.v3.listener.FindCallback; import cn.bmob.v3.listener.FindListener; import cn.bmob.v3.listener.GetListener; import cn.bmob.v3.listener.SaveListener; import cn.bmob.v3.listener.UpdateListener; import com.bmob.base.BombApplication; /** * @Date 2016-6-12 下午2:17:55 * @Author Arvin * @Description Bmob数据处理工具类 */ public class BmobUtils { public static final String TAG = "BmobUtils"; public static Context mContext = BombApplication.getInstance(); public static List<BmobObject> queryBeans = new ArrayList<BmobObject>(); public static BmobObject bmobObject = new BmobObject(); /*****************************数据保存************************/ /** * @Description 批量数据保存 * @param List<BmobObject> * @return void * @throws */ public static void saveBatch(List<BmobObject> list) { new BmobObject().insertBatch(mContext, list, new SaveListener() { @Override public void onSuccess() { showToast("批量增加成功"); } @Override public void onFailure(int arg0, String arg1) { showToast(arg1 + "批量增加失败"); } }); } /** * @Description 单一数据保存 * @param BmobObject * @return return_type * @throws */ public static void saveSignle(BmobObject object) { object.save(mContext, new SaveListener() { @Override public void onSuccess() { showToast("增加成功"); } @Override public void onFailure(int arg0, String arg1) { showToast(arg1 + "增加失败"); } }); } /*****************************查询*************************/ /** * @Description 查询所有数据并封装(不支持指定tabName) * @param null * @return void * @throws */ public static void queryTable() { BmobQuery<BmobObject> query = new BmobQuery<BmobObject>(); query.findObjects(mContext, new FindListener<BmobObject>() { @Override public void onSuccess(List<BmobObject> objects) { // TODO Auto-generated method stub showToast("查询成功"); } @Override public void onError(int arg0, String arg1) { showToast(arg1 + "查询失败"); } }); } /** * @Description 查询指定table中所有数据解析并封装(解析JSONArray) * @param null * @return void * @throws */ public static void queryData(String tabName){ BmobQuery query = new BmobQuery(tabName); query.findObjects(mContext , new FindCallback() { @Override public void onSuccess(JSONArray jsonArray) { //注意:查询的结果是JSONArray,需要自行解析 Log.i(TAG, "查询成功:"+jsonArray.length()); } @Override public void onFailure(int arg0, String arg1) { Log.i(TAG, "查询失败:"+arg1); } }); } /** * @Description 传值,指定字段进行条件查询(不支持指定tabName) * @param String key , String searchVal * @return void * @throws */ public static void queryName(String key , String searchVal) { if (searchVal.equals("")) { return; } BmobQuery<BmobObject> query = new BmobQuery<BmobObject>(); query.addWhereEqualTo(key, searchVal); //条件查询 query.findObjects(mContext, new FindListener<BmobObject>() { @Override public void onSuccess(List<BmobObject> objects) { showToast("查询成功"); //实现数据封装的逻辑 } @Override public void onError(int arg0, String arg1) { // TODO Auto-generated method stub showToast(arg1+"查询失败"); } }); } /** * @Description 根据objectId ,查询单条数据(不支持指定tabName) * @param String objectId * @return return_type * @throws */ public static void querySingleData(String objectId){ BmobQuery<BmobObject> query = new BmobQuery<BmobObject>(); query.getObject(mContext, objectId, new GetListener<BmobObject>() { @Override public void onSuccess(BmobObject object) { // TODO Auto-generated method stub showToast("查询成功:"); //实现数据封装的逻辑 } @Override public void onFailure(int code, String arg0) { // TODO Auto-generated method stub showToast("查询失败:" + arg0); } }); } /** * @Description 查询多条数据 /根据列值查询 (不支持指定tabName) * @param String key , String queryVal * @return return_type * @throws */ public static void queryMultiData(String key , String queryVal){ BmobQuery<BmobObject> query = new BmobQuery<BmobObject>(); query.addWhereEqualTo(key, queryVal); //条件查询 //返回50条数据,如果不加上这条语句,默认返回10条数据 query.setLimit(); //执行查询方法 query.findObjects(mContext, new FindListener<BmobObject>() { @Override public void onSuccess(List<BmobObject> objects) { // TODO Auto-generated method stub showToast("查询成功:共" + objects.size() + "条数据。"); //实现数据封装的逻辑 } @Override public void onError(int code, String msg) { // TODO Auto-generated method stub showToast("查询失败:" + msg); } }); } /** * @Description 查询指定列 (不支持指定tabName) * @param String key * @return void * @throws */ public static void queryRow(String key){ //只返回Lost表的objectId这列的值 BmobQuery<BmobObject> bmobQuery = new BmobQuery<BmobObject>(); bmobQuery.addQueryKeys(key); bmobQuery.findObjects(mContext, new FindListener<BmobObject>() { @Override public void onSuccess(List<BmobObject> objects) { // TODO Auto-generated method stub showToast("查询成功:共" + objects.size() + "条数据。"); //实现数据封装的逻辑 } @Override public void onError(int code, String msg) { // TODO Auto-generated method stub showToast("查询失败:" + msg); } }); } /** * @Description 对数据进行排序 * @param BmobQuery<BmobObject> query , String key * @return void * @throws */ public static void orderData(BmobQuery<BmobObject> query , String key){ // 根据score字段升序显示数据 query.order("key"); // 根据score字段降序显示数据 query.order("-key"); } /********************************数据更新******************************/ /** * @Description 批量更新数据 * @param List<BmobObject> list * @return void * @throws */ public static void updateBatch(List<BmobObject> list) { if(list.size() == ){ showToast("数据不存在,请先插入"); return; } new BmobObject().updateBatch(mContext, list, new UpdateListener() { @Override public void onSuccess() { showToast("批量修改成功"); } @Override public void onFailure(int arg0, String arg1) { showToast(arg1 + "批量修改失败"); } }); } /** * @Description 根据指定的objectId修改元素 * @param BmobObject , String * @return void * @throws */ public static void updateById(BmobObject bmobObject , String objectId){ bmobObject.update(mContext, objectId, new UpdateListener() { @Override public void onSuccess() { showToast("修改成功"); } @Override public void onFailure(int arg0, String arg1) { showToast(arg1 + "修改失败"); } }); } /** * @Description 修改更新BmobObject对象 * @param BmobObject * @return void * @throws */ public static void updateObject(BmobObject bmobObject){ bmobObject.update(mContext , new UpdateListener() { @Override public void onSuccess() { // TODO Auto-generated method stub showToast("修改成功"); } @Override public void onFailure(int arg0, String arg1) { // TODO Auto-generated method stub showToast(arg1 + "修改失败"); } }); } /***********************************数据删除***********************************/ /** * @Description 批量删除数据 * @param tags * @return return_type * @throws */ public static void deleteBatch(List<BmobObject> objects) { new BmobObject().deleteBatch(mContext, objects, new DeleteListener() { @Override public void onSuccess() { showToast("批量删除成功"); } @Override public void onFailure(int arg0, String arg1) { showToast(arg1 + "批量删除失败"); } }); } /** * @Description 根据objectId删除BmobObject对象 * @param BmobObject ,String * @return void * @throws */ public static void deleteById(BmobObject object , String objectId) { //根据指定的objectId修改元素 object.delete(mContext , objectId , new DeleteListener() { @Override public void onSuccess() { showToast("删除成功"); } @Override public void onFailure(int arg0, String arg1) { showToast(arg1 + "删除失败"); } }); } /** * @Description 删除BmobObject对象 * @param BmobObject * @return void * @throws */ public static void deleteData(BmobObject object) { object.delete(mContext, new DeleteListener() { @Override public void onSuccess() { showToast("删除成功"); } @Override public void onFailure(int arg0, String arg1) { showToast("删除失败"); } }); } /** * @Description 显示Toast信息(msg) * @param tags * @return return_type * @throws */ public static void showToast(String msg){ Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); } /** * @Description 显示Toast信息(id) * @param tags * @return return_type * @throws */ public static void showToast(int resId){ Toast.makeText(mContext, resId, Toast.LENGTH_SHORT).show(); } }