当前位置:   article > 正文

springBoot整合mongodb-jpa_springboot mongodb jpa

springboot mongodb jpa

第1步:基于maven新建springBoot工程

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.8.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <modelVersion>4.0.0</modelVersion>
  8. <groupId>com.springcloud</groupId>
  9. <artifactId>mogodb_springboot_jpa</artifactId>
  10. <version>1.1.0</version>
  11. <build>
  12. <plugins>
  13. <plugin>
  14. <groupId>org.apache.maven.plugins</groupId>
  15. <artifactId>maven-compiler-plugin</artifactId>
  16. <configuration>
  17. <source>8</source>
  18. <target>8</target>
  19. </configuration>
  20. </plugin>
  21. </plugins>
  22. </build>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <!-- Apache Lang3 -->
  29. <dependency>
  30. <groupId>org.apache.commons</groupId>
  31. <artifactId>commons-lang3</artifactId>
  32. <version>3.9</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-data-mongodb</artifactId>
  37. <version>2.2.2.RELEASE</version>
  38. </dependency>
  39. <!-- JSON 解析器和生成器 -->
  40. <dependency>
  41. <groupId>com.alibaba</groupId>
  42. <artifactId>fastjson</artifactId>
  43. <version>1.2.74</version>
  44. </dependency>
  45. <!--Jackson required包-->
  46. <dependency>
  47. <groupId>com.fasterxml.jackson.core</groupId>
  48. <artifactId>jackson-core</artifactId>
  49. <version>2.11.2</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>com.fasterxml.jackson.core</groupId>
  53. <artifactId>jackson-databind</artifactId>
  54. <version>2.11.2</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>com.fasterxml.jackson.core</groupId>
  58. <artifactId>jackson-annotations</artifactId>
  59. <version>2.11.2</version>
  60. </dependency>
  61. <dependency>
  62. <groupId>javax.servlet</groupId>
  63. <artifactId>servlet-api</artifactId>
  64. <version>2.5</version>
  65. <scope>provided</scope>
  66. </dependency>
  67. </dependencies>

第2步:配置application.properties文件

  1. server.port=8090
  2. spring.application.name=mongodb-8090
  3. spring.data.mongodb.host=192.168.42.22
  4. spring.data.mongodb.port=27017
  5. spring.data.mongodb.database=db_resume

