Bmob crud详细使用及数据类型介绍

设置表名

当自定义类进行数据存储时,默认表名为类名,当时可以在构造方法中调用setTableName函数自定义表名,但是文档推荐表名和类名一致,因为表名和类名不一致在其他操作时会出现序列化的问题

setTableName("表名");
public class Person extends BmobObject {
    private String name;
    private String address;
    public Person(){
        setTableName("NPerson");
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

自定义表名下的数据查询

		BmobQuery query=new BmobQuery("Person");
        query.getObjectByTable("5628796c96",new QueryListener<JSONObject>() {
            @Override
            public void done(JSONObject jsonObject, BmobException e) {
                Toast.makeText(MainActivity.this, jsonObject.toString(), Toast.LENGTH_SHORT).show();
            }
        });

通过键值对的方式插入

		Person person=new Person();
		//属性名,内容
        person.setValue("name","myname");
        person.setValue("address","myaddress");
        person.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                //BmobException为空为正常执行,不为空为异常
                if(e!=null){
                    Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
                } else{
                    Toast.makeText(MainActivity.this,"sucess",Toast.LENGTH_LONG).show();
                }
            }
        });

原子计数器

当一个字段多次操作容易出错通过原子计数器的方式来确保数据的准确性

		Person person = new Person();
        person.increment("num", 1);
        person.update("69da11a542", new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Toast.makeText(MainActivity.this, "sucess", Toast.LENGTH_SHORT).show();
                }
            }
        });

删除字段的值

删除某个字段的值,使字段的值为默认状态

		Person person=new Person();
        person.remove("age");
        person.update("69da11a542", new UpdateListener() {
            @Override
            public void done(BmobException e) {
                Toast.makeText(MainActivity.this,"sucess" , Toast.LENGTH_SHORT).show();
            }
        });

批量操作详解之批量插入数据

		List<BmobObject> list=new ArrayList<>();
        for(int i=0;i<10;i++){
            Person p=new Person();
            p.setName("name"+i);
            p.setAddress("address"+i);
            list.add(p);
        }
        new BmobBatch().insertBatch(list).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> list, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess"+list.size(), Toast.LENGTH_SHORT).show();
                }
            }
        });

批量操作详解之批量更新数据

		List<BmobObject> list=new ArrayList<>();
        Person person1=new Person();
        person1.setObjectId("69da11a542");
        person1.setNum(32423423);
        list.add(person1);
        Person person2=new Person();
        person2.setObjectId("a2874a0783");
        person2.setNum(324);
        list.add(person2);
        new BmobBatch().updateBatch(list).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> list, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess"+list.size(), Toast.LENGTH_SHORT).show();
                }
            }
        });

批量操作详解之批量删除数据

		List<BmobObject> list=new ArrayList<>();
        Person person1=new Person();
        person1.setObjectId("69da11a542");
        list.add(person1);
        Person person2=new Person();
        person2.setObjectId("a2874a0783");
        list.add(person2);
        new BmobBatch().deleteBatch(list).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> list, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess"+list.size(), Toast.LENGTH_SHORT).show();
                }
            }
        });

批量操作详解之同时提交增删改

		BmobBatch bmobBatch=new BmobBatch();
        //插入
        List<BmobObject> list1=new ArrayList<>();
        for(int i=0;i<10;i++){
            Person p=new Person();
            p.setName("name"+i);
            p.setAddress("address"+i);
            list1.add(p);
        }
        bmobBatch.insertBatch(list1);


        //更新
        List<BmobObject> list2=new ArrayList<>();
        Person person1=new Person();
        person1.setObjectId("69da11a542");
        person1.setNum(111);
        list2.add(person1);
        Person person2=new Person();
        person2.setObjectId("a2874a0783");
        person2.setNum(111);
        list2.add(person2);
        bmobBatch.updateBatch(list2);


        //删除
        List<BmobObject> list3=new ArrayList<>();
        Person person3=new Person();
        person3.setObjectId("29090e3a31");
        list3.add(person3);
        bmobBatch.deleteBatch(list3);

        //提交
        bmobBatch.doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> list, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess", Toast.LENGTH_SHORT).show();
                }
            }
        });

数据查询详解之多条数据查询

		BmobQuery<Person> bmobQuery=new BmobQuery<>();
        bmobQuery.addWhereEqualTo("num",0);
        bmobQuery.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> list, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess"+list.size(), Toast.LENGTH_SHORT).show();
                }
            }
        });

数据查询详解之条件筛选查询

		BmobQuery<Person> bmobQuery=new BmobQuery<>();
        bmobQuery.addWhereGreaterThan("num",0);
        bmobQuery.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> list, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess"+list.size(), Toast.LENGTH_SHORT).show();
                }
            }
        });

