当前位置:   article > 正文

springboot随笔之四:springboot整合spring-data-jpa(mogodb)_spring data jpa和spring-boot-starter-data-mongodb

spring data jpa和spring-boot-starter-data-mongodb

一、在mongodb中准备相应数据

1、创建数据库并创建对应可以访问的对应库的用户信息

2、创建对应collection,并插入相应的文档数据

二、结合前面的实例,项目中配置包依赖及数据源

1、jpa包依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-jpa</artifactId>
  4. </dependency>
  5. <!-- mongodb依赖 -->
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-data-mongodb</artifactId>
  9. </dependency>

2、配置数据源

  1. # mongodb 2.4以上版本支持uri配置(此处未设置用户名密码,mongodb默认不设置用户名密码)
  2. spring.data.mongodb.uri=mongodb://admin:123456@localhost/test
  3. #spring.data.mongodb.database=test
  4. #spring.data.mongodb.host=127.0.0.1
  5. #spring.data.mongodb.port=27017
  6. #spring.data.mongodb.repositories.enabled=true
  7. #spring.data.mongodb.username=admin
  8. #spring.data.mongodb.password=123456

三、编写数据库表对应的实体类

  1. package com.expample.springboot04.domain;
  2. //与mongo collection对应的Entity类
  3. /**
  4. * ORM对象关系映射,即java 实体类和mongodb collection的映射关系:
  5. * 类成员变量 -> collection字段,类名 -> collection名
  6. **/
  7. import org.springframework.data.mongodb.core.mapping.Document;
  8. import org.springframework.data.mongodb.core.mapping.Field;
  9. import javax.persistence.Entity;
  10. import javax.persistence.Id;
  11. //注解实体类,springboot自动装载为bean
  12. @Entity
  13. @Document("student")
  14. public class Student {
  15. // 指定主键字段,注意import是javax持久化的Id而非springboot annotation的Id
  16. // @Field("id")指定该主键名称为id,如果不添加此项注解的话,
  17. // 程序会认为private int id对应的是mongodb studdent集合中的objectid字段(该字段是mongodb中的默认主键)
  18. @Id
  19. @Field("id")
  20. private int id;
  21. private String name;
  22. private int age;
  23. // 默认构造方法
  24. public Student(){
  25. }
  26. // 有参构造方法
  27. public Student(int id, String name, int age){
  28. this.id = id;
  29. this.name = name;
  30. this.age = age;
  31. }
  32. public int getId() {
  33. return id;
  34. }
  35. public void setId(int id) {
  36. this.id = id;
  37. }
  38. public String getName() {
  39. return name;
  40. }
  41. public void setName(String name) {
  42. this.name = name;
  43. }
  44. public int getAge() {
  45. return age;
  46. }
  47. public void setAge(int age) {
  48. this.age = age;
  49. }
  50. }

四、编写持久层

  1. package com.expample.springboot04.dao;
  2. import org.springframework.data.mongodb.repository.MongoRepository;
  3. import org.springframework.stereotype.Repository;
  4. import com.expample.springboot04.domain.Student;
  5. @Repository("studentDao")
  6. public interface IStudentDao extends MongoRepository<Student, Integer> {
  7. }

其中MongoRepository<Student, Integer>
Student表示数据库中表对应的实体类
Integer表示数据中表对应主键类型

Spring容器会根据@Repository("studentDao")创建对应的对象studentDao

五、编写业务层

  1. package com.expample.springboot04.service;
  2. import java.util.List;
  3. import org.springframework.stereotype.Service;
  4. import com.expample.springboot04.domain.Student;
  5. @Service
  6. public interface IStudentService {
  7. List<Student> findAllStudents();
  8. }
  1. package com.expample.springboot04.service.impl;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import com.expample.springboot04.dao.IStudentDao;
  6. import com.expample.springboot04.domain.Student;
  7. import com.expample.springboot04.service.IStudentService;
  8. @Service("studentService")
  9. public class StudentServiceImpl implements IStudentService {
  10. @Autowired
  11. private IStudentDao studentDao;
  12. @Override
  13. public List<Student> findAllStudents() {
  14. // TODO Auto-generated method stub
  15. return studentDao.findAll();
  16. }
  17. }

六、完善控制器

  1. package com.expample.springboot04.control;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.expample.springboot04.domain.Student;
  7. import com.expample.springboot04.service.IStudentService;
  8. @RestController
  9. @RequestMapping("/exampleControl")
  10. public class ExampleControl {
  11. @Autowired
  12. private IStudentService studentService;
  13. @RequestMapping("/testExample")
  14. public String testExample() {
  15. return "testExample";
  16. }
  17. @RequestMapping("/showStudents")
  18. public List<Student> showStudents() {
  19. return studentService.findAllStudents();
  20. }
  21. }

七、运行springboot启动类,浏览器测试http://localhost:8000/test/exampleControl/showStudents

 

点击跳转到源码下载地址

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55950
推荐阅读
相关标签
  

闽ICP备14008679号