Fork me on GitHub

SpringBoot 集成mongodb(1)单数据源配置

新项目要用到mongodb,于是在个人电脑上的虚拟环境linux上安装了下mongodb,练习熟悉下。

1、虚拟机上启动mongodb。

首先查看虚拟机ip地址,忘了哈~~

命令行>ifconfig 

mongodb安装目录bin>sudo ./mongod -f mongodb.conf

检查是否已经启动:>netstat -lanp|grep "27017"

也可以用客户端连接:

2、用SpringBoot建立mongoProj工程;

2.1、配置文件设置

pom.xml

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

首先单数据源配置,具体如下:

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

2.2、编写实体类,本例mongo中的集合随便起的名字,这里创建一个Person类

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、创建DAO层:

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));
        //这里需要指定col集合
        Person p = mongoTemplate.findOne(query,Person.class,"col");
        return p;
    }
}

2.4、测试类:

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);
    }
}

 

测试,结果:

 

posted @ 2019-06-30 22:27  小传风  阅读(381)  评论(0编辑  收藏  举报