栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot整合Mongodb实战

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot整合Mongodb实战

一、添加依赖
        
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        
二、yml配置

此处我用的是本地的Mongodb,所有可以省略用户名和密码

spring:
	data:
		mongodb:
			uri: mongodb://localhost:27017/数据库名称

有用户名和密码的格式:其中name是用户名,pass是密码

spring:
	data:
		mongodb:
			uri=mongodb://name:pass@localhost:27017/数据库名称
三、封装工具类
@Component
public class MongoUtils {

    public static MongoUtils mongoUtils;

    @PostConstruct
    public void init() {
        mongoUtils = this;
        mongoUtils.mongoTemplate = this.mongoTemplate;
    }

    @Autowired
    private MongoTemplate mongoTemplate;
}
下面的方法是自己封装的一些基础增删改查,直接写在上面定义的工具类即可(代码写的不好,大佬勿喷)
    
    public static Map saveMap(Map map,String collectionName){
        if(map == null || map.size() < 1 || StringUtils.isBlank(collectionName)){
            return null;
        }
        return mongoUtils.mongoTemplate.insert(map, collectionName);
    }
    
    public static Collection> saveList(List> list, String collectionName){
        if(list == null || list.size() < 1 || StringUtils.isBlank(collectionName)){
            return null;
        }
        return mongoUtils.mongoTemplate.insert(list, collectionName);
    }
    
    public static void removeById(String id, String collectionName) {
        if(StringUtils.isBlank(id) || StringUtils.isBlank(collectionName)){
            return;
        }
        Query query = Query.query(Criteria.where("_id").is(id));
        mongoUtils.mongoTemplate.remove(query, collectionName);
    }
    
    public static void remove(Query query, String collectionName) {
        if(query == null || StringUtils.isBlank(collectionName)){
            return;
        }
        mongoUtils.mongoTemplate.remove(query, collectionName);
    }
  
    public static Object findById(String id, String collectionName) {
        if(StringUtils.isBlank(collectionName) || StringUtils.isBlank(id)){
            return new ArrayList<>();
        }
        return mongoUtils.mongoTemplate.findById(id, Object.class, collectionName);
    }

    
    public static List find(Query query, String collectionName) {
        if(StringUtils.isBlank(collectionName) || query == null){
            return new ArrayList<>();
        }
        return mongoUtils.mongoTemplate.find(query, Object.class, collectionName);
    }
    
    public static Page pageList (int currentPage, int size,String collectionName,Query query){
        if(currentPage < 1){
            currentPage = 1;
        }
        if (size < 1){
            size = 10;
        }
        if(StringUtils.isBlank(collectionName) || query == null){
            return Page.empty();
        }
        long count = mongoUtils.mongoTemplate.count(query, collectionName);
        if (count == 0) {
            return Page.empty();
        }
        Sort sort = Sort.by(Sort.Direction.ASC, "title");
        Pageable pageable = PageRequest.of(currentPage - 1, size,sort);
        query.with(pageable);

        List list = mongoUtils.mongoTemplate.find(query, Object.class,collectionName);

        PageImpl page = new PageImpl<>(list, pageable, count);
        return page;
    }
 
    
    public static Map updateMulti(Query query, Map map, String collectionName) {
        if(map == null || map.size() < 1 || query == null || StringUtils.isBlank(collectionName)){
            return new HashMap<>();
        }
        Update update = new Update();
        for (String key : map.keySet()){
            update.set(key, map.get(key));
        }
        Map newMap = (Map)mongoUtils.mongoTemplate.updateMulti(query, update, collectionName);
        return newMap;
    }
四、调用

方法调用都差不多,只举例说明一下

// 参数一:保存的数据,参数二:集合名称(类似于表的意思)
MongoUtils.saveMap(map,"paragraph");
// 参数一:query(条件),参数二:集合名称(类似于表的意思)
MongoUtils.updateMulti(new Query().addCriteria(Criteria.where("_id").is(paragraphInfoVo.get_id())), map, "paragraph");

因为我的需求没法创建实体操作,所以代码里都是map和list的操作

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号