前言
mongodb 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
提示:以下是本篇文章正文内容,下面案例可供参考
一、技术介绍
1.mongodb是什么?
mongodb(来自于英文单词[humongous],中文含义为[庞大])是可以应用于各种规模的企业、各个行业以及各类应用程序的开源数据库。作为一个适用于敏捷开发的数据库,mongodb的数据模式可以随着应用程序的发展而灵活地更新。与此同时,它也为开发人员 提供了传统数据库的功能:二级索引,完整的查询系统以及严格一致性等等。 mongodb能够使企业更加具有敏捷性和可扩展性,各种规模的企业都可以通过使用mongodb来创建新的应用,提高与客户之间的工作效率,加快产品上市时间,以及降低企业成本。
mongodb是专为可扩展性,高性能和高可用性而设计的数据库。它可以从单服务器部署扩展到大型、复杂的多数据中心架构。利用内存计算的优势,mongodb能够提供高性能的数据读写操作。 mongodb的本地复制和自动故障转移功能使您的应用程序具有企业级的可靠性和操作灵活性。
二、使用步骤
1.引入maven库
代码如下(示例):
1 2 3 4 5 6 7 8 9 10 11 12 |
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 2.4 . 1 </version> <relativepath/> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-mongodb</artifactid> </dependency> </dependencies> |
2.具体使用示例
mongodb封装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/** * mongo db助手 * * @author: heyuhua * @date: 2021/1/19 9:52 */ @component public class mongodbhelper {
@autowired private mongotemplate mongotemplate;
/** * 保存 * * @param t * @param <t> * @return */ public <t> t save(t t) { return mongotemplate.save(t); }
/** * 保存 * * @param t * @param collectionname * @param <t> * @return */ public <t> t save(t t, string collectionname) { return mongotemplate.save(t, collectionname); }
/** * 查询 * * @param query * @param tclass * @param <t> * @return */ public <t> list<t> find(query query, class <t> tclass) { return mongotemplate.find(query, tclass); }
/** * 查询所有 * * @param tclass * @param <t> * @return */ public <t> list<t> findall( class <t> tclass) { return mongotemplate.findall(tclass); }
} |
3.配置文件
代码如下(示例):
1 2 3 4 5 6 7 8 9 |
server: port: 8088
spring: #mongodb配置 data: mongodb: uri: mongodb: //admin:admin@127.0.0.1:27017/admin |
4.单元测试
测试代码如下(示例):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package com.hyh.core.test;
import com.hyh.core.test.base.hyhtest; import com.hyh.core.test.po.person; import com.hyh.mongodb.helper.mongodbhelper; import org.junit.test; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.mongodb.core.query.criteria; import org.springframework.data.mongodb.core.query.criteriadefinition; import org.springframework.data.mongodb.core.query.query; import org.springframework.data.mongodb.core.query.textcriteria;
import java.util.list;
/** * mongodb test * * @author: heyuhua * @date: 2021/1/19 10:28 */ public class mongodbtest extends hyhtest {
@autowired private mongodbhelper mongodbhelper;
@test public void testsave() { person person = new person(); person.setname( "heyuhua" ); person.setage( 25 ); mongodbhelper.save(person); person person2 = new person(); person2.setname( "hyh" ); person2.setage( 52 ); mongodbhelper.save(person2); }
@test public void testsavecollection() { person person = new person(); person.setname( "heyuhua" ); person.setage( 25 ); mongodbhelper.save(person, "personcollection" ); person person2 = new person(); person2.setname( "hyh" ); person2.setage( 52 ); mongodbhelper.save(person2, "personcollection" ); }
@test public void testfindall() { list<person> list = mongodbhelper.findall(person. class ); for (person person : list) { system.out.println( "name=" + person.getname() + ",age=" + person.getage()); } }
@test public void testfind() { criteria criteria = new criteria(); criteria.and( "age" ).gte( "25" ); query query = new query(criteria); list<person> list = mongodbhelper.find(query,person. class ); for (person person : list) { system.out.println( "name=" + person.getname() + ",age=" + person.getage()); } }
@test @override public void test() { system.out.println( "---mongodb test---" ); }
} |
总结
是不是感觉很简单?更多用法请点击下方查看源码,关注我带你揭秘更多高级用法
源码地址: 点此查看源码 .
到此这篇关于springboot轻松整合mongodb的文章就介绍到这了,更多相关springboot轻松整合mongodb内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://juejin.cn/post/6922634986856120327
查看更多关于SpringBoot轻松整合MongoDB的全过程记录的详细内容...