2.Mongodb

安装:mongodb

下载链接:https://www.mongodb.com/download-center#community

下载版本:mongodb-win32-x86_64-2008plus-ssl-3.2.9-signed.msi

安装出现 2502、2503 错误解决办法:

http://jingyan.baidu.com/article/a501d80cec07daec630f5e18.html

启动命令:mongod.exe --dbpath d:\mongodb\

指定路径:--dbpath

注:要先创建文件夹

一、 添加依赖

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

二、 配置文件:

# MONGODB (MongoProperties)
spring.data.mongodb.uri=mongodb://localhost/test
spring.data.mongodb.port=27017
#spring.data.mongodb.authentication-database=
#spring.data.mongodb.database=test
#spring.data.mongodb.field-naming-strategy=
#spring.data.mongodb.grid-fs-database=
#spring.data.mongodb.host=localhost
#spring.data.mongodb.password=
#spring.data.mongodb.repositories.enabled=true
#spring.data.mongodb.username=

三、 代码

主要涉及的文件

component/DemoMongodbComponent.java

package com.shuju.test2.component;

import com.shuju.test2.bean.DemoUser;
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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

@Component
public class DemoMongodbComponent {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void insert(DemoUser demoUser) {
        mongoTemplate.insert(demoUser);
    }

    public void deleteById(int id) {
        Criteria criteria = Criteria.where("id").in(id);
        Query query = new Query(criteria);
        mongoTemplate.remove(query, DemoUser.class);
    }

    public void updateById(DemoUser demoUser) {

        Criteria criteria = Criteria.where("id").in(demoUser.getId());
        Query query = new Query(criteria);
        Update update = new Update();

        update.set("name", demoUser.getName());
        update.set("createTime", demoUser.getCreateTime());
        mongoTemplate.updateMulti(query, update, DemoUser.class);

    }

    public DemoUser selectById(int id) {
        Criteria criteria = Criteria.where("id").in(id);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, DemoUser.class);
    }
}

bean/DemoUser.java

package com.shuju.test2.bean;

import java.util.Date;

public class DemoUser {

    private int id;
    private String name;
    private Date createTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "DemoUser{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", createTime=" + createTime +
                '}';
    }
}

测试

package com.shuju.test2;

import com.shuju.test2.bean.DemoUser;
import com.shuju.test2.component.DemoMongodbComponent;
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;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test2ApplicationTests {

    @Autowired
    private DemoMongodbComponent demoMongodbComponent;

    @Test
    public void set() {
        DemoUser demoUser = new DemoUser();
        demoUser.setId(1);
        demoUser.setName("虾米");
        demoUser.setCreateTime(new Date());
        demoMongodbComponent.insert(demoUser);
    }

    @Test
    public void select() {
        System.out.println(demoMongodbComponent.selectById(1));
    }

    @Test
    public void update() {
        DemoUser demoUser = new DemoUser();
        demoUser.setId(1);
        demoUser.setName("测试修改");
        demoUser.setCreateTime(new Date());
        demoMongodbComponent.updateById(demoUser);
        System.out.println(demoMongodbComponent.selectById(1));
    }

    @Test
    public void delete() {
        demoMongodbComponent.deleteById(1);
    }
}

四、使用:MongoRepository

加入bean/DemoUserLog.java

package com.shuju.test2;

import com.shuju.test2.bean.DemoUserLog;
import com.shuju.test2.dao.DemoUserLogMongoDao;
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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test2ApplicationTests {

    @Autowired
    private DemoUserLogMongoDao demoUserLogMongoDao;

    @Test
    public void insert() {
        DemoUserLog entity = new DemoUserLog();
        entity.setId(1);
        entity.setUserName("无境");
        entity.setUserIp("192.168.0.1");
        entity.setCreateTime(new Date());
        demoUserLogMongoDao.save(entity);
    }

    @Test
    public void delete() {
        demoUserLogMongoDao.deleteById(1);
    }

    @Test
    public void update() {

        DemoUserLog entity = new DemoUserLog();
        entity.setId(1);
        entity.setUserName("无境2");
        entity.setUserIp("192.168.0.1");
        entity.setCreateTime(new Date());
        demoUserLogMongoDao.save(entity);
    }

    @Test
    public void select() {
        DemoUserLog result = demoUserLogMongoDao.findById(1).get();
        System.out.println(result);
    }

    @Test
    public void select2() {
        DemoUserLog result = demoUserLogMongoDao.findByUserName("无境2");
        System.out.println(result);
    }

    //    分页
    @Test
    public void queryForPage() {
        Pageable pageable = new PageRequest(0, 20, new Sort(new Sort.Order(Direction.DESC, "id")));
    //  Page<DemoUserLog> result = demoUserLogDao.findByUserName("无境2", pageable);
        Page<DemoUserLog> result = demoUserLogMongoDao.findAll(pageable);
        System.out.println(result.getContent());
    }
}

dao/DemoUserLogMongoDao.java

package com.shuju.test2.dao;

import com.shuju.test2.bean.DemoUserLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface DemoUserLogMongoDao  extends MongoRepository<DemoUserLog, Integer> {

    DemoUserLog findByUserName(String username);

    DemoUserLog findByUserNameAndUserIp(String string, String ip);

    Page<DemoUserLog> findByUserName(String string, Pageable pageable);
}

测试类进行测试

package com.shuju.test2;

import com.shuju.test2.bean.DemoUserLog;
import com.shuju.test2.dao.DemoUserLogMongoDao;
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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test2ApplicationTests {

    @Autowired
    private DemoUserLogMongoDao demoUserLogMongoDao;

    @Test
    public void insert() {
        DemoUserLog entity = new DemoUserLog();
        entity.setId(1);
        entity.setUserName("无境");
        entity.setUserIp("192.168.0.1");
        entity.setCreateTime(new Date());
        demoUserLogMongoDao.save(entity);
    }

    @Test
    public void delete() {
        demoUserLogMongoDao.deleteById(1);
    }

    @Test
    public void update() {

        DemoUserLog entity = new DemoUserLog();
        entity.setId(1);
        entity.setUserName("无境2");
        entity.setUserIp("192.168.0.1");
        entity.setCreateTime(new Date());
        demoUserLogMongoDao.save(entity);
    }

    @Test
    public void select() {
        DemoUserLog result = demoUserLogMongoDao.findById(1).get();
        System.out.println(result);
    }

    @Test
    public void select2() {
        DemoUserLog result = demoUserLogMongoDao.findByUserName("无境2");
        System.out.println(result);
    }

    //    分页
    @Test
    public void queryForPage() {
        Pageable pageable = new PageRequest(0, 20, new Sort(new Sort.Order(Direction.DESC, "id")));
    //  Page<DemoUserLog> result = demoUserLogDao.findByUserName("无境2", pageable);
        Page<DemoUserLog> result = demoUserLogMongoDao.findAll(pageable);
        System.out.println(result.getContent());
    }
}

五、 使用嵌入式的 mongo

<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
</dependency>

注意:

  • 1.加入嵌入式的 mongo 之后,首次启动会进行下载,时间会比较久,请耐心等待

  • 2.下载完成,启动之后,默认情况下数据会在内存里面,重启会丢失

其他

设置打印日志 logback

<logger name="org.springframework.data.mongodb.core.MongoTemplate" level="debug"/>

springboot(十一):Spring boot中mongodb的使用

Last updated