可扩展性:MongoDB可以轻松地扩展到多个节点,以处理大量数据和高并发请求。
MongoDB的缺点:
不支持事务:MongoDB在早期版本中不支持事务,虽然在最新版本中已经支持了事务,但是相比传统的关系型数据库还是有所不足。<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>然后配置连接信息:
spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=mydb其中,host和port分别是MongoDB的主机名和端口号,database是要连接的数据库名称。如果MongoDB需要进行身份验证,还需要添加以下配置:
spring.data.mongodb.username=myuser spring.data.mongodb.password=mypassword其中,username和password分别是MongoDB的用户名和密码。
@Document(collection = "student") public class Student { @Id private String id; private String name; private int age; // 省略getter和setter方法 }3.创建学生Repository接口。
@Repository public interface StudentRepository extends MongoRepository<Student, String> { }4.创建学生Service接口和实现类。
public interface StudentService { List<Student> findAll(); Student findById(String id); void save(Student student); void deleteById(String id); } // 堆代码 duidaima.com @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentRepository studentRepository; @Override public List<Student> findAll() { return studentRepository.findAll(); } @Override public Student findById(String id) { return studentRepository.findById(id).orElse(null); } @Override public void save(Student student) { studentRepository.save(student); } @Override public void deleteById(String id) { studentRepository.deleteById(id); } }5.创建学生Controller类。
@RestController @RequestMapping("/students") public class StudentController { @Autowired private StudentService studentService; @GetMapping("") public List<Student> findAll() { return studentService.findAll(); } @GetMapping("/{id}") public Student findById(@PathVariable String id) { return studentService.findById(id); } @PostMapping("") public void save(@RequestBody Student student) { studentService.save(student); } @PutMapping("/{id}") public void update(@PathVariable String id, @RequestBody Student student) { student.setId(id); studentService.save(student); } @DeleteMapping("/{id}") public void deleteById(@PathVariable String id) { studentService.deleteById(id); } }完整代码示例:
@Document(collection = "student") public class Student { @Id private String id; private String name; private int age; // 省略getter和setter方法 }StudentRepository.java
@Repository public interface StudentRepository extends MongoRepository<Student, String> { }StudentService.java
public interface StudentService { List<Student> findAll(); Student findById(String id); void save(Student student); void deleteById(String id); } @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentRepository studentRepository; @Override public List<Student> findAll() { return studentRepository.findAll(); } @Override public Student findById(String id) { return studentRepository.findById(id).orElse(null); } @Override public void save(Student student) { studentRepository.save(student); } @Override public void deleteById(String id) { studentRepository.deleteById(id); } }StudentController.java
@RestController @RequestMapping("/students") public class StudentController { @Autowired private StudentService studentService; @GetMapping("") public List<Student> findAll() { return studentService.findAll(); } @GetMapping("/{id}") public Student findById(@PathVariable String id) { return studentService.findById(id); } @PostMapping("") public void save(@RequestBody Student student) { studentService.save(student); } @PutMapping("/{id}") public void update(@PathVariable String id, @RequestBody Student student) { student.setId(id); studentService.save(student); } @DeleteMapping("/{id}") public void deleteById(@PathVariable String id) { studentService.deleteById(id); } }