当前位置: 首页 > 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;比如身…...

Qt图形界面开发集成AI:SmallThinker-3B-Preview实现智能桌面应用

Qt图形界面开发集成AI&#xff1a;SmallThinker-3B-Preview实现智能桌面应用 你是不是也想过&#xff0c;能不能把现在这些厉害的AI能力&#xff0c;直接塞进我们自己写的桌面软件里&#xff1f;比如&#xff0c;在写代码的时候&#xff0c;旁边就有一个能解释复杂代码片段的助…...

SpringBoot+Vue IT交流和分享平台平台完整项目源码+SQL脚本+接口文档【Java Web毕设】

系统架构设计### 摘要 随着信息技术的快速发展&#xff0c;互联网已成为人们获取和分享知识的重要渠道。尤其是在IT领域&#xff0c;技术人员和爱好者需要一个高效、便捷的交流平台来分享经验、讨论技术问题并获取最新行业动态。传统的论坛和社交媒体平台虽然功能丰富&#xff…...

Graphormer保姆级教学:Supervisor配置文件(graphormer.conf)逐行注释

Graphormer保姆级教学&#xff1a;Supervisor配置文件&#xff08;graphormer.conf&#xff09;逐行注释 1. Graphormer简介 Graphormer是一种基于纯Transformer架构的图神经网络&#xff0c;专门为分子图&#xff08;原子-键结构&#xff09;的全局结构建模与属性预测而设计…...

intv_ai_mk11应用场景:金融从业者用其生成监管政策要点摘要、投研报告初稿框架

intv_ai_mk11在金融领域的应用实践&#xff1a;政策摘要与投研报告生成 1. 金融从业者的AI助手需求 金融行业每天需要处理海量的监管政策和市场信息&#xff0c;传统人工处理方式面临三大挑战&#xff1a; 时效性压力&#xff1a;新政策发布后需要快速理解要点信息过载&…...

从Linux内核页表映射到用户态HugeTLB池:金融级C++内存池的7层硬件协同优化法(仅限TOP20对冲基金内部文档解密版)

第一章&#xff1a;金融高频交易C内存池的硬件协同优化全景图在纳秒级响应要求的金融高频交易系统中&#xff0c;C内存池不再仅是软件抽象层的性能补丁&#xff0c;而是CPU缓存子系统、内存控制器与DRAM物理特性的协同执行面。现代x86-64平台&#xff08;如Intel Ice Lake-SP或…...

MGeo地址结构化实战:对接RPA机器人自动填写政务表格中的标准地址字段

MGeo地址结构化实战&#xff1a;对接RPA机器人自动填写政务表格中的标准地址字段 1. 引言&#xff1a;当RPA机器人遇上“不标准”的地址 想象一下这个场景&#xff1a;你是一家政务服务中心的技术负责人&#xff0c;每天有成百上千份表格需要处理。其中&#xff0c;地址信息填…...

RK3588 android12休眠唤醒后以太网不可用

现象&#xff1a;开机后连接网线可正常使用&#xff0c;系统休眠后再次唤醒后网络不通&#xff0c;等待约30秒后看门狗复位&#xff0c;gmac重新初始化后可继续使用。&#xff08;此问题还会导致屏幕唤醒点亮延时1-2秒&#xff09;日志&#xff1a;休眠唤醒后提示报错如下&…...

MeteorSeed

从0构建WAV文件&#xff1a;读懂计算机文件的本质 虽然接触计算机有一段时间了&#xff0c;但是我的视野一直局限于一个较小的范围之内&#xff0c;往往只能看到于算法竞赛相关的内容&#xff0c;计算机各种文件在我看来十分复杂&#xff0c;认为构建他们并能达到目的是一件困难…...

如何快速集成gh_mirrors/ca/card到React/Vue/Angular:打造专业信用卡表单的完整指南

如何快速集成gh_mirrors/ca/card到React/Vue/Angular&#xff1a;打造专业信用卡表单的完整指南 【免费下载链接】card :credit_card: make your credit card form better in one line of code 项目地址: https://gitcode.com/gh_mirrors/ca/card gh_mirrors/ca/card是一…...

神马网站 SEO 优化对网站转化率的影响

神马网站 SEO 优化对网站转化率的影响 在当今互联网时代&#xff0c;网站的流量和转化率直接关系到企业的销售和品牌的知名度。仅仅拥有流量并不意味着一切都顺风顺水&#xff0c;如何把这些流量转化为实际的业务和销售&#xff0c;这就涉及到神马网站的SEO优化对网站转化率的…...