springboot集成mongodb

2020-06-12 18:29:15 浏览数 (1)

1导入pom文件

代码语言:javascript复制
<!-- mongodb -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>

2编写配置文件

代码语言:javascript复制
spring:
  data:
    mongodb:
      host: *
      port: 27017
      database: appReport
      username: *
      password: *

3.编写基础 MongoDbDao

代码语言:javascript复制
package com.un.common.utils.mongodb;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
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 java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author shiye
 * @create 2020-06-08 10:18
 * @Description: mongoDB基础方法封装
 */
public abstract class MongoDbDao<T> {

    protected Logger logger = LoggerFactory.getLogger(MongoDbDao.class);

    /**
     * 反射获取泛型类型
     *
     * @return
     */
    protected abstract Class<T> getEntityClass();

    @Autowired
    private MongoTemplate mongoTemplate;

    /***
     * 保存一个对象
     * @param t
     */
    public void save(T t) {
        logger.info("-------------->MongoDB save start");
        this.mongoTemplate.save(t);
    }

    /***
     * 根据id从几何中查询对象
     * @param id
     * @return
     */
    public T queryById(Integer id) {
        Query query = new Query(Criteria.where("_id").is(id));
        logger.info("-------------->MongoDB find start");
        return this.mongoTemplate.findOne(query, this.getEntityClass());
    }

    /**
     * 根据条件查询集合
     *
     * @param object
     * @return
     */
    public List<T> queryList(T object) {
        Query query = getQueryByObject(object);
        logger.info("-------------->MongoDB find start");
        return mongoTemplate.find(query, this.getEntityClass());
    }

    /**
     * 根据条件查询只返回一个文档
     *
     * @param object
     * @return
     */
    public T queryOne(T object) {
        Query query = getQueryByObject(object);
        logger.info("-------------->MongoDB find start");
        return mongoTemplate.findOne(query, this.getEntityClass());
    }

    /***
     * 根据条件分页查询
     * @param object
     * @param start 查询起始值
     * @param size  查询大小
     * @return
     */
    public List<T> getPage(T object, int start, int size) {
        Query query = getQueryByObject(object);
        query.skip(start);
        query.limit(size);
        logger.info("-------------->MongoDB queryPage start");
        return this.mongoTemplate.find(query, this.getEntityClass());
    }

    /***
     * 根据条件查询库中符合条件的记录数量
     * @param object
     * @return
     */
    public Long getCount(T object) {
        Query query = getQueryByObject(object);
        logger.info("-------------->MongoDB Count start");
        return this.mongoTemplate.count(query, this.getEntityClass());
    }

    /***
     * 删除对象
     * @param t
     * @return
     */
    public int delete(T t) {
        logger.info("-------------->MongoDB delete start");
        return (int) this.mongoTemplate.remove(t).getDeletedCount();
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void deleteById(Integer id) {
        Criteria criteria = Criteria.where("_id").is(id);
        if (null != criteria) {
            Query query = new Query(criteria);
            T obj = this.mongoTemplate.findOne(query, this.getEntityClass());
            logger.info("-------------->MongoDB deleteById start");
            if (obj != null) {
                this.delete(obj);
            }
        }
    }

    /*MongoDB中更新操作分为三种
     * 1:updateFirst     修改第一条
     * 2:updateMulti     修改所有匹配的记录
     * 3:upsert  修改时如果不存在则进行添加操作
     * */

    /**
     * 修改匹配到的第一条记录
     *
     * @param srcObj
     * @param targetObj
     */
    public void updateFirst(T srcObj, T targetObj) {
        Query query = getQueryByObject(srcObj);
        Update update = getUpdateByObject(targetObj);
        logger.info("-------------->MongoDB updateFirst start");
        this.mongoTemplate.updateFirst(query, update, this.getEntityClass());
    }

    /***
     * 修改匹配到的所有记录
     * @param srcObj
     * @param targetObj
     */
    public void updateMulti(T srcObj, T targetObj) {
        Query query = getQueryByObject(srcObj);
        Update update = getUpdateByObject(targetObj);
        logger.info("-------------->MongoDB updateFirst start");
        this.mongoTemplate.updateMulti(query, update, this.getEntityClass());
    }

    /***
     * 修改匹配到的记录,若不存在该记录则进行添加
     * @param srcObj
     * @param targetObj
     */
    public void updateInsert(T srcObj, T targetObj) {
        Query query = getQueryByObject(srcObj);
        Update update = getUpdateByObject(targetObj);
        logger.info("-------------->MongoDB updateInsert start");
        this.mongoTemplate.upsert(query, update, this.getEntityClass());
    }

    /**
     * 将查询条件对象转换为query
     *
     * @param object
     * @return
     * @author Jason
     */
    private Query getQueryByObject(T object) {
        Query query = new Query();
        String[] fileds = getFiledName(object);
        Criteria criteria = new Criteria();
        for (int i = 0; i < fileds.length; i  ) {
            String filedName = (String) fileds[i];
            Object filedValue = getFieldValueByName(filedName, object);
            if (filedValue != null) {
                criteria.and(filedName).is(filedValue);
            }
        }
        query.addCriteria(criteria);
        return query;
    }

    /**
     * 将查询条件对象转换为update
     *
     * @param object
     * @return
     * @author Jason
     */
    private Update getUpdateByObject(T object) {
        Update update = new Update();
        String[] fileds = getFiledName(object);
        for (int i = 0; i < fileds.length; i  ) {
            String filedName = (String) fileds[i];
            Object filedValue = getFieldValueByName(filedName, object);
            if (filedValue != null) {
                update.set(filedName, filedValue);
            }
        }
        return update;
    }

    /***
     * 获取对象属性返回字符串数组
     * @param o
     * @return
     */
    private static String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];

