[MongoDB基础]-- 简化MongoAPI(spring-data-mongo)第三部分----Criteria和Query对象的使用

Criteria对象代表的是查询条件

 

1、举例说明

     (1)mongo sql 对应关系:

              db.users.find({name:"suns"});                 -------->Criteria criteria=new Criteria("name");

                                                                                                                criteria.is("suns");

     (2)两个查询条件:

               db.users.find({name:"suns",_id:22});----------->Criteria  criteria=new Criteria("name");

                                                                                                                 criteria.is("suns");

                                                                                                                 criteria.and("_id").is(22);

      (3)条件复杂(age>10)

               db.users.find({age:{$gt:10:}});           ------------>Criteria criteia=new Criteria("age");

                                                 criteria.gt("10");//如果是大于等于10,那么再添加一句话:criteria.and("age").is("10");即可

 2、Query对象代表本次查询:包含查询条件和分页、排序等

      (1)举例查询

         //a、查询条件

           Criteria  criteria =new Criteria("name");

                          criteria.is("suns");

                          criteria.and("_id").is(10);

          //b、分页、排序等

             Query      query=new Query();

                              query.addCriteria(criteria);

                query.sort();//排序

                query.limit();//分页

                queryskip();//分页                                                                  

3、演示代码进行增删改查

     (1)spring.xml中

        dbFactory"  host="127.0.0.1" dbname="baichi"/>

        mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

                  mongoDbFactory"/>

        

     

      (2)java代码集成测试

       @RunWithSpringJUnit4ClassRunner.class)

      @ContertConfiguration("spring.xml")

       public class TestTemplate{

            @AutoWeired

           private    MongoTemplate  mongoTemplate;//该属性名称必须与spring.xml中的id一致

          //1、测试数据插入

         @Test

           public void testSave(){

                Customer c=new Customer();

                         c.setId(1);

                         c.setName("suns1");

                         c.setPassword("123456");

                  mongoTemplate.save();

            //验证语句:db------->baichi----------->show  collections -------->db.customer.find();

              }

 

          //2、测试查询操作

         @Test

           public void testQuery(){

                Criteria criteria=new Criteria("customer_name");

                              criteria.is("suns1");

                 Query        q=new Query();

                                    q.addCriteria(criteria);

                   Customer c=mongoTemplate.findOne(q,Customer.class);

                   System.out.print("打印查询一条数据:"+c);

              }

          //3、测试更新操作(多行数据更新)

         @Test

           public void testUpdate(){

                Criteria criteria=new Criteria("customer_name");

                              criteria.is("suns1");

                 Query        q=new Query();

                                    q.addCriteria(criteria);

                  Update  up=new Update();

                                 up.set("password","88888888");//等价于$set{password,"88888888"}

                  //更新多行数据

                   mongoTemplate.updateMulti(q,up,Customer.class);//参数意义:更新的条件、更新的值、更新的类

              }

 

        //4、测试删除操作

         @Test

           public void testRemove(){

                Criteria criteria=new Criteria("customer_name");

                              criteria.is("suns1");

                 Query        q=new Query();

                                    q.addCriteria(criteria);

                  //根据条件删除数据

                   mongoTemplate.remove(q,Customer.class);//删除的条件、删除的类型

 

 

              }

 

        }

你可能感兴趣的:(mongodb,对象,spring,Mongodb)