一起来学SpringBoot(十九)整合MongoDB

MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

依赖依赖依赖

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

然后呢yml

spring:
data:
mongodb:
uri: mongodb://localhost:27017/test #如果没有test就会自动创建

集群呢就这样写

spring:
data:
mongodb:
uri: mongodb://192.168.8.101:27017,192.168.8.101:27018/test #如果没有test就会自动创建

有密码了怎么办

spring:
data:
mongodb:
uri: mongodb://user:secret@127.0.0.1:27017/test #如果没有test就会自动创建

springboot中是使用MongoTemplate 来简化操作的。写个简单的例子 创建一个实体类

package com.fulinlin.pojo;

import java.io.Serializable;

public class User implements Serializable {
private Long id;
private String name;
private String password;

public User(Long id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public String getPassword() {
return password;
}

@Override
public String toString() {
final StringBuffer sb = new StringBuffer("User{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", password='").append(password).append('\'');
sb.append('}');
return sb.toString();
}
}
package com.fulinlin.dao;

import com.maoxs.pojo.User;

import java.util.List;

public interface UserDao {
void add(User user) throws Exception;

void update(User user) throws Exception;

void delete(Long id) throws Exception;

List<User> select(int page, int size)throws Exception;
}
package com.fulinlin.dao.impl;

import com.fulinlin.dao.UserDao;
import com.fulinlin.pojo.User;
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.Service;

import java.util.List;

@Service
public class UserDaoImpl implements UserDao {
@Autowired
private MongoTemplate mongoTemplate;

/**
* 添加
* @param user
* @throws Exception
*/
@Override
public void add(User user) throws Exception {
mongoTemplate.save(user);
}

/**
* 根据id更新
* @param user
* @throws Exception
*/
@Override
public void update(User user) throws Exception {
Query query = new Query(Criteria.where("id").is(user.getId()));
Update update = new Update().set("name", user.getName()).set("password", user.getPassword());
mongoTemplate.updateFirst(query, update, User.class);
}

/**
* 删除
* @param id
* @throws Exception
*/
@Override
public void delete(Long id) throws Exception {
Query query = new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query,User.class);
}

/**
* 分页
* @param page
* @param size
* @return
* @throws Exception
*/
@Override
public List<User> select(int page,int size) throws Exception {
Query query = new Query();
query.skip(page*size).limit(size);
List<User> users = mongoTemplate.find(query, User.class);
return users;
}
}

然后是测试类

package com.fulinlin;

import com.fulinlin.dao.UserDao;
import com.fulinlin.pojo.User;
import org.junit.Assert;
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.List;
import java.util.Random;
import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMongodbApplicationTests {
@Autowired
private UserDao userDao;

@Test
public void add() throws Exception {
Random rand = new Random();
for (int i = 1; i <= 50; i++) {
User user = new User(rand.nextInt(100) + 1L, "jacl" + i, UUID.randomUUID().toString());
userDao.add(user);
}
}

@Test
public void update() throws Exception {
User user = new User(79L, "fulin", "wangle");
this.userDao.update(user);
}

@Test
public void del() throws Exception {
this.userDao.delete(5L);
}

@Test
public void selcet() throws Exception {
List<User> select = this.userDao.select(1, 10);
select.forEach(p -> {
System.out.println(p.toString());
});
}
}

在用robo看看效果


ok没问题 这里呢在放一个学习手册 MongoTemplate 学习手册

本博文是基于springboot2.x 如果有什么不对的请在下方留言。