MongoDB是主流的非关系型数据库,它也是非关系型数据库所有的产品当中最像关系型数据库的。它是一个面向文档的非关系型数据库,同时它的使用也跟我们传统的关系型数据库结构是差不多的。它里面也会有数据表的概念。但是它的数据表相对来说比较灵活。
mysql数据库建表必须规定好它的多少个字段,字段的数据类型是什么。
在MongoDB里面我们不用去规定数据类型了,因为MongoDB里面的数据类型全部都是类似于json格式进行存储的,所以存储非常灵活。
SpringBoot提供了一个Spring Data MongoDB的模块来操作MongoDB.
安装MongoDB数据库:
MongoDB的安装非常简单,在下载完成后,接直接双击下载好的MongoDB安装包,进入MongoDB安装界面,点击Next下一步、同意条款、选择安装路径 和 日志路径、勾选是否安装MongoDB Compass(MongoDB数据库图形管理工具,类似MySQL的Navicat ),其它的步骤一直点下一步直到完成安装就OK啦。
mongo.exe和mongod.exe有区别嘛?
感觉mongo.exe相当于mongod.exe的下一步
数据库和图形化界面都有了:
链接:https://pan.baidu.com/s/1hZ9AJ726Ae8rWjdJwC6tjQ
提取码:h7xn
参考文章:https://blog.csdn.net/muguli2008/article/details/80591256
注:在MongoDB中创建一个新的数据库时,需要向数据库中创建一个集合(collections【就像关系数据库中的表】),并且插入一条数据,这个数据库才能创建成功!!
use my_test //使用my_test数据库,如果没有这个数据库会帮我们创建
show tables 和 show collections 是一样的都是查看有多少表
db.my_student.find() //查看my_student表里面的内容
db.user.insert({
"name": "xiaoming"}); // user 就是集合(表)名
或
db.user.save({
name:"沐枫", job:"Web前端"}) // 效果和上面一样,都是添加数据
db.dropDatabase();
db //查看当前的数据库
use命令具有打开、 切换、 创建数据库的功能
db.user.drop() //删除user表
查找出来的数据是BSON的格式类似于JSON的一种格式
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.southwind</groupId>
<artifactId>springbootmongodb22</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2.创建实体类
如果表名对应我们就可以不需要对表进行映射(意思就是Document里面不需要写东西了)
mongodb的主键类型都是string类型的。
@Field注解指的是普通字段
package com.southwind.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Data
@Document(collection = "my_student")
public class Student {
@Id
private String id;
@Field(value = "student_age")
private Integer age;
@Field(value = "student_name")
private String name;
}
3.创建StudentRepository接口(这个接口跟JPA是一样的只需要去定义不需要我们去实现),Spring Data Mongodb也给我们提供了一套模板,包括也支持自定义方法(自定义方法和jpa也是一样的)
package com.southwind.repository;
import com.southwind.entity.Student;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface StudentRepository extends MongoRepository<Student,String> {
}
4.创建StudentHandler
package com.southwind.controller;
import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public List<Student> findAll(){
return studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") String id){
return studentRepository.findById(id).get();
}
@PostMapping("/save")
public Student save(@RequestBody Student student){
return studentRepository.save(student);
}
@PutMapping("/update")
public Student update(@RequestBody Student student){
return studentRepository.save(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") String id){
studentRepository.deleteById(id);
}
}
5.创建配置文件application.yml
spring:
data:
mongodb:
database: my_test
host: 127.0.0.1
port: 27017
6.创建启动类Application
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}