文章目录
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
spring:
data:
mongodb:
host: localhost
port: 27017
database: bid_manager
uri: mongodb://localhost:27017/bid_manager
工具类
package com.dcits.bid.common.mongodb.util;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dcits.bid.common.core.util.SpringUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class MongoCtxUtil {
private static final Query EMPTY_QUERY = new BasicQuery("{}");
private static final MongoTemplate template;
private static final String PRIMKARY_KEY = "_id";
static {
template = SpringUtil.getBean(MongoTemplate.class);
}
private Criteria criteria;
/**
* 监测一个空间是否存在
*
* @param collection 名称
* @return
*/
public static boolean isExist(String collection) {
return template.collectionExists(collection);
}
/**
* 创建新空间
*
* @param collection
*/
public static void createCollection(String collection) {
template.createCollection(collection);
}
/**
* 删除空间
*
* @param
*/
public static void dropCollection(String collection) {
template.dropCollection(collection);
}
/**
* 获取总记录数
*
* @param
* @return
*/
public static long totalCount(String collection) {
return template.count(new Query(), collection);
}
/**
* 按照查询条件获取记录数
*
* @param query
* @param
* @return
*/
public static long count(Query query, String collection) {
return template.count(query, collection);
}
/**
* 查询某个collection中所有数据,并映射为List<map>类型
*
* @return
*/
public static List<Map> findAllForMap(String collection) {
return template.findAll(Map.class, collection);
}
/**
* 根据条件查询,获取List<Map>类型
*
* @param query
* @param collection
* @return
*/
public static List<Map> findForMap(Query query, String collection) {
return template.find(query, Map.class, collection);
}
/**
* 根据主键查询某个集合的数据,并返回map类型
* @param id
* @param collection
* @return
*/
public static Map findOne(Serializable id, String collection){
Criteria criteria = Criteria.where(PRIMKARY_KEY).is(id);
return template.findOne(Query.query(criteria),Map.class,collection);
}
/**
* 获取集合
* @param idList
* @param collection
* @return
*/
public static List<Map> findByIdInQuery(Collection<? extends Serializable> idList,String collection){
Criteria criteria = Criteria.where(PRIMKARY_KEY).in(idList);
return template.find(Query.query(criteria),Map.class,collection);
}
/**
* 根据参数进行筛选查询获得集合
* @param data
* @param collection
* @return
*/
public static List<Map> findByParams(Map<String, Object> data,String collection){
Query query = EMPTY_QUERY;
if (!CollectionUtils.isEmpty(data)) {
Criteria criteria = new Criteria();
data.forEach((k, v) -> criteria.and(k).is(v));
query = Query.query(criteria);
}
return template.find(query,Map.class,collection);
}
/**
* 查询collection中所有内容,并分页
* @param pageNo
* @param pageSize
* @param collection
* @return
*/
public static Page<Map> pageList(int pageNo,int pageSize,String collection){
Query query = new Query();
//增加筛选条件
// query.addCriteria(Criteria.where("").is());
long count = template.count(query,Map.class,collection);
query.with(Sort.by(Sort.Order.desc(PRIMKARY_KEY)));//倒序排列,最后新建的职员_id越大
query.skip((pageNo-1)*pageSize);
//设置查询条数
query.limit(pageSize);
//查询当前页数据集合
List<Map> pageList = template.find(query,Map.class,collection);
//创建分页实体对象
Page<Map> page = new Page<>();
page.setRecords(pageList);
page.setTotal(count);
page.setSize(count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
return page;
}
/**
* 保存单个对象
* @param entity
* @param collection
*/
public static void saveOne(Map entity,String collection){
template.save(entity,collection);
}
/**
* 批量插入,如果已经存在此内容,则忽略
* @param list
* @param collection
*/
public static void insertBatch(List<Map<String,Object>> list,String collection){
template.insert(list,collection);
}
/**
* 批量修改
*
* @param query
* @param newKeyVal
* @param collection
*/
public static void updateMulti(Query query, Map<String, Object> newKeyVal,String collection) {
Update update = new Update();
for (Map.Entry<String, Object> entry : newKeyVal.entrySet()) {
update = update.set(entry.getKey(), entry.getValue());
}
template.updateMulti(query, update, collection);
}
/**
* 根据主键获取并删除
* @param id
* @param collection
*/
public static void removeById(String id, String collection) {
template.remove(new Query(Criteria.where(PRIMKARY_KEY).is(id)), collection);
}
/**
* 查找并删除
* @param query
* @param collection
*/
public static void removeByQuery(Query query,String collection){
template.findAllAndRemove(query,collection);
}
}