SpringBoot使用MongoDB

90 阅读2分钟

MongoDB

Docker创建MongDB

docker run -id --name=mongo -p 27017:27017 mongo

SpringBoot整合MongoDB

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

application.yml

spring:
  data:
    mongodb:
      host: 192.168.0.0:27017
      database: spitdb

MongoDao

import com.ktc.spit.pojo.Spit;
import org.springframework.data.mongodb.repository.MongoRepository;
​
public interface SpitDao extends MongoRepository<Spit,String> {
}

MongoTest

mongoTemplate是后来参考别人的文章手写写上去,有问题我记得再补

@SpringBootTest(classes = MongoTestApplication.class)
@RunWith(SpringRunner.class)
public class Demo01_MongoRepository {
​
    @Autowired
    private SpitDao spitDao;
    
    @Autowired
    private MongoTemplate mongoTemplate;
​
    /**
     * 新增
     *
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
​
        Spit spit = new Spit();
        spit.setId("1");
        spit.setContent("今天天气不错");
        spit.setNickname("小东");
        spit.setUserid("1");
        spit.setVisits(200);
​
        spitDao.save(spit);
    }
​
    /**
     * 修改
     *
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {
​
        Spit spit = new Spit();
        spit.setId("1");
        spit.setContent("今天天气不好啊");
        spit.setNickname("小准");
        spit.setUserid("4");
        spit.setVisits(200);
​
        spitDao.save(spit);
    }
​
    /**
     * 查询全部
     *
     * @throws Exception
     */
    @Test
    public void test3() throws Exception {
​
        List<Spit> spitList = spitDao.findAll();
        for (Spit spit : spitList) {
            System.out.println(spit);
        }
    }
​
    /**
     * 根据id查询
     *
     * @throws Exception
     */
    @Test
    public void test4() throws Exception {
        Spit spit = spitDao.findById("1").get();
        System.out.println(spit);
    }
​
    /**
     * 删除
     *
     * @throws Exception
     */
    @Test
    public void test5() throws Exception {
        spitDao.deleteById("1");
    }
​
    /**
     * 方法命名查询
     *
     * @throws Exception
     */
    @Test
    public void test6() throws Exception {
//        List<Spit> spitList = spitDao.findByContentLike("天");// 模糊查询不需要加%
//        List<Spit> spitList = spitDao.findByVisitsGreaterThan(800);
        List<Spit> spitList = spitDao.findByNicknameAndContentLike("小东", "天");
        for (Spit spit : spitList) {
            System.out.println(spit);
        }
    }
    /**
     * 查询Spit文档的全部数据
     *
     * @throws Exception
     */
    @Test
    public void test7() throws Exception {
        List<Spit> spitList = mongoTemplate.findAll(Spit.class);
        
        //排序
        //Query query = new Query();
        //query.with(Sort.by("visits").ascending()); //升序
        //query.with(Sort.by("visits").descending()); //降序
        //List<Spit> spitList = mongoTemplate.find(query, Spit.class);
        //分页
        //Query query = new Query();
        //query.with(PageRequest.of(0, 2));
        //List<Spit> spitList = mongoTemplate.find(query, Spit.class);
        
        for (Spit spit : spitList) {
            System.out.println(spit);
        }
    }
    /**
     * 查询Spit文档id为1的数据
     *
     * @throws Exception
     */
    @Test
    public void test8() throws Exception {
        List<Spit> spitList = mongoTemplate.findById("1", Spit.class);
        for (Spit spit : spitList) {
            System.out.println(spit);
        }
    }
    /**
     * 根据query内的查询条件查询
     *
     * @throws Exception
     */
    @Test
    public void test9() throws Exception {
        // 查询对象
        //Query query = new Query();
​
        //条件查询
        //Criteria criteria=new Criteria("userid");
        //criteria.is("1");
​
        //Criteria criteria = Criteria.where("userid").is("1");// 精确匹配
        //Criteria criteria = Criteria.where("content").regex(".*是.*");// 正则表达式
        //Criteria criteria = Criteria.where("visits").gt(1000);// 大于
        //Criteria criteria = Criteria.where("userid").in("1","2");// 包含
        //Criteria criteria = Criteria.where("userid").nin("1", "2");// 不包含
        
        //多条件合成
        //Criteria c_1 = Criteria.where("userid").is("1");// 条件1
        //Criteria c_2 = Criteria.where("visits").gt(2000);// 条件2
        //Criteria criteria=new Criteria();
        //criteria.andOperator(c_1, c_2);//封装成一个条件
​
        // 将查询条件添加到Query中
        //query.addCriteria(criteria);
        
        
        Query query = new Query(Criteria.where("content").is("天气"));
        List<Spit> spitList = mongoTemplate.find(query, Spit.class);
        for (Spit spit : spitList) {
            System.out.println(spit);
        }
    }
    /**
     * 修改
     *
     * @throws Exception
     */
    @Test
    public void test10() throws Exception {
        
        //Spit spit = mongoTemplate.findById("1",Spit.class)
        //spit.setContent("今天天气不太好");
        //spit.setNickname("小西");
        
        Query query = new Query(Criteria.where("id").is("1"));
        
        Update update = new Update();
        update.set("content","今天天气不太好");
        update.set("nickname","小西");
        
        //mongoTemplate.updateFirst(query, update, Spit.class);// 修改符合条件的第一条数据
        //mongoTemplate.updateMulti(query, update, Spit.class);// 修改全部符合条件的数据
        mongoTemplate.upsert(query, update, Spit.class);// 如果不存在时则添加
        
    }
    /**
     * 删除
     *
     * @throws Exception
     */
    @Test
    public void test11() throws Exception {
        Query query = new Query(Criteria.where("id").is("1"));
        mongoTemplate.remove(query, Spit.class);
    }
    /**
     * 新增
     *
     * @throws Exception
     */
    @Test
    public void test12() throws Exception {
        Spit spit = new Spit();
        spit.setId("10");
        spit.setContent("今天天气不错");
        spit.setNickname("小龙");
        spit.setUserid("10");
        spit.setVisits(200);
        
        mongoTemplate.insert(spit);
    }
​
}

相关资料

mongoTemplate:Spring boot集成mongodb_spring整合mongo_小码哥呀的博客-CSDN博客