当前位置: 首页 > news >正文

SpringBoot MongoDB操作封装

1.引入Jar包

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

2.MongoDbHelper操作

/*** MongoDB Operation class* @author Mr.Li* @date 2022-12-05*/
public class MongoDbHelper {@Autowiredprivate MongoTemplate mongoTemplate;/*** Save Individual Objects** @param t* @param <T>* @return*/public <T> T save(T t) {return mongoTemplate.insert(t);}/*** Store the object to the specified collectionName* @param objectToSave* @param collectionName* @param <T>* @return*/public <T> T save(T objectToSave, String collectionName){return mongoTemplate.insert(objectToSave,collectionName);}/*** Batch save track data** @param list* @param collectionName* @return*/public <T> Collection<T> batchSave(Collection<T> list, String collectionName) {return mongoTemplate.insert(list,collectionName);}/*** Query Data** @param query* @param tClass* @param <T>* @return*/public <T> List<T> find(Query query, Class<T> tClass) {return mongoTemplate.find(query, tClass);}/*** Collection specified by query data** @param query* @param tClass* @param <T>* @return*/public <T> List<T> find(Query query, Class<T> tClass, String collectionName) {return mongoTemplate.find(query, tClass,collectionName);}/*** Pagination query* @param query query criteria* @param pageNum Current Page* @param pageSize Number of entries per page* @param sortField Sorted Field* @param sortType 1:asc;0:desc* @param tClass to class* @param collectionName collection name* @param <T>* @return*/public <T> MongoPage findByPage(Query query,int pageNum,int pageSize,String sortField,int sortType, Class<T> tClass, String collectionName) {int count = (int) mongoTemplate.count(query, tClass, collectionName);if(sortType==1){query.with(Sort.by(Sort.Order.asc(sortField)));}else {query.with(Sort.by(Sort.Order.desc(sortField)));}//Set starting numberquery.skip((pageNum - 1) * pageSize);//Set the number of queriesquery.limit(pageSize);//Query the current page data setList<T> taskList = mongoTemplate.find(query, tClass,collectionName);int size=count % pageSize == 0 ? count / pageSize : count / pageSize + 1;MongoPage page=new MongoPage();page.setTotal(count);page.setSize(size);page.setData(taskList);return page;}/*** 查询前几条数据* @param query* @param limitNum 前几条* @param sortField 排序字段* @param sortType 0:倒序;1:正序* @param tClass* @param collectionName* @param <T>* @return*/public <T> List<T> findTop(Query query,Integer limitNum,String sortField,int sortType, Class<T> tClass, String collectionName){if(sortType==1){query.with(Sort.by(Sort.Order.asc(sortField)));}else {query.with(Sort.by(Sort.Order.desc(sortField)));}query.limit(limitNum);return mongoTemplate.find(query, tClass,collectionName);}/*** 查询一条数据* @param query* @param sortField* @param sortType* @param tClass* @param collectionName* @param <T>* @return*/public <T> List<T> findOne(Query query,String sortField,int sortType, Class<T> tClass, String collectionName){if(sortType==1){query.with(Sort.by(Sort.Order.asc(sortField)));}else {query.with(Sort.by(Sort.Order.desc(sortField)));}//Set the number of queriesquery.limit(1);//Query the current page data setList<T> taskList = mongoTemplate.find(query, tClass,collectionName);return taskList;}/*** Query All** @param tClass* @param <T>* @return*/public <T> List<T> findAll(Class<T> tClass) {return mongoTemplate.findAll(tClass);}/*** Query all specified collections** @param tClass* @param collectionName* @param <T>* @return*/public <T> List<T> findAll(Class<T> tClass,String collectionName) {return mongoTemplate.findAll(tClass,collectionName);}/*** create collection* @param collName* @param indexList* @return*/public boolean createCollection(String collName, List<Map<String,Integer>> indexList){try {if (mongoTemplate.collectionExists(collName)) {return true;}//Index collection to be createdList<IndexModel> indexModels = new ArrayList<>();for (Map<String, Integer> indexMap : indexList) {BasicDBObject index = new BasicDBObject();for (String key : indexMap.keySet()) {index.put(key, indexMap.get(key));}indexModels.add(new IndexModel(index));}mongoTemplate.createCollection(collName).createIndexes(indexModels);return true;}catch (Exception e){return false;}}/*** Update the first result set returned by the query* @param query* @param update* @param collectionName* @return*/public boolean updateFirst(Query query, Update update, String collectionName){try {mongoTemplate.updateFirst(query, update, collectionName);return true;}catch (Exception e){return false;}}/*** Update all returned result sets* @param query* @param update* @param collectionName* @return*/public boolean updateMulti(Query query, Update update, String collectionName){try {mongoTemplate.updateMulti(query, update, collectionName);return true;}catch (Exception e){return false;}}/*** If the update object does not exist, add it* @param query* @param update* @param tClass* @param <T>* @param collectionName* @return*/public <T> boolean upsert(Query query, Update update, Class<T> tClass,String collectionName){try {mongoTemplate.upsert(query, update, tClass,collectionName);return true;}catch (Exception e){return false;}}/*** 存在则更新不存在则创建* @param query* @param update* @param collectionName* @return*/public boolean upsert(Query query, Update update, String collectionName){try {mongoTemplate.upsert(query, update,collectionName);return true;}catch (Exception e){return false;}}/*** 汇总查询* @param aggregation* @param tClass* @param collectionName* @param <T>* @return*/public <T> List<T> groupQuery(Aggregation aggregation,Class<T> tClass,String collectionName){AggregationResults<T> maps = mongoTemplate.aggregate(aggregation, collectionName, tClass);return maps.getMappedResults();}/*** 查总条数* @param query* @param collectionName* @return*/public long queryCount(Query query, String collectionName){return mongoTemplate.count(query, collectionName);}
}

3.分页实体类MongoPage

/*** MongoDB paged query returns result set* @author Mr.Li* @date 2023-01-11*/
@Data
public class MongoPage {/*** Total number of data*/private Integer total;/*** Page count*/private Integer size;/*** Data result set per page*/private Object data;
}

相关文章:

SpringBoot MongoDB操作封装

1.引入Jar包 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency> 2.MongoDbHelper操作 /*** MongoDB Operation class* author Mr.Li* date 2022-12-05*…...

PyTorch 模型性能分析和优化 — 第 1 部分

一、说明 这篇文章的重点将是GPU上的PyTorch培训。更具体地说&#xff0c;我们将专注于 PyTorch 的内置性能分析器 PyTorch Profiler&#xff0c;以及查看其结果的方法之一&#xff0c;即 PyTorch Profiler TensorBoard 插件。 二、深度框架 训练深度学习模型&#xff0c;尤其是…...

Unity3D 简易音频管理器

依赖于Addressable 依赖于单例模板&#xff1a;传送门 using System.Collections.Generic; using System.Security.Cryptography; using System; using UnityEngine; using UnityEngine.AddressableAssets;namespace EasyAVG {public class AudioManager : MonoSingleton<…...

【李沐深度学习笔记】线性回归

课程地址和说明 线性回归p1 本系列文章是我学习李沐老师深度学习系列课程的学习笔记&#xff0c;可能会对李沐老师上课没讲到的进行补充。 线性回归 如何在美国买房&#xff08;经典买房预测问题&#xff09; 一个简化的模型 线性模型 其中&#xff0c; x → [ x 1 , x 2 ,…...

微信收款码费率0.38太坑了

作为一个有多年运营经验的商家&#xff0c;我本人在申请收款功能时曾经走过了不少弯路。我找遍了市面上的知名的支付公司&#xff0c;但了解到的收款手续费率通常都在0.6左右&#xff0c;最低也只能降到0.38。这个过程吃过不少苦头。毕竟&#xff0c;收款功能是我们商家的命脉&…...

【学习笔记】CF1103D Professional layer

首先分析不出啥性质&#xff0c;所以肯定是暴力优化&#x1f605; 常见的暴力优化手段有均摊&#xff0c;剪枝&#xff0c;数据范围分治&#xff08;points&#xff09;&#xff0c;答案值域分析之类的。 比较经典的题目是 CF1870E Another MEX Problem&#xff0c;可以用剪枝…...

vue之Pinia

定义 Store | Pinia 开发文档 1.什么是Pinaia Pinia 是 Vue 的专属状态管理库&#xff0c;它允许你跨组件或页面共享状态。 2.理解Pinaia核心概念 定义Store 在深入研究核心概念之前&#xff0c;我们得知道 Store 是用 defineStore() 定义的&#xff0c;它的第一个参数要求是一…...

antd-vue 级联选择器默认值不生效解决方案

一、业务场景&#xff1a; 最近在使用Vue框架和antd-vue组件库的时候&#xff0c;发现在做编辑回显时** 级联选择器** 组件的默认值不生效。为了大家后面遇到和我一样的问题&#xff0c;给大家分享一下 二、bug信息&#xff1a; 三、问题原因&#xff1a; 确定不了唯一的值&a…...

分享53个Python源码源代码总有一个是你想要的

分享53个Python源码源代码总有一个是你想要的 链接&#xff1a;https://pan.baidu.com/s/1ew3w2_DXlSBrK7Mybx3Ttg?pwd8888 提取码&#xff1a;8888 项目名称 100-Python ControlXiaomiDevices DRF-ADMIN 后台管理系统 FishC-Python3小甲鱼 Flask框架的api项目脚手架 …...

【每日一题】658. 找到 K 个最接近的元素

658. 找到 K 个最接近的元素 - 力扣&#xff08;LeetCode&#xff09; 给定一个 排序好 的数组 arr &#xff0c;两个整数 k 和 x &#xff0c;从数组中找到最靠近 x&#xff08;两数之差最小&#xff09;的 k 个数。返回的结果必须要是按升序排好的。 整数 a 比整数 b 更接近 …...

并发任务队列(字节青训测试题)

需求描述 封装一个并发任务队列类&#xff0c;用于对一些异步任务按指定的并发数量进行并发执行。 /*** 延迟函数* param {number} time - 延迟时间* return {Promise} delayFn - 延迟函数(异步封装)*/ function timeout(time) {return new Promise((resolve) > {setTimeo…...

Ubuntu 安装Nacos

1、官网下载最新版nacos https://github.com/alibaba/nacos/releases 本人环境JDK8&#xff0c;Maven3.6.3&#xff0c;启动Nacos2.2.1启动失败&#xff0c;故切换到2.1.0启动成功 2、放到服务器目录下&#xff0c;我的在/home/xxx/apps下 3、解压 $ tar -zxvf nacos-serve…...

CSS 小球随着椭圆移动

html代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title><…...

【李沐深度学习笔记】线性代数

课程地址和说明 线性代数p1 本系列文章是我学习李沐老师深度学习系列课程的学习笔记&#xff0c;可能会对李沐老师上课没讲到的进行补充。 线性代数 标量 标量&#xff08;scalar&#xff09;&#xff0c;亦称“无向量”。有些物理量&#xff0c;只具有数值大小&#xff0c…...

vuejs - - - - - 递归组件的实现

递归组件的实现 1. 需求描述&#xff1a;2. 效果图&#xff1a;3. 代码3.1 封装组件代码3.2 父组件使用 1. 需求描述&#xff1a; 点击添加行&#xff0c;增加一级目录结构当类型为object or array时&#xff0c;点击右侧➕&#xff0c;增加子集点击右侧&#x1f6ae;&#x…...

精准对接促合作:飞讯受邀参加市工信局举办的企业供需对接会

2023年9月21日&#xff0c;由惠州市工业和信息化局主办的惠州市工业软件企业与制造业企业供需对接会成功举办&#xff0c;对接会旨在促进本地工业软件企业与制造业企业的紧密合作&#xff0c;推动数字化转型的深入发展。此次会议在市工业和信息化局16楼会议室举行&#xff0c;会…...

数学建模之遗传算法

文章目录 前言遗传算法算法思想生物的表示初始种群的生成下一代种群的产生适应度函数轮盘赌交配变异混合产生新种群 停止迭代的条件遗传算法在01背包中的应用01背包问题介绍01背包的其它解法01背包的遗传算法解法生物的表示初始种群的生成下一代种群的产生适应度函数轮盘赌交配…...

ISO9001认证常见的不符合项

今天&#xff0c;整理了一些关于ISO9001质量管理体系审核最常见的不合格项&#xff0c;以供大家参考。 一、质量管理体系 1、质量手册&#xff08;标准条款4.2.2&#xff09; &#xff08;1&#xff09;各部门执行的文件与手册的规定不一致。 &#xff08;2&#xff09;质量…...

crypto:看我回旋踢

题目 下载压缩包后解压可得到提示文本 经过观察&#xff0c;synt{}这个提示与flag{}形式很像 由题目名中的回旋可以推测为凯撒密码&#xff0c;由凯撒密码的定义可知&#xff0c;需要先推出移位数&#xff0c;s->f数13次&#xff0c;因此移位数为13&#xff0c;解码可得...

Springcloud实战之自研分布式id生成器

一&#xff0c;背景 日常开发中&#xff0c;我们需要对系统中的各种数据使用 ID 唯一表示&#xff0c;比如用户 ID 对应且仅对应一个人&#xff0c;商品 ID 对应且仅对应一件商品&#xff0c;订单 ID 对应且仅对应 一个订单。我们现实生活中也有各种 ID &#xff0c;比如身…...

IDEA运行Tomcat出现乱码问题解决汇总

最近正值期末周&#xff0c;有很多同学在写期末Java web作业时&#xff0c;运行tomcat出现乱码问题&#xff0c;经过多次解决与研究&#xff0c;我做了如下整理&#xff1a; 原因&#xff1a; IDEA本身编码与tomcat的编码与Windows编码不同导致&#xff0c;Windows 系统控制台…...

React Native 导航系统实战(React Navigation)

导航系统实战&#xff08;React Navigation&#xff09; React Navigation 是 React Native 应用中最常用的导航库之一&#xff0c;它提供了多种导航模式&#xff0c;如堆栈导航&#xff08;Stack Navigator&#xff09;、标签导航&#xff08;Tab Navigator&#xff09;和抽屉…...

循环冗余码校验CRC码 算法步骤+详细实例计算

通信过程&#xff1a;&#xff08;白话解释&#xff09; 我们将原始待发送的消息称为 M M M&#xff0c;依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)&#xff08;意思就是 G &#xff08; x ) G&#xff08;x) G&#xff08;x) 是已知的&#xff09;&#xff0…...

【Go】3、Go语言进阶与依赖管理

前言 本系列文章参考自稀土掘金上的 【字节内部课】公开课&#xff0c;做自我学习总结整理。 Go语言并发编程 Go语言原生支持并发编程&#xff0c;它的核心机制是 Goroutine 协程、Channel 通道&#xff0c;并基于CSP&#xff08;Communicating Sequential Processes&#xff0…...

从零开始打造 OpenSTLinux 6.6 Yocto 系统(基于STM32CubeMX)(九)

设备树移植 和uboot设备树修改的内容同步到kernel将设备树stm32mp157d-stm32mp157daa1-mx.dts复制到内核源码目录下 源码修改及编译 修改arch/arm/boot/dts/st/Makefile&#xff0c;新增设备树编译 stm32mp157f-ev1-m4-examples.dtb \stm32mp157d-stm32mp157daa1-mx.dtb修改…...

什么?连接服务器也能可视化显示界面?:基于X11 Forwarding + CentOS + MobaXterm实战指南

文章目录 什么是X11?环境准备实战步骤1️⃣ 服务器端配置(CentOS)2️⃣ 客户端配置(MobaXterm)3️⃣ 验证X11 Forwarding4️⃣ 运行自定义GUI程序(Python示例)5️⃣ 成功效果![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/55aefaea8a9f477e86d065227851fe3d.pn…...

【HarmonyOS 5 开发速记】如何获取用户信息(头像/昵称/手机号)

1.获取 authorizationCode&#xff1a; 2.利用 authorizationCode 获取 accessToken&#xff1a;文档中心 3.获取手机&#xff1a;文档中心 4.获取昵称头像&#xff1a;文档中心 首先创建 request 若要获取手机号&#xff0c;scope必填 phone&#xff0c;permissions 必填 …...

SQL慢可能是触发了ring buffer

简介 最近在进行 postgresql 性能排查的时候,发现 PG 在某一个时间并行执行的 SQL 变得特别慢。最后通过监控监观察到并行发起得时间 buffers_alloc 就急速上升,且低水位伴随在整个慢 SQL,一直是 buferIO 的等待事件,此时也没有其他会话的争抢。SQL 虽然不是高效 SQL ,但…...

现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?

现有的 Redis 分布式锁库&#xff08;如 Redisson&#xff09;相比于开发者自己基于 Redis 命令&#xff08;如 SETNX, EXPIRE, DEL&#xff09;手动实现分布式锁&#xff0c;提供了巨大的便利性和健壮性。主要体现在以下几个方面&#xff1a; 原子性保证 (Atomicity)&#xff…...

消防一体化安全管控平台:构建消防“一张图”和APP统一管理

在城市的某个角落&#xff0c;一场突如其来的火灾打破了平静。熊熊烈火迅速蔓延&#xff0c;滚滚浓烟弥漫开来&#xff0c;周围群众的生命财产安全受到严重威胁。就在这千钧一发之际&#xff0c;消防救援队伍迅速行动&#xff0c;而豪越科技消防一体化安全管控平台构建的消防“…...