springboot如何进行整合mongodb
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,springboot如何进行整合mongodb,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。准备工作安装 MongoDBjdk 1.
千家信息网最后更新 2025年12月03日springboot如何进行整合mongodb
springboot如何进行整合mongodb,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
准备工作
安装 MongoDB
jdk 1.8
maven 3.0
idea
环境依赖
在pom文件引入spring-boot-starter-data-mongodb依赖:
org.springframework.boot spring-boot-starter-data-mongodb
数据源配置
如果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 { @Id public String id; public String firstName; public String lastName; public Customer() {} public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return String.format( "Customer[id=%s, firstName='%s', lastName='%s']", id, firstName, lastName); }}数据操作dao层
public interface CustomerRepository extends MongoRepository{ public Customer findByFirstName(String firstName); public List findByLastName(String lastName);}
写一个接口,继承MongoRepository,这个接口有了几本的CURD的功能。如果你想自定义一些查询,比如根据firstName来查询,获取根据lastName来查询,只需要定义一个方法即可。注意firstName严格按照存入的mongodb的字段对应。在典型的java的应用程序,写这样一个接口的方法,需要自己实现,但是在springboot中,你只需要按照格式写一个接口名和对应的参数就可以了,因为springboot已经帮你实现了。
测试
@SpringBootApplicationpublic class SpringbootMongodbApplication implements CommandLineRunner { @Autowired private CustomerRepository repository; public static void main(String[] args) { SpringApplication.run(SpringbootMongodbApplication.class, args); } @Override public void run(String... args) throws Exception { repository.deleteAll(); // save a couple of customers repository.save(new Customer("Alice", "Smith")); repository.save(new Customer("Bob", "Smith")); // fetch all customers System.out.println("Customers found with findAll():"); System.out.println("-------------------------------"); for (Customer customer : repository.findAll()) { System.out.println(customer); } System.out.println(); // fetch an individual customer System.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']测试通过。
关于springboot如何进行整合mongodb问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
接口
方法
程序
问题
查询
测试
配置
整合
密码
应用程序
数据
更多
端口
帮助
应用
解答
易行
可不
简单易行
严格按照
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
学校资源服务器购置申请
湖北卫视青少年网络安全教育
广东省 网络安全
国旗下致辞网络安全
管理服务器ip的前台页面
浙江混合现实设计软件开发
数据库文本处理函数left
软件开发要不要色盲
甘肃省网络安全 政策
中国网络安全和信息委员会
巫山软件开发公司哪家好
秦汉新城网络安全
网络安全大班信息报道
怎么用ip地址进入文件服务器
抢票软件开发黑客
软件开发小组成员
不想显示数据库中的某些字段
服务器管理证书
如何在服务器上建造网站
国外服务器ss
博时数据库怎么查询
滨州市网络安全知识答题
数据库对象变更
博雅数据库2021年高考云南
深圳web前端软件开发价位
推动公司网络安全
2018软件开发语言
怎么用数据库解决ios
idea显示数据库
优化政府管理服务器