Spring Data之MongoDB配置

一、重写基类扩展功能

package com.mk.mongodb.repository;import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.repository.NoRepositoryBean;
import java.util.Collection;
import java.util.List;@NoRepositoryBean
public interface IBaseRepository<T, ID> extends MongoRepository<T, ID> {//根据id查询对象T find(ID id);T findOne(Criteria criteria);//查询对象idList<ID> findIdsByQuery(Query query);//根据字段名查询对象列表List<T> listAll();//根据id列表查询对象列表List<T> listByIds(Collection<ID> ids);//根据字段名查询对象列表List<T> listByField(String fieldName, Collection values);//根据字段名查询对象列表List<T> listByField(String fieldName, Object value);//根据字段名查询对象列表List<T> listByField(String fieldName1, Object value1, String fieldName2, Collection value2);//根据字段名查询对象列表List<T> listByField(String fieldName1, Object value1, String fieldName2, Object value2);//表名String getTableName();//id值ID getIdValue(T entity);//id属性名String getIdAttribute();//id列名String getIdColumn();//删除void deleteAll(Collection<ID> ids);//mongo操作对象MongoOperations getMongoOperations();//实体定义MongoEntityInformation<T, ID> getEntityInformation();//实体类Class<T> getEntityClass();//id类Class<ID> getIdClass();//mongo转换对象default MongoConverter getMongoConverter(){return getMongoOperations().getConverter();}//实例类base对象BaseRepository<T, ID> _baseThis();//查询数据List<T> find(Query query);//计算数量long count(Query query);//查询去重字段<F> List<F> findDistinct(Query query, String field, Class<F> clazz);//聚合<F> AggregationResults<F> aggregate(Aggregation aggregation, Class<F> clazz);
}
package com.mk.mongodb.repository;import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;public class BaseRepository<T, ID> extends SimpleMongoRepository<T, ID> implements IBaseRepository<T, ID> {protected final MongoOperations mongoOperations;protected final MongoEntityInformation<T, ID> entityInformation;public BaseRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {super(metadata, mongoOperations);this.mongoOperations = mongoOperations;this.entityInformation = metadata;}@Overridepublic T find(ID id) {return findById(id).orElse(null);}@Overridepublic T findOne(Criteria criteria) {Query query = new Query(criteria);return this.mongoOperations.findOne(query, getEntityClass(), getTableName());}@Overridepublic List<ID> findIdsByQuery(Query query) {return this.mongoOperations.findDistinct(query, this.getIdColumn(), getTableName(), getIdClass());}@Overridepublic List<T> listAll() {return this.mongoOperations.findAll(getEntityClass(), getTableName());}@Overridepublic List<T> listByIds(Collection<ID> ids) {Iterable<T> iterable =  findAllById(ids);if(iterable instanceof List){return (List<T>) iterable;}List<T> list = new LinkedList<>();iterable.forEach(list::add);return list;}@Overridepublic List<T> listByField(String fieldName, Collection values) {Criteria criteria = new Criteria();criteria.and(fieldName).in(values);Query query = new Query(criteria);List<T> list = this.find(query);return list;}@Overridepublic List<T> listByField(String fieldName, Object value) {Criteria criteria = new Criteria();criteria.and(fieldName).is(value);Query query = new Query(criteria);List<T> list = this.find(query);return list;}@Overridepublic List<T> listByField(String fieldName1, Object value1, String fieldName2, Collection value2) {Criteria criteria = new Criteria();criteria.and(fieldName1).is(value1).and(fieldName2).in(value2);Query query = new Query(criteria);List<T> list = this.find(query);return list;}@Overridepublic List<T> listByField(String fieldName1, Object value1, String fieldName2, Object value2) {Criteria criteria = new Criteria();criteria.and(fieldName1).is(value1).and(fieldName2).is(value2);Query query = new Query(criteria);List<T> list = this.find(query);return list;}@Overridepublic String getTableName(){return entityInformation.getCollectionName();}@Overridepublic ID getIdValue(T entity){return entityInformation.getId(entity);}@Overridepublic String getIdAttribute(){return entityInformation.getIdAttribute();}@Overridepublic String getIdColumn(){return "_id";}@Overridepublic void deleteAll(Collection<ID> ids) {Criteria criteria = Criteria.where(this.getIdColumn()).in(ids);Query query = new Query(criteria);mongoOperations.remove(query, getTableName());}@Overridepublic MongoOperations getMongoOperations() {return mongoOperations;}@Overridepublic MongoEntityInformation<T, ID> getEntityInformation() {return entityInformation;}@Overridepublic Class<T> getEntityClass() {return entityInformation.getJavaType();}@Overridepublic Class<ID> getIdClass() {return entityInformation.getIdType();}@Overridepublic BaseRepository<T, ID> _baseThis() {return this;}@Overridepublic List<T> find(Query query) {return this.mongoOperations.find(query, getEntityClass(), getTableName());}@Overridepublic long count(Query query) {return this.mongoOperations.count(query, getTableName());}@Overridepublic <F> List<F> findDistinct(Query query, String field, Class<F> clazz) {return this.mongoOperations.findDistinct(query, field, getTableName(), clazz);}@Overridepublic <F> AggregationResults<F> aggregate(Aggregation aggregation, Class<F> clazz) {return  this.mongoOperations.aggregate(aggregation, getTableName(), clazz);}}

 

