SpringBoot集成MongoDB

SpringBoot集成MongoDB非常的简单。

依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
MongoDB配置
spring.data.mongodb.uri=mongodb://10.236.93.211:27017/hulk

注意:我MongoDB服务没有设置密码

创建一个实体
package cc.koit.hulk.dao.entity;

import org.springframework.data.annotation.Id;

import java.util.Date;

public class Log {

    @Id
    private String id;

    private int type;

    private String remark;

    private Date operateTime;

    public Log() {
    }

    public Log(int type, String remark, Date operateTime) {
        this.type = type;
        this.remark = remark;
        this.operateTime = operateTime;
    }

    public String getId() {
        return id;
    }

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

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Date getOperateTime() {
        return operateTime;
    }

    public void setOperateTime(Date operateTime) {
        this.operateTime = operateTime;
    }

    @Override
    public String toString() {
        return "Log{" +
                "id=" + id +
                ", type=" + type +
                ", remark='" + remark + '\'' +
                ", operateTime=" + operateTime +
                '}';
    }
}

使用@Id标注ID列,该ID是String类型。

DAO
package cc.koit.hulk.dao.mapper;

import cc.koit.hulk.dao.entity.Log;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface LogRepository extends MongoRepository<Log, String> {

    public List<Log> findByType(int type);

    public List<Log> findAll();

}

测试用例
package cc.koit.hulk.service;

import cc.koit.hulk.dao.entity.Log;
import cc.koit.hulk.dao.mapper.LogRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
@Component
public class MongoTests{

   public static final Logger logger = LoggerFactory.getLogger(MongoTests.class);

    @Autowired
    private LogRepository logRepository;

    @Test
    public void testMongo(){
        logRepository.save(new Log(1,"插入第一条日志",new Date()));
        logRepository.save(new Log(1,"插入第二条日志",new Date()));
        logRepository.save(new Log(2,"插入第三条日志",new Date()));
        logRepository.save(new Log(1,"插入第四条日志",new Date()));

        logger.info("-------------------------FIND ALL start--------------------------------");

        List<Log> allLogList = logRepository.findAll();

        for(Log log : allLogList){
            logger.info(log.toString());
        }

        logger.info("-------------------------FIND ALL end--------------------------------");



        logger.info("-------------------------FIND BY TYPE start--------------------------------");

        List<Log> logList = logRepository.findByType(1);

        for(Log log : logList){
            logger.info(log.toString());
        }

        logger.info("-------------------------FIND BY TYPE end--------------------------------");

    }


}

运行测试用例可以看到输出:

2018-09-30 10:11:44.655  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : -------------------------FIND ALL start--------------------------------
2018-09-30 10:11:44.735  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : Log{id=5bb030e0ca57a15090ace337, type=1, remark='插入第一条日志', operateTime=Sun Sep 30 10:11:44 CST 2018}
2018-09-30 10:11:44.736  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : Log{id=5bb030e0ca57a15090ace338, type=1, remark='插入第二条日志', operateTime=Sun Sep 30 10:11:44 CST 2018}
2018-09-30 10:11:44.736  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : Log{id=5bb030e0ca57a15090ace339, type=2, remark='插入第三条日志', operateTime=Sun Sep 30 10:11:44 CST 2018}
2018-09-30 10:11:44.736  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : Log{id=5bb030e0ca57a15090ace33a, type=1, remark='插入第四条日志', operateTime=Sun Sep 30 10:11:44 CST 2018}
2018-09-30 10:11:44.736  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : -------------------------FIND ALL end--------------------------------
2018-09-30 10:11:44.736  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : -------------------------FIND BY TYPE start--------------------------------
2018-09-30 10:11:44.764  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : Log{id=5bb030e0ca57a15090ace337, type=1, remark='插入第一条日志', operateTime=Sun Sep 30 10:11:44 CST 2018}
2018-09-30 10:11:44.765  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : Log{id=5bb030e0ca57a15090ace338, type=1, remark='插入第二条日志', operateTime=Sun Sep 30 10:11:44 CST 2018}
2018-09-30 10:11:44.765  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : Log{id=5bb030e0ca57a15090ace33a, type=1, remark='插入第四条日志', operateTime=Sun Sep 30 10:11:44 CST 2018}
2018-09-30 10:11:44.765  INFO 20624 --- [           main] cc.koit.hulk.service.MongoTests          : -------------------------FIND BY TYPE end--------------------------------

至此,我们实现了往MongoDB中插入数据和查询数据。这里面核心的就是LogRepository接口,该接口继承MongoRepository.简单的CURD直接调用即可,当然我们可以自己定义查询接口,比如示例中的findByType(),方法名中的字段需要与Bean中字段名一致。

,
© 2019 FunGa技术札记 All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero