SpringBoot第八篇:springboot整合mongodb

news/2024/7/5 5:58:13

这篇文章主要介绍springboot如何整合mongodb。

准备工作

  • 安装 MongoDB
  • jdk 1.8
  • maven 3.0
  • idea

环境依赖

在pom文件引入spring-boot-starter-data-mongodb依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency>

数据源配置

如果mongodb端口是默认端口,并且没有设置密码,可不配置,sprinboot会开启默认的。

spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db

mongodb设置了密码,这样配置:

spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname

定义一个简单的实体

mongodb

package com.forezp.entity;import org.springframework.data.annotation.Id;public class Customer {@Idpublic String id;public String firstName;public String lastName;public Customer() {}public Customer(String firstName, String lastName) {this.firstName = firstName;this.lastName = lastName;}@Overridepublic String toString() {return String.format("Customer[id=%s, firstName='%s', lastName='%s']",id, firstName, lastName);}}

数据操作dao层

public interface CustomerRepository extends MongoRepository<Customer, String> {public Customer findByFirstName(String firstName);public List<Customer> findByLastName(String lastName);}

写一个接口,继承MongoRepository,这个接口有了几本的CURD的功能。如果你想自定义一些查询,比如根据firstName来查询,获取根据lastName来查询,只需要定义一个方法即可。注意firstName严格按照存入的mongodb的字段对应。在典型的java的应用程序,写这样一个接口的方法,需要自己实现,但是在springboot中,你只需要按照格式写一个接口名和对应的参数就可以了,因为springboot已经帮你实现了。

测试

@SpringBootApplication
public class SpringbootMongodbApplication  implements CommandLineRunner {@Autowiredprivate CustomerRepository repository;public static void main(String[] args) {SpringApplication.run(SpringbootMongodbApplication.class, args);}@Overridepublic void run(String... args) throws Exception {repository.deleteAll();// save a couple of customersrepository.save(new Customer("Alice", "Smith"));repository.save(new Customer("Bob", "Smith"));// fetch all customersSystem.out.println("Customers found with findAll():");System.out.println("-------------------------------");for (Customer customer : repository.findAll()) {System.out.println(customer);}System.out.println();// fetch an individual customerSystem.out.println("Customer found with findByFirstName('Alice'):");System.out.println("--------------------------------");System.out.println(repository.findByFirstName("Alice"));System.out.println("Customers found with findByLastName('Smith'):");System.out.println("--------------------------------");for (Customer customer : repository.findByLastName("Smith")) {System.out.println(customer);}}

在springboot的应用程序,加入测试代码。启动程序,控制台打印了:

>Customers found with findAll():

 Customer[id=58f880f589ffb696b8a6077e, firstName='Alice', lastName='Smith']Customer[id=58f880f589ffb696b8a6077f, firstName='Bob', lastName='Smith']Customer found with findByFirstName('Alice'):--------------------------------Customer[id=58f880f589ffb696b8a6077e, firstName='Alice', lastName='Smith']Customers found with findByLastName('Smith'):--------------------------------Customer[id=58f880f589ffb696b8a6077e, firstName='Alice', lastName='Smith']Customer[id=58f880f589ffb696b8a6077f, firstName='Bob', lastName='Smith']

测试通过。

源码下载:https://github.com/forezp/SpringBootLearning


http://lihuaxi.xjx100.cn/news/281400.html

相关文章

百度提出PADDLESEG:一个高效的图像分割开发工具

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶” 重磅干货&#xff0c;第一时间送达小白导读 论文是学术研究的精华和未来发展的明灯。小白决心每天为大家带来经典或者最新论文的解读和分享&#xff0c;旨在帮助各位读者快速了解论文内容。个人能力有限&a…

为了不让GPU等CPU,谷歌提出“数据回波”榨干GPU空闲时间,训练速度提升3倍多...

晓查 发自 凹非寺 量子位 报道 | 公众号 QbitAI因为通用计算芯片不能满足神经网络运算需求&#xff0c;越来越多的人转而使用GPU和TPU这类专用硬件加速器&#xff0c;加快神经网络训练的速度。但是&#xff0c;用了更快的GPU和TPU就一定能加速训练吗&#xff1f;训练流水线的所…

Linux 中监控 MySQL性能的调优工具

MySQL是最常见的一种轻量型数据库&#xff0c;也是目前在市面上应用最广泛的一种数据库&#xff0c;所以懂得几个MySQL的调优工具非常必要&#xff0c;我个人比较推荐mytop和innotop 监控mysql性能的工具有很多&#xff0c;好的工具是诊断myql性能瓶颈和排除服务器的利器。日常…

【转】Flex4:利用HttpService与ASP.NET传输JSON数据(登录为例)

开发环境&#xff1a;Flash Builder4,Vs2005 1、首先打开FlashBuilde4,创建一个名为HttpService_Net_Json的flex项目 &#xff08;图1&#xff09; 然后下一步,应用程序类型选择web,应用程序服务器类型选择ASP.NET(如图2) &#xff08;图2&#xff09; 下一步&#xff0c;出现配…

zoom:1是什么意思

当一个容器内元素都浮动后&#xff0c;它将高度将不会随着内部元素高度的增加而增加&#xff0c;所以造成内容元素的显示超出了容器。 overflow:auto;是让高度自适应&#xff0c; zoom:1;是为了兼容IE6&#xff0c;也可以用height:1%;的方式来解决1、浮动 浮动是CSS中用到的最多…

JavaScript初学者编程题(13)

JavaScript初学者编程题(13) 题目&#xff1a;输入一行字符&#xff0c;分别统计出其中英文字母、空格、数字和其它字符的个数。 HTMl部分 <input type"text" id"str"><button onclick"getTheNum()">get</button>JavaScript…

SpringBoot第九篇: springboot整合Redis

这篇文章主要介绍springboot整合redis&#xff0c;至于没有接触过redis的同学可以看下这篇文章&#xff1a;5分钟带你入门Redis。引入依赖&#xff1a;在pom文件中添加redis依赖&#xff1a;<dependency><groupId>org.springframework.boot</groupId><art…

Google 确认 Chrome 存在严重漏洞,向 20 亿用户发出警告:你们需立即更新浏览器...

作者&#xff1a;okay来源&#xff1a;扩展迷EXTFANS近日&#xff0c;Google面向二十亿Chrome浏览器用户推出至关重要的补丁程序&#xff0c;并再次强调大家需要立即更新其浏览器。如果你在Mac&#xff0c;Windows 10或Linux计算机上使用的是Google Chrome浏览器&#xff0c;则…