        for (int i = 0; i < fields.length;   i) {
            fieldNames[i] = fields[i].getName();
        }

        return fieldNames;
    }

    /***
     * 根据属性获取对象属性值
     * @param fieldName
     * @param o
     * @return
     */
    private static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String e = fieldName.substring(0, 1).toUpperCase();
            String getter = "get"   e   fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[0]);
            return method.invoke(o, new Object[0]);
        } catch (Exception var6) {
            return null;
        }
    }
}

4.编写需要具体处理的对象

代码语言:javascript复制
package com.un.project.system.domain.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.NotNull;

/**
 * App上报对象
 *
 * @author shiye
 * @create 2020-06-08 10:25
 */
@ApiModel
public class AppReportVo {

    @ApiModelProperty(value = "住户id")
    @NotNull(message = "住户id不能为空")
    private String hId;

    @ApiModelProperty(value = "社区id")
    @NotNull(message = "社区id不能为空")
    private String coId;

   

    public String gethId() {
        return hId;
    }

    public void sethId(String hId) {
        this.hId = hId;
    }

    public String getCoId() {
        return coId;
    }

    public void setCoId(String coId) {
        this.coId = coId;
    }   
}

5.编写服务类(具体使用到了哪个方法,就在这个接口定义一下这个方法)

代码语言:javascript复制
package com.un.project.system.service;

import com.un.project.system.domain.vo.AppReportVo;

import java.util.List;

/**
 * @author shiye
 * @create 2020-06-09 9:01
 */
public interface AppReportService {

    /**
     * 保存一条数据
     *
     * @param appReportVo
     */
    public void save(AppReportVo appReportVo);

    /**
     * 根据条件查询集合
     *
     * @param appReportVo
     * @return
     */
    public List<AppReportVo> queryList(AppReportVo appReportVo);
}
代码语言:javascript复制
package com.un.project.system.service.impl;

import com.un.common.utils.mongodb.MongoDbDao;
import com.un.project.system.domain.vo.AppReportVo;
import com.un.project.system.service.AppReportService;
import org.springframework.stereotype.Service;

/**
 * @author shiye
 * @create 2020-06-09 9:03
 */
@Service
public class AppReportServiceImpl extends MongoDbDao<AppReportVo> implements AppReportService {
    @Override
    protected Class<AppReportVo> getEntityClass() {
        return AppReportVo.class;
    }


}

6.编写controller测试数据

代码语言:javascript复制
package com.un.project.system.controller;

import com.un.framework.web.controller.BaseController;
import com.un.framework.web.domain.AjaxResult;
import com.un.project.system.domain.vo.AppReportVo;
import com.un.project.system.service.AppReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据上报接口
 *
 * @author shiye
 * @create 2020-06-09 9:20
 */
@RestController
@RequestMapping("/appReport")
@Api("数据上报接口")
public class AppReportController extends BaseController {

    @Autowired
    private AppReportService appReportService;

    /**
     * 存储一条数据
     *
     * @param appReportVo
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("存储一条数据")
    public AjaxResult save(@RequestBody @Validated AppReportVo appReportVo) {
        appReportService.save(appReportVo);
        return AjaxResult.success();
    }

    /**
     * 根据条件查询列表
     *
     * @param appReportVo
     * @return
     */
    @PostMapping("/queryList")
    @ApiOperation("根据条件查询列表")
    public AjaxResult queryList(@RequestBody AppReportVo appReportVo) {
        List<AppReportVo> list = appReportService.queryList(appReportVo);
        return AjaxResult.success(list);
    }
}

0 人点赞