二、Repository继承新基类

package com.mk.mongodb.entity;import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;public class Config {@Idprivate ObjectId id;private String value;public ObjectId getId() {return id;}public void setId(ObjectId id) {this.id = id;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}}
package com.mk.mongodb.repository;package com.mk.mongodb.entity.Config;
import org.bson.types.ObjectId;@Repository
public interface ConfigRepository extends IBaseRepository<Config, ObjectId> {}

 

三、配置MongoDB默认基类

package com.mk;import com.mk.mongodb.repository.BaseRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;@SpringBootApplication
@EnableMongoRepositories(repositoryBaseClass = BaseRepository.class)
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

数据库配置 

spring:data:mongodb:uri: mongodb://username:password@192.168.1.2:40001,192.168.1.3:40001/configDb

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/322870.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

欢乐纪中某B组赛【2019.1.28】

前言 心态爆炸 成绩 RankRankRank是有算别人的 RankRankRankPersonPersonPersonScoreScoreScoreAAABBBCCCDDD3332017myself2017myself2017myself1901901901001001005050500004040401313132017zyc2017zyc2017zyc1701701707070706060600004040401313132017hzb2017hzb2017hzb1701…

这应该是目前最快速有效的ASP.NET Core学习方式(视频)

ASP.NET Core都2.0了&#xff0c;它的普及还是不太好。作为一个.NET的老司机&#xff0c;我觉得.NET Core给我带来了很多的乐趣。Linux&#xff0c; Docker&#xff0c; CloudNative&#xff0c;MicroService&#xff0c;DevOps这些都能跟它很完美的结合&#xff0c;再加ASP.NE…

大叔公开课~微服务与持久集成

闲话多说 免费报名&#xff1a;http://www.genshuixue.com/teacher/classCourseDetail/171117794648么可以通过阅读原文报名 .Net Core来了&#xff0c;带给我们的是什么&#xff1f;跨平台&#xff0c;无疑是最大的亮点&#xff01; Docker横空出世&#xff0c;让开发者和运维…

语音服务——腾讯云

腾讯语音服务文档 一、开通语音消息服务流程 &#xff08;1&#xff09;注册并认证 如果您还没有腾讯云账号&#xff0c;您需要 注册腾讯云 账号&#xff0c;并完成 企业实名认证。如果您已有企业认证的腾讯云账号&#xff0c;请直接进行下一步操作。 申请开通语音消息服务 …

P4231-三步必杀【差分】

正题 题目大意 修改[L..R][L..R][L..R]加上[S..E][S..E][S..E]的等差数列&#xff0c;求最终答案。 题目大意 很明显的差分。 aia_iai​为原数组&#xff0c;bib_ibi​为一阶差分数组&#xff0c;cic_ici​为二阶差分数组 axaxs(x−l)∗k(x∈[l..r])a_xa_xs(x-l)*k(x\in[l..r…

请用JavaScript实现一个函数,接受一-个IP白名单列表whitelist以及列表ipList

请用JavaScript实现一个函数&#xff0c;接受一-个IP白名单列表whitelist以及 列表ipList,判断输入的ipList中是否有任何ip包含在whitelist中&#xff0c;如果存在返回true,如果都不存在返回false。要求:1.列出所有测试用例2.定义并实现该函数&#xff0c;有完整出入参*示例:如…

Visual Studio交叉编译器提供对ARM的支持

只要ARM平台能够运行Windows&#xff0c;Visual Studio就有能力拓展ARM平台。在Visual Studio 2017 15.5预览版2中&#xff0c;该IDE通过使用GCC编译器&#xff0c;增加了对基于ARM的计算机和物联网&#xff08;IoT&#xff09;设备的支持力度&#xff0c;从而扩展了对ARM平台的…

