Spring boot integrated mongodb single data source configuration

Keywords: PHP MongoDB Spring Junit Linux

Mongodb is used in the new project, so mongodb is installed on the virtual environment linux on the personal computer. Practice and get familiar with it.

1. Start mongodb on the virtual machine.

First check the ip address of the virtual machine, forget ha~~

Command line > ifconfig

Mongodb installation directory bin > sudo. / mongod - F mongodb.conf

Check if it has started:

You can also connect with the client:

2. Build mongoProj project with SpringBoot;

2.1 configuration file settings

pom.xml

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

First, configure a single data source as follows:

#mongodb configuration
spring.data.mongodb.uri=mongodb://long:long@192.168.0.102:27017/long

2.2. Write entity class. In this example, the collection in mongo is named casually. Here, create a Person class.

package com.example.demo.dto;

public class Person {
    private static final long serialVersionUID = -3258839839160856613L;

    private String name;

    private int age;

    private String gender;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

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

2.3 create DAO layer:

package com.example.demo.dao;

import com.example.demo.dto.Person;

public interface IPersonDao {
    public Person findPersonByName(String name);
}
package com.example.demo.dao.impl;

import com.example.demo.dao.IPersonDao;
import com.example.demo.dto.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

@Repository
public class PersonDaoImpl implements IPersonDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Person findPersonByName(String name) {
        Query query=new Query(Criteria.where("name").is(name));
        //You need to specify col aggregate
        Person p = mongoTemplate.findOne(query,Person.class,"col");
        return p;
    }
}

2.4 test type:

package com.example.demo;

import com.example.demo.dao.IPersonDao;
import com.example.demo.dto.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MongoDbTest {
    @Autowired
    private IPersonDao ipersonDao;

    @Test
    public void findPersonByName(){
        Person p =ipersonDao.findPersonByName("Jkson");
        System.out.println(">>>>Person ="+p);
    }
}

 

Test results:

Posted by coho75 on Fri, 01 Nov 2019 18:09:21 -0700