MongoDB是一个非常强大的非关系型数据库,他以集合的形式存储我们的数据到数据库,他灵活的数据结构以及便于快速迭代业务的使用场景,包括他强大的查询相应速度,导致应用场景越来越普遍,下面我们将讲解如何使用我们的MongoDB集成我们的SpringBoot。
安装:
这里讲解如何在win10上面安装我们的MongoDB。下载地址。下载完成以后双击打开下载应用包。
后面一直下一步到完成就好。。。。。。。。。。,下面使用工具链接到我们的MongoDB,不需要账号密码。
SpringBoot依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.28</version> <scope>compile</scope> </dependency>
XML
spring.application.name=kafkaTest
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=mydatabase
#不需要密码验证
spring.data.mongodb.uri=mongodb://localhost:27017/mydatabase
#需要密码验证
#spring.data.mongodb.uri=mongodb://rootuser:rootpwd@localhost:27017/mydatabase
java
package org.aogo.kafkatest.entity;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "users")
@Data
@Accessors(chain = true)
public class User {
@Id
private String id;
private String username;
private String email;
}
java
package org.aogo.kafkatest.api;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aogo.kafkatest.entity.User;
import org.aogo.kafkatest.server.UserService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/api/account")
@CrossOrigin(origins = "*")
public class AccountController {
@Resource
private UserService userService;
@GetMapping("test")
public User getUser() {
User user = new User();
user.setId("38");
user.setUsername("388");
user.setEmail("38888888@qq.com");
userService.saveUser(user);
if(user != null){
throw new RuntimeException("Username cannot be null");
}
//userService.getUserByUser("28");
return user;
}
}
java
package org.aogo.kafkatest.server;
import org.aogo.kafkatest.entity.User;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, String> {
// 可以在这里定义一些自定义的查询方法
}
java
package org.aogo.kafkatest.server;
import org.aogo.kafkatest.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void saveUser(User user) {
//保存或者更新
userRepository.save(user);
}
public User getUserById(String id) {
return userRepository.findById(id).orElse(null);
}
public Page<User> getUserByUser(String id) {
//分页
Pageable pageable = PageRequest.of(0, 10, Sort.by("id").descending()); // 第 0 页,每页 10 条数据
User user = new User();
user.setUsername("名稱");
Example<User> example = Example.of(user);
// 查询分页结果
Page<User> userPage = userRepository.findAll(example,pageable);
return userPage;
}
}
常用语法:
sql
//切换到数据库
use mydatabase
//创建用户并赋予权限
db.createUser({
user: "rootuser", // 用户名
pwd: "rootpwd", // 密码
roles: [
{ role: "readWrite", db: "mydatabase" } // 给用户赋予mydatabase数据库的权限 权限类别给admin类别
]
})
//查看用户
show users
//查看版本号
db.version()
sql
//插入数据
db.users.insert({
_id: '50',
username: "7899",
email: "7899"
})
//查询文档
db.users.find()
//根据条件查询文档
db.users.find({_id:'50'})
//保存/更新
db.users.save({
_id: '50',
username: "789911",
email: "7899"
})
//更新语句
db.users.update(
{ _id: "50" }, // 查询条件
{ // 更新的内容
$set: {
username: "789911",
email: "7899111"
}
}
)