Flume均匀发送数据到kafka的partition配置UUID Interceptor生成key的坑

一、需求 Flume向kafka发送数据时&#xff0c;同一个flume发送到kafka的数据总是固定在某一个partition中。而业务需求是发送的数据在所有的partition平均分布 二、实现 Flume的官方文档&#xff1a; Kafka Sink uses the topic and key properties from the FlumeEvent hea…

jzoj4244-yi【贪心】

正题 题目大意 一些飞船&#xff0c;选最少的&#xff0c;使得可以载所有乘客来回两次 解题思路 因为代价都是一样的&#xff0c;选载客最多可以往返两次的就好了。 codecodecode #include<cstdio> #include<algorithm> using namespace std; const int N100010…

我心中的ASP.NET Core 新核心对象WebHost(二)

这是ASP.NET Core新核心对象系列的第二篇&#xff0c;上一篇 WebHost准备阶段 我们讲到了WebHostBuilder的初始化及配置。我们给WebHostBuilder进行以下配置 UseKestrel 设置Kestrel为HttpServer ConfigureAppConfiguration 设置了配置文件 ConfigureLogging 配置了日志处理器…

Vue及React脚手架安装

React npm i -g create-react-app create-react-app project React路由安装 yarn add react-router-dom Vue cnpm install -g vue-cli vue create project

Hadoop生态Flume(三)拦截器(Interceptor)介绍与使用(1)

转载自 Flume中的拦截器&#xff08;Interceptor&#xff09;介绍与使用&#xff08;一&#xff09; Flume中的拦截器&#xff08;interceptor&#xff09; 用户Source读取events发送到Sink的时候&#xff0c;在events header中加入一些有用的信息&#xff0c;或者对events的…

SOA对微服务的残余影响

近日&#xff0c;Tareq Abedrabbo在伦敦2017 Con微服务大会上说&#xff0c;SOA对微服务架构设计的残余影响仍然存在&#xff0c;包括技术选型和组织方面的问题。最直接的一个例子就是大多数企业仍然区分对待架构师和开发人员&#xff0c;架构师负责出规范&#xff0c;开发人员…

如何查看python安装路径

import sys sys.path 我的安装地址 python2: C:\Users\Tecna1205\Miniconda python3: C:\Users\Tecna1205\AppData\Local\Programs\Python\Python37 python3 -m pip install --upgrade pip --force-reinstall

jzoj4245-er【dp,贪心】

正题 题目大意 nnn个武器(n≤2n\leq2n≤2)&#xff0c;mmm个符文 符文1:直接改变一个武器的攻击力(最多一个) 符文2:增加一个武器的攻击力 符文3:使一个人的武器攻击力翻若干倍 求武器攻击力乘积最大&#xff0c;输出答案的自然对数。 解题思路 首先log(ab)log(a)log(b)lo…

Hadoop生态Flume(四)拦截器(Interceptor)介绍与使用(2)

转载自 Flume中的拦截器&#xff08;Interceptor&#xff09;介绍与使用&#xff08;二&#xff09; lume中的拦截器&#xff08;interceptor&#xff09;&#xff0c;用户Source读取events发送到Sink的时候&#xff0c;在events header中加入一些有用的信息&#xff0c;或者对…

协作更进一步:微软隆重介绍Visual Studio动态分享功能

微软刚刚在 Visual Studio Code 网站上宣布了“动态分享”&#xff08;Live Share&#xff09;功能&#xff0c;开发者们可以在 VS 2017 或 VS Code 中体验全新的实施协作。微软表示&#xff0c;Live Share 可让团队在相同的代码库上启用快速协作&#xff0c;而无需同步代码或配…

python打包exe文件

首先安装pyinstaller pip3 install pyinstaller接着导报指定文件 pyinstaller.exe -F 文件路径文件名 举例 pyinstaller.exe -F C:\Users\Tecna1205\Desktop\工作目录\Python工作目录\测试\3.3\test\tk.py 如果有图形界面&#xff0c;不想打开命令行&#xff0c;可在打包命令…

SpringBoot maven打包源码发布到仓库配置

一、项目pom.xml配置 添加发布仓库 配置上传源码 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocatio…

使用MS Test做单元测试

声明&#xff1a;本篇博客翻译自&#xff1a;http://www.c-sharpcorner.com/article/unit-testing-with-ms-tests-in-c-sharp/ 写在翻译之前&#xff1a; 依然清晰的记得刚工作的第一个项目中&#xff0c;在完成一个功能模块开发后&#xff0c;师傅让我把代码做一下单元测试。当…