|
@@ -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);
|
|
|
+ }
|
|
|
+}
|