数据查询详解之条件子查询

		BmobQuery<Person> bmobQuery=new BmobQuery<>();
        bmobQuery.addWhereContainedIn("num", Arrays.asList(new Integer[]{0,1}));
        bmobQuery.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> list, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess"+list.size(), Toast.LENGTH_SHORT).show();
                }
            }
        });

数据查询详解之时间查询

		BmobQuery<Person> bmobQuery=new BmobQuery<>();
        Date date=new Date(System.currentTimeMillis());
        BmobDate bmobDate=new BmobDate(date);
        bmobQuery.addWhereLessThan("createdAt",bmobDate);
        bmobQuery.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> list, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess"+list.size(), Toast.LENGTH_SHORT).show();
                }
            }
        });

数据查询详解之分页查询

		BmobQuery<Person> bmobQuery=new BmobQuery<>();
		//限定3条
        bmobQuery.setLimit(3);
        //忽略前三条
        bmobQuery.setSkip(3);
		BmobQuery<Person> bmobQuery=new BmobQuery<>();
        bmobQuery.addWhereExists("objectId");
        int page=3;
        bmobQuery.setLimit((page-1)*2);
        bmobQuery.setSkip(2);
        bmobQuery.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> list, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess"+list.size(), Toast.LENGTH_SHORT).show();
                }
            }
        });

数据查询详解之排序

BmobQuery<Person> bmobQuery=new BmobQuery<>();
        bmobQuery.addWhereExists("num");
        bmobQuery.order("num");
        bmobQuery.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> list, BmobException e) {
                tv.setText(list.toString());
            }
        });

数据查询详解之复合查询

//num>-1;
        BmobQuery<Person> greater=new BmobQuery<>();
        greater.addWhereGreaterThan("num",-1);
        //num<2;
        BmobQuery<Person> less=new BmobQuery<>();
        less.addWhereLessThan("num",2);
        //and
        List<BmobQuery<Person>> list=new ArrayList<>();
        list.add(greater);
        list.add(less);
        //find
        BmobQuery<Person> query=new BmobQuery<>();
        query.and(list);
        query.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> list, BmobException e) {
                if(e==null){
                    tv.setText(list.toString());
                }
            }
        });

数据查询详解之查询结果计数

BmobQuery<Person> bmobQuery=new BmobQuery<>();
        bmobQuery.addWhereExists("objectId");
        bmobQuery.count(Person.class, new CountListener() {
            @Override
            public void done(Integer integer, BmobException e) {
                if(e==null){
                    tv.setText(String.valueOf(integer));
                }
            }
        });

数据查询详解之统计查询

BmobQuery bmobQuery=new BmobQuery();
        bmobQuery.addWhereExists("objectId");
        bmobQuery.sum(new String[]{"num"});
        bmobQuery.findStatistics(Person.class, new QueryListener<JSONArray>() {
            @Override
            public void done(JSONArray jsonArray, BmobException e) {
                //jsonArray函数名+统计结果
                tv.setText(jsonArray.toString());
            }
        });

数据查询详解之分组统计

        BmobQuery bmobQuery=new BmobQuery();
        bmobQuery.average(new String[]{"num"});
        bmobQuery.groupby(new String[]{"name"});
        bmobQuery.findStatistics(Person.class, new QueryListener<JSONArray>() {
            @Override
            public void done(JSONArray jsonArray, BmobException e) {
                tv.setText(jsonArray.toString());
            }
        });

数组

在Bmob手动添加一列,代表数组
通过List赋值

        Person person=new Person();
        List list=new ArrayList<String>();
        list.add("address");
        list.add("address2");
        person.addArray(list);
        person.update("b133579017", new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess", Toast.LENGTH_SHORT).show();
                }
            }
        });

通过Array.asList赋值

		Person person=new Person();
        person.addAll("myArray",Arrays.asList("34324","323"));
        person.update("b133579017", new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess", Toast.LENGTH_SHORT).show();
                }
            }
        });

去重插入到数组中

Person person=new Person();
        person.addAllUnique("myArray",Arrays.asList("工作","工作","工作","工作","象征"));
        person.update("b133579017", new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess", Toast.LENGTH_SHORT).show();
                }
            }
        });

数组删除内容的操作

		Person person=new Person();
        person.removeAll("myArray",Arrays.asList("哈哈哈,我改变了","address2"));
        person.update("b133579017", new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "sucess", Toast.LENGTH_SHORT).show();
                }
            }
        });
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值