本文共 9093 字,大约阅读时间需要 30 分钟。
SpringData还提供了对多种NoSQL数据库的支持,包括MongoDB;neo4j和redis.他不仅支持自动化的repository,还支持基于模板的数据访问和映射注解.下面是一个Spring通过Jpa操作MongoDB数据库的小Demo:
StuController:
import com.demo.jpamongodb.dao.StudentRepository;import com.demo.jpamongodb.entity.Student;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Optional;@RestControllerpublic class StuController { @Autowired private StudentRepository studentRepository; @RequestMapping("/getStuByName/{name}") public OptionalgetSchool1() { Optional stu = studentRepository.findById(1L); return stu; }}
SchoolReponsitory
package com.demo.jpamongodb.dao;import com.demo.jpamongodb.entity.School;import org.springframework.data.mongodb.repository.MongoRepository;public interface SchoolReponsitory extends MongoRepository{ School findSchoolByName(String name);}
StudentRepository
import com.demo.jpamongodb.entity.Student;import org.springframework.data.mongodb.repository.MongoRepository;public interface StudentRepository extends MongoRepository{ Student findByName(String name);}
School
package com.demo.jpamongodb.entity;import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.ToString;import org.springframework.data.annotation.Id;import org.springframework.data.mongodb.core.mapping.Document;import java.net.Proxy;@Documentpublic class School { @Id private Long id; private String name; private String address; public School() { } public School(Long id, String name, String address) { this.id = id; this.name = name; this.address = address; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "School{" + "id=" + id + ", name='" + name + '\'' + ", address='" + address + '\'' + '}'; }}
Student
package com.demo.jpamongodb.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.ToString;import org.springframework.data.annotation.Id;import org.springframework.data.mongodb.core.mapping.Document;//@AllArgsConstructor//@Data//@ToString@Documentpublic class Student { @Id private Long id; private String name; private Integer age; private School shool; public Student() { } public Student(Long id, String name, Integer age, School shool) { this.id = id; this.name = name; this.age = age; this.shool = shool; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public School getShool() { return shool; } public void setShool(School shool) { this.shool = shool; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", shool=" + shool + '}'; }}
application.properties
spring.data.mongodb.host=127.0.0.1spring.data.mongodb.port=27017spring.data.mongodb.database=testspring.data.mongodb.username=testspring.data.mongodb.password=123456server.port=8787
JpaMongodbApplicationTests
package com.demo.jpamongodb;import com.demo.jpamongodb.dao.SchoolReponsitory;import com.demo.jpamongodb.dao.StudentRepository;import com.demo.jpamongodb.entity.School;import com.demo.jpamongodb.entity.Student;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;@RunWith(SpringRunner.class)@SpringBootTestpublic class JpaMongodbApplicationTests { @Autowired private StudentRepository studentRepository; @Autowired private SchoolReponsitory schoolReponsitory; @Test public void contextLoads() { } /** * 第一次单元测试 * - student实体没有加home属性 * * @throws Exception */ @Test public void insertStudentWithoutHome() throws Exception { School school1 = schoolReponsitory.findSchoolByName("南京路中学"); School school2 = schoolReponsitory.findSchoolByName("北京路中学"); System.out.println(school1); System.out.println(school2);// schoolReponsitory.save(new School(1L,"南京路中学","南京路")); studentRepository.save(new Student(1L, "小明", 30,school1));// studentRepository.save(new Student(2L, "小红", 40,school1));// studentRepository.save(new Student(3L, "小王", 50,school2)); } /** * 第二次单元测试 * - student实体加home属性 * * @throws Exception */ @Test public void insertStudentWitHome() throws Exception { School school1 = schoolReponsitory.findSchoolByName("南京路中学"); School school2 = schoolReponsitory.findSchoolByName("北京路中学");// studentRepository.save(new Student(4L, "tom", 30,school1,"1小区"));// studentRepository.save(new Student(5L, "peter", 40,school1,"2小区"));// studentRepository.save(new Student(6L, "joy", 50,school2,"3小区")); } /** * 对查询结果打印 */ @Test public void findAll() { Liststudents = studentRepository.findAll(); students.forEach(student -> { System.out.println(student); }); }// @Test// public void insertSchool(){// School school1 = School.builder().address("南京路").name("南京路中学").id(1L).build();// School school2 = School.builder().address("北京路").name("北京路中学").id(2L).build();//new School(1L,"南京路中学","南京路");//// schoolReponsitory.save(new School(1L,"南京路中学","南京路"));// schoolReponsitory.save(school2);//// School school = schoolReponsitory.findSchoolByName("南京路中学");// System.out.println(school);// }}
pom.xml:
4.0.0 com.beacon jpa-mongodb 0.0.1-SNAPSHOT jar Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-configuration-processor true org.springframework.boot spring-boot-starter-aop org.springframework.boot spring-boot-starter-thymeleaf org.mongodb mongo-java-driver 3.8.0 org.springframework.boot spring-boot-starter-data-mongodb org.projectlombok lombok 1.16.22 org.springframework.boot spring-boot-maven-plugin
转载地址:http://ykonl.baihongyu.com/