Java SpringBoot MongoPlus 使用MyBatisPlus的方式,优雅的操作MongoDB

news/2024/7/8 1:54:29 标签: java, spring boot, mongodb

Java SpringBoot MongoPlus 使用MyBatisPlus的方式,优雅的操作MongoDB

    • 介绍
    • 特性
    • 安装
      • 新建SpringBoot工程
      • 引入依赖
      • 配置文件
    • 使用
      • 新建实体类
      • 创建Service
      • 测试类进行测试
        • 新增方法
        • 查询方法
    • 官方网站
    • 获取本项目案例代码

image-20240704134601333

介绍

MongoPlus

Mongo-Plus(简称 MP)是一个 MongoDB 的操作工具,可和现有mongoDB框架结合使用,为简化开发、提高效率而生。

特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
  • 强大的 CRUD 操作:通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
  • 支持主键自动生成:支持多达 5 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
  • 支持自定义全局通用操作:支持全局通用方法注入
  • 支持无实体类情况下的操作
  • 支持动态数据源
  • 支持逻辑删除、防止全集合更新和删除、自动填充等等功能

安装

新建SpringBoot工程

采用的是IDEA新建的工程

image-20240704135503380

引入依赖

pom文件增加以下内容:

全部增加在project标签下面

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.6.13</version>
  <relativePath/>
</parent>

<dependencies>
  <!-- mongo-plus依赖 -->
  <dependency>
    <groupId>com.gitee.anwena</groupId>
    <artifactId>mongo-plus-boot-starter</artifactId>
    <version>2.0.9.3</version>
  </dependency>
  <!-- boot容器依赖 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!-- 测试依赖 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
  </dependency>
  <!-- lombok依赖 -->
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
  </dependency>
  <!-- hutool工具依赖 -->
  <dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.5</version>
  </dependency>
</dependencies>

增加完后执行下载包命令

mvn clean install

最后重新加载maven依赖

配置文件

mongo-plus:
  data:
    mongodb:
      host: 127.0.0.1   #ip
      port: 27017   #端口
      database: demo    #数据库名
      username: root    #用户名,没有可不填(若账号中出现@,!等等符号,不需要再进行转码!!!)
      password: root    #密码,同上(若密码中出现@,!等等符号,不需要再进行转码!!!)
      authenticationDatabase: admin     #验证数据库
      connectTimeoutMS: 50000   #在超时之前等待连接打开的最长时间(以毫秒为单位)
	log: true

配置自己的数据库以及账号密码

使用

新建实体类

java">@Data
@CollectionName("astarDemo")
public class AstarDemoEntity {
    /**
     * 使用ID注解,标注此字段为MongoDB的_id,或者继承BaseModelID类
     */
    @ID(type = IdTypeEnum.ASSIGN_ID)
    private String id;
    private String name;
    private int age;
    private String email;

}

创建Service

编写Service下的AstarDemoService和实现类AstarDemoServiceImpl,像MyBatisPlus一样

java">public interface AstarDemoService extends IService<AstarDemoEntity> {
}
java">public class AstarDemoServiceImpl extends ServiceImpl<AstarDemoEntity> implements AstarDemoService {
}

测试类进行测试

新增方法
java">@ContextConfiguration(classes = MongoPlusApp.class)
@SpringBootTest
public class AstarDemoTest {
    @Autowired
    private AstarDemoService astarDemoService;
    @Test
    public void insert() {
        AstarDemoEntity entity = new AstarDemoEntity();
        entity.setName("一颗星");
        entity.setAge(111);
        entity.setEmail("xxx@aaa.email");
        astarDemoService.save(entity);
    }
}

运行测试

控制台已经把日志给打印出来了

image-20240704142516312

接下来我们看数据库是否存在

image-20240704142552267

数据库中也存在数据

查询方法
java">@Test
public void query() {
  LambdaQueryChainWrapper<AstarDemoEntity> lambdaQuery = astarDemoService.lambdaQuery();
  // age为111的
  lambdaQuery.eq(AstarDemoEntity::getAge, 111);
  List<AstarDemoEntity> list = astarDemoService.list(lambdaQuery);
  list.forEach(System.out::println);
}

image-20240704143313363

官方网站

官方网址

获取本项目案例代码

获取本项目代码

公粽号:一颗星宇宙

发送mongo获取


http://www.niftyadmin.cn/n/5536871.html

相关文章

Flash存储器解析:从原理到应用,全面了解其与缓存的区别

Flash存储器解析&#xff1a;从原理到应用&#xff0c;全面了解其与缓存的区别 Flash存储器是一种非易失性存储器技术&#xff0c;广泛应用于各种电子设备中&#xff0c;如USB闪存盘、固态硬盘&#xff08;SSD&#xff09;、智能手机、数码相机和嵌入式系统。它能够在断电情况下…

Spring Security 认证流程

Spring Scurity是spring生态下用于认证和授权的框架&#xff0c;具有高度的灵活性和可扩展行&#xff0c;本节主要对Spring Security的认证过程中进行概括性的介绍&#xff0c;主要介绍在该过程中&#xff0c;会涉及到哪些组件以及每个组件所承担的职责&#xff0c;希望大家可以…

白骑士的Python教学高级篇 3.4 Web开发

系列目录 上一篇&#xff1a;白骑士的Python教学高级篇 3.3 数据库编程 在现代软件开发中&#xff0c;Web开发占据了重要的一席之地。通过Web开发&#xff0c;我们可以创建从简单的个人博客到复杂的电子商务网站等各种应用。在Python的生态系统中&#xff0c;Flask和Django是两…

PX2平台Pytorch源码编译

写在前面&#xff1a;以下内容完成于2019年底&#xff0c;只是把笔记放到了CSDN上。 需要注释掉NCLL及分布式相关的配置 libcudart.patch diff --git a/torch/cuda/__init__.py b/torch/cuda/__init__.py index 4591702..07e1268 100644 --- a/torch/cuda/__init__.pyb/torc…

C++: Map数组的遍历

在C中&#xff0c;map是一个关联容器&#xff0c;它存储的元素是键值对&#xff08;key-value pairs&#xff09;&#xff0c;其中每个键都是唯一的&#xff0c;并且自动根据键来排序。遍历map的方式有几种&#xff0c;但最常用的两种是使用迭代器&#xff08;iterator&#xf…

密码学原理精解【5】

这里写目录标题 移位密码概述代码 希尔密码( Z 256 Z_{256} Z256​)待加密长度被3整除待加密长度不一定被3整除加解密文件 移位密码 概述 以 z 26 运算为例 , k 为密钥 加密&#xff1a; e k ( x ) ( x k ) m o d 26 解密&#xff1a; d k ( x ) ( x − k ) m o d 26 以z_{…

C#Winform窗体中嵌入exe文件

1&#xff0c;效果以嵌入Modbus Slave为例&#xff1a; 2&#xff0c;代码&#xff1a; public partial class Form1 : Form{//设置嵌入exe的常量private const int nIndex -16;private const int dwNewLong 0x10000000;Process m_AppProcess;public Form1(){InitializeCompo…

c进阶篇(四):内存函数

内存函数以字节为单位更改 1.memcpy memcpy 是 C/C 中的一个标准库函数&#xff0c;用于内存拷贝操作。它的原型通常定义在 <cstring> 头文件中&#xff0c;其作用是将一块内存中的数据复制到另一块内存中。 函数原型&#xff1a;void *memcpy(void *dest, const void…