admin 1 an în urmă
părinte
comite
6962d2e830

+ 63 - 0
mongodbSpringBoot/pom.xml

@@ -0,0 +1,63 @@
+<?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>org.example</groupId>
+    <artifactId>mongodbSpringBoot</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.RELEASE</version>
+    </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-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>2.9.2</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-mongodb</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 11 - 0
mongodbSpringBoot/src/main/java/org/example/Main.java

@@ -0,0 +1,11 @@
+package org.example;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Main {
+    public static void main(String[] args) {
+        SpringApplication.run(Main.class, args);
+    }
+}

+ 82 - 0
mongodbSpringBoot/src/main/java/org/example/Student.java

@@ -0,0 +1,82 @@
+package org.example;
+
+import org.springframework.data.annotation.Id;
+
+import java.io.Serializable;
+import java.util.Date;
+
+public class Student implements Serializable {
+
+    @Id
+    private String studentId;
+
+    private String studentName;
+
+    private Integer studentAge;
+
+    private Double studentScore;
+
+    private Date studentBirthday;
+
+    public Student(String studentId, String studentName, Integer studentAge, Double studentScore, Date studentBirthday) {
+        this.studentId = studentId;
+        this.studentName = studentName;
+        this.studentAge = studentAge;
+        this.studentScore = studentScore;
+        this.studentBirthday = studentBirthday;
+    }
+
+    public Student() {
+    }
+
+    public String getStudentId() {
+        return studentId;
+    }
+
+    public void setStudentId(String studentId) {
+        this.studentId = studentId;
+    }
+
+    public String getStudentName() {
+        return studentName;
+    }
+
+    public void setStudentName(String studentName) {
+        this.studentName = studentName;
+    }
+
+    public Integer getStudentAge() {
+        return studentAge;
+    }
+
+    public void setStudentAge(Integer studentAge) {
+        this.studentAge = studentAge;
+    }
+
+    public Double getStudentScore() {
+        return studentScore;
+    }
+
+    public void setStudentScore(Double studentScore) {
+        this.studentScore = studentScore;
+    }
+
+    public Date getStudentBirthday() {
+        return studentBirthday;
+    }
+
+    public void setStudentBirthday(Date studentBirthday) {
+        this.studentBirthday = studentBirthday;
+    }
+
+    @Override
+    public String toString() {
+        return "Student{" +
+                "studentId='" + studentId + '\'' +
+                ", studentName='" + studentName + '\'' +
+                ", studentAge=" + studentAge +
+                ", studentScore=" + studentScore +
+                ", studentBirthday=" + studentBirthday +
+                '}';
+    }
+}

+ 6 - 0
mongodbSpringBoot/src/main/java/org/example/StudentRepository.java

@@ -0,0 +1,6 @@
+package org.example;
+
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+public interface StudentRepository extends MongoRepository<Student, String> {
+}

+ 7 - 0
mongodbSpringBoot/src/main/resources/application.yaml

@@ -0,0 +1,7 @@
+spring:
+  data:
+    mongodb:
+      host: 127.0.0.1
+      database: test
+      port: 27017
+      # 也可以使用uri   mongodb://127.0.0.1:27017/test

+ 145 - 0
mongodbSpringBoot/src/test/java/StudentTest.java

@@ -0,0 +1,145 @@
+import org.example.Main;
+import org.example.Student;
+import org.example.StudentRepository;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.data.domain.*;
+
+
+import java.util.*;
+
+/**
+ * @Author: acton_zhang
+ * @Date: 2023/5/9 12:44 上午
+ * @Version 1.0
+ */
+@SpringBootTest(classes = Main.class)
+public class StudentTest {
+
+    @Autowired
+    private StudentRepository studentRepository;
+
+    /**
+     * 插入单条数据
+     */
+    @Test
+    public void insertOne() {
+        Student student = new Student("009", "tom", 18, 88.2d, new Date());
+        studentRepository.insert(student);
+    }
+
+    /**
+     * 插入多条数据
+     */
+    @Test
+    public void insertMany() {
+        Student student1 = new Student("002", "jerry", 19, 38.2d, new Date());
+        Student student2 = new Student("003", "mike", 20, 78.2d, new Date());
+        List<Student> list = new ArrayList<>();
+        list.add(student1);
+        list.add(student2);
+        studentRepository.insert(list);
+    }
+
+    /**
+     * 修改数据
+     */
+    @Test
+    public void update() {
+        Optional<Student> op = studentRepository.findById("002");
+        Student student = op.get();
+        student.setStudentAge(222);
+        studentRepository.save(student);
+    }
+
+    /**
+     * 查询数据
+     */
+    @Test
+    public void query() {
+        //根据Id查询单个对象
+        Optional<Student> stuOp = studentRepository.findById("002");
+        System.out.println(stuOp.get());
+
+        //根据字段查询单个对象
+        Student student = new Student();
+        student.setStudentAge(19);
+        Optional<Student> stuOp1 = studentRepository.findOne(Example.of(student));
+        System.out.println(stuOp1.get());
+
+        //根据id列表查询多个对象
+        Iterable<Student> itStu = studentRepository.findAllById(Arrays.asList(new String[]{"003", "002"}));
+        Iterator<Student> itor = itStu.iterator();
+        while (itor.hasNext())
+            System.out.println(itor.next());
+
+        //查询所有
+        List<Student> all = studentRepository.findAll();
+
+        //查询所有并排序
+        List<Student> all1 = studentRepository.findAll(Sort.by(Sort.Order.desc("studentId"), Sort.Order.asc("studentName")));
+        for (Student stu : all1)
+            System.out.println(stu);
+
+        //根据条件查询所有并排序
+        Student student1 = new Student();
+        student1.setStudentName("tom");
+        List<Student> all2 = studentRepository.findAll(Example.of(student1), Sort.by(Sort.Order.desc("studentId"), Sort.Order.asc("studentName")));
+        for (Student stu : all2)
+            System.out.println(stu);
+
+        //根据条件查询所有并排序,且分页
+        Pageable pageable = PageRequest.of(0, 2);
+        Page<Student> all3 = studentRepository.findAll(pageable);
+        List<Student> content = all3.getContent();
+        for (Student stu : content)
+            System.out.println(stu);
+    }
+
+    /**
+     * 其他操作
+     */
+    @Test
+    public void other() {
+        //count
+        long count = studentRepository.count();
+        System.out.println(count);
+        Student student = new Student();
+        student.setStudentAge(18);
+        long count1 = studentRepository.count(Example.of(student));
+        System.out.println(count1);
+
+        //exists
+        boolean exists = studentRepository.exists(Example.of(student));
+        System.out.println(exists);
+        boolean b = studentRepository.existsById("002");
+        System.out.println(b);
+    }
+
+
+    /**
+     * 删除操作
+     */
+    @Test
+    public void remove() {
+        //根据id删除单个对象
+        studentRepository.deleteById("002");
+
+//        //根据字段删除
+//        Student student = new Student();
+//        student.setStudentAge(20);
+//        studentRepository.delete(student);
+//
+//        //删除所有
+//        studentRepository.deleteAll();
+//
+//        //根据字段删除多个
+//        Student student1 = new Student();
+//        student1.setStudentName("jerry");
+//        List<Student> list = new ArrayList<>();
+//        list.add(student);
+//        list.add(student1);
+//        studentRepository.deleteAll(list);
+    }
+}