第3步:编写实体类,并在实体类上面加上注解 @Docment("集合名")

  1. package com.springcloud.domian.bean;
  2. import org.apache.commons.lang3.builder.ToStringBuilder;
  3. import java.math.BigDecimal;
  4. import java.util.Date;
  5. public class Resume {
  6. private String id;
  7. private String name;
  8. private String city;
  9. private Date birthday;
  10. private BigDecimal expectSalary;
  11. public Resume() {
  12. }
  13. @Override
  14. public String toString() {
  15. return ToStringBuilder.reflectionToString(this);
  16. }
  17. public String getId() {
  18. return id;
  19. }
  20. public void setId(String id) {
  21. this.id = id;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public String getCity() {
  30. return city;
  31. }
  32. public void setCity(String city) {
  33. this.city = city;
  34. }
  35. public Date getBirthday() {
  36. return birthday;
  37. }
  38. public void setBirthday(Date birthday) {
  39. this.birthday = birthday;
  40. }
  41. public BigDecimal getExpectSalary() {
  42. return expectSalary;
  43. }
  44. public void setExpectSalary(BigDecimal expectSalary) {
  45. this.expectSalary = expectSalary;
  46. }
  47. }

第4步:编写Repository接口, 继承MongoRepository

  1. package com.springcloud.repository;
  2. import com.springcloud.domian.bean.Resume;
  3. import org.springframework.data.mongodb.repository.MongoRepository;
  4. public interface ResumeRepository extends MongoRepository<Resume, String> {
  5. }

第5步: 从Spring容器中获取Repository对象进行数据操作

  1. package com.springcloud.service.impl;
  2. import com.springcloud.domian.bean.Resume;
  3. import com.springcloud.repository.ResumeRepository;
  4. import com.springcloud.service.IResumeService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.domain.Example;
  7. import org.springframework.data.domain.ExampleMatcher;
  8. import org.springframework.data.domain.Page;
  9. import org.springframework.data.domain.PageRequest;
  10. import org.springframework.data.domain.Pageable;
  11. import org.springframework.data.domain.Sort;
  12. import org.springframework.stereotype.Service;
  13. import java.math.BigDecimal;
  14. import java.text.ParseException;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Objects;
  17. @Service
  18. public class ResumeServiceImpl implements IResumeService {
  19. @Autowired
  20. private ResumeRepository resumeRepository;
  21. @Override
  22. public Resume findByName(String name) {
  23. Resume resume = new Resume();
  24. resume.setName(name);
  25. Example<Resume> resumeExample = Example.of(resume);
  26. return resumeRepository.findOne(resumeExample);
  27. }
  28. @Override
  29. public Resume insertResume(Integer no) throws ParseException {
  30. Resume resume = new Resume();
  31. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  32. resume.setName("李" + no);
  33. resume.setCity("hk" + no);
  34. resume.setBirthday(sdf.parse("1996-03-14"));
  35. resume.setExpectSalary(new BigDecimal("15000"));
  36. resumeRepository.insert(resume);
  37. return resume;
  38. }
  39. @Override
  40. public Boolean updateResume(String id, String name) {
  41. Resume resume = resumeRepository.findOne(id);
  42. resume.setName(name);
  43. Resume save = resumeRepository.save(resume);
  44. return (Objects.nonNull(save) ? Boolean.TRUE : Boolean.FALSE);
  45. }
  46. @Override
  47. public Boolean deleteResume(String id) {
  48. resumeRepository.delete(id);
  49. return Boolean.TRUE;
  50. }
  51. /**
  52. * 分页查询
  53. *
  54. * @param name
  55. * @param currentPage
  56. * @param pageSize
  57. * @return
  58. */
  59. @Override
  60. public Page<Resume> listByName(String name, int currentPage, int pageSize) {
  61. Sort sort = new Sort(Sort.Direction.DESC, "city");
  62. Pageable pageable = new PageRequest(currentPage - 1, pageSize, sort);
  63. //创建匹配器,即如何使用查询条件
  64. ExampleMatcher matcher = ExampleMatcher.matching()
  65. .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
  66. .withIgnoreCase(true)
  67. .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
  68. .withIgnorePaths("pageNum", "pageSize");
  69. //创建实例
  70. Resume resume = new Resume();
  71. resume.setName(name);
  72. Example<Resume> example = Example.of(resume, matcher);
  73. Page<Resume> resumes = resumeRepository.findAll(example, pageable);
  74. return resumes;
  75. }
  76. }

测试结果:

        ① 查询document: http://localhost:8090/resume/query/document?name=李1

  ② 新增document: http://localhost:8090/resume/document/add?no=1

 ③ 更新document:

  http://localhost:8090/resume/document/update?id=625533977654045b6ce01685&name=离家程

 

 

④ 删除document:

http://localhost:8090/resume/document/delete?id=625535e77654045b6ce01688

  ⑤ 分页查询document:

http://localhost:8090/resume/list/document?name=张三¤tPage=1&pageSize=10

 

总结

        在进行分页查询的时候,需要特别注意的是,传递的分页参数的page是从0开始的,即0代表是第1页,要是不注意的话,就会造成查不到数据,或者是查询的数据不准确,需要做代码上的兼容,即:

Pageable pageable = new PageRequest(currentPage - 1, pageSize, sort);

源代码地址:springcloud-learn: 自建springcloud项目(练习) - Gitee.com

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

闽ICP备14008679号