Learning Spring Boot: (20) Using MongoDB

foreword

MongoDB 1 is an open source database that can be used by businesses of all sizes, industries, and applications. Database based on distributed file storage. Written in C++ language. It aims to provide scalable high-performance data storage solutions for WEB applications. MongoDB is a high-performance, open source, schema-less document database, and it is one of the most popular NoSql databases.

text

Spring Boot encapsulates MongoDB's data source operations.

add dependencies

In pom.xml add:

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

Configure connection parameters

Configure in the system configuration file:

spring:
  data:
    mongodb:
      uri: mongodb://wuwii:123456@localhost:27017/learn

test use

  1. Create entity
@Data
@Document(collection = "pet") // 标识要持久化到MongoDB的域对象。模型名是 pet
public class Pet implements Serializable {
    @Id
    //@Indexed(unique = true) // 使用MongoDB的索引特性标记一个字段
    private Long id;
    @Field("pet_name") //自定义设置对应MongoDB中的key
    private String name;
    private String species;
}
  1. Create dao interface to complete basic operations
@Repository
public class PetDaoImpl implements PetDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Pet find(Long id) {
        return mongoTemplate.findById(id, Pet.class);
    }

    @Override
    public List<Pet> findAll() {
        return mongoTemplate.findAll(Pet.class);
    }

    @Override
    public void add(Pet pet) {
        mongoTemplate.insert(pet);
    }

    @Override
    public void update(Pet pet) {
        Query query = new Query();
        Criteria criteria = new Criteria("id");
        criteria.is(pet.getId());
        query.addCriteria(criteria);
        Update update = new Update();
        update.set("pet_name", pet.getName())
                .set("species", pet.getSpecies());
        mongoTemplate.updateFirst(query, update, Pet.class); // 条件,更新的数据,更新的类型
    }

    @Override
    public void delete(Long id) {
        Criteria criteria = new Criteria("id");
        criteria.is(id);
        Query query = new Query();
        query.addCriteria(criteria);
        mongoTemplate.remove(query, Pet.class); // 删除的条件、删除的类型
    }
}
  1. simple test
@SpringBootTest
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PetDaoTest {

    @Autowired
    private PetDao petDao;

    private Pet pet;

    @Before
    public void before() {
        pet = new Pet();
        pet.setId(1L);
        pet.setName("Tom");
        pet.setSpecies("cat");
    }

    @After
    public void after() {
    }

    @Test
    public void test01Add() {
        Pet pet = new Pet();
        pet.setId(1L);
        pet.setName("Tom");
        pet.setSpecies("cat");
        petDao.add(pet);
    }

    @Test
    public void test02Find() {
        Assert.assertThat(pet, Matchers.equalTo(petDao.find(pet.getId())));
    }

    @Test
    public void test03FindAll() {
        System.out.println(petDao.findAll());
    }

    @Test
    public void test04Update() {
        pet.setName("KronChan");
        petDao.update(pet);
        Assert.assertThat(pet, Matchers.equalTo(petDao.find(pet.getId())));
    }

    @Test
    public void test05Delete() {
        petDao.delete(pet.getId());
        Assert.assertThat(null, Matchers.equalTo(petDao.find(pet.getId())));
    }

}

Go to the database to verify the results

> use learn
switched to db learn
> db.pet.find()
{ "_id" : NumberLong(1), "_class" : "com.wuwii.testmongodb.Pet", "pet_name" : "KronChan", "species" : "cat" }

Use of multiple data sources

undone


  1. From the English word "Humongous", which means "huge" in Chinese

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325632162&siteId=291194637