springboot jpa + mongodb实现多条件查询数据

问题一:单条件查询如何查?

问题二:非IBaseMongoRepository接口原生的方法,采用自定义仓库?

问题三:如何结合jpa和mogodb语句从mogodb数据库查询出数据?

解决问题一:

单条件查询mogodb数据,直接定义接口仓库类:

public interface xxx extends IBaseMongoRepository<实体类, Serializable> {
}

原因:IBaseMongoRepository实现了在mogodb原生的sql增删改查功能,即存在:

      xxx .save();
      xxx .findAll();
      xxx .findOne();
      xxx .delete();

等等操作mogodb数据的方法。

解决问题二:

非IBaseMongoRepository接口原生的方法,自定义接口仓库类:

public interface xxx extends IBaseMongoRepository<实体类, Serializable> {

接收数据的类型  自定义名(参数);

}

解决问题三:

结合jpa和mogodb语句从mogodb数据库查询出数据:

举例多条件查询数据:


条件:设备号,序号,时间戳
规则1、当条件全部为空时,查询所有数据。
规则2、当其中某一个或者多个条件没有时,模糊匹配所有该字段下的所有数据。
规则3、当时间戳一头没有数据传入时,可以查询出另一头时间到上限或者下限日期。

控制层DeviceController:

public class DeviceController {

    @GetMapping("/aal/v1/selectByCondition")
    public Result selectByCondition(String imsi, String serialNumber, String buildTimeAfter, String buildTimeBefore) {
        aalService.selectByCondition(imsi, serialNumber, buildTimeAfter, buildTimeBefore);
        return Result.success();
    }

}

我在定义前端传参数的时候,不是没有想过将多个参数封装成一个req的请求对象类,也就是让前端直接将所有参数传到一个对象中,然后在业务层面直接取或者在sql层面直接取出动态判断。但是,在执行过程中,发现mogodb对sql的操作无法做到像操作mysql数据库一样动态实现sql语句的判断。鄙人才疏学浅,如有大佬知道,希望多多指教。

接口层AalService:

public interface AalService {

    Object selectByCondition(String imsi, String serialNumber, String buildTimeAfter, String buildTimeBefore);

}

在定义接口层面,未防止数据类型接收不对,我将类型统一成Object类型。

接口层实现类AalServiceImpl:

public class AalServiceImpl implements AalService {

 @Override
    public Object selectByCondition(String imsi, String serialNumber, String buildTimeAfter, String buildTimeBefore) {

        if (buildTimeAfter == null || "".equals(buildTimeAfter)) {
            buildTimeAfter = "2555-12-12";
        }
        if (buildTimeBefore == null || "".equals(buildTimeBefore)) {
            buildTimeBefore = "2000-01-01";
        }
        Date buildDateBefore = null;
        Date buildDateAfter = null;
        try {
            buildDateBefore = new SimpleDateFormat("yyyy-MM-dd").parse(buildTimeBefore);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            buildDateAfter = new SimpleDateFormat("yyyy-MM-dd").parse(buildTimeAfter);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<BiolandData> biolandDataList = biolandRepository.findPressureByISD(imsi, serialNumber, buildDateBefore, buildDateAfter);

        return biolandDataList;
    }
}

我利用postman模拟前端请求,发现传入日期格式为String类型,但数据表日期类型为Date类型,所以在日期数据上进行了转换,但在转换中可能出现异常,原本打算新建一个异常的类,用来接catch抛出来的异常并丢出信息给前端。

接口仓库类BiolandRepository,直接操作sql数据:

public interface BiolandRepository extends IBaseMongoRepository<BiolandData, Serializable> {

    @Query(value = "{" +
            "    imsiCode:{$regex:?0},\n" +
            "    serialNumber:{$regex:?1},\n" +
            "    buildTime:{$gte:?2,$lte:?3}\n" +
            "}")
    List<BiolandData> findPressureByISD(String imsi, String serialNumber, Date buildTimeBefore, Date buildTimeAfter);
}

这里利用了mogodb+jpa集成的一个类似于mybatis+mysql语句的格式。原因在于,如果以代码层次将数据经行增删改查,无疑在代码量上经行了猛烈抨击,多且复杂。这不符合我们常说的敏捷开发。@Query注解是基于(Spring Data JPA)的,其value值可以存入操作mogodb数据的语句。在mogodb里面,数据的查询语句以:

db.getCollection('biolandData').find({})

开始,在find({})的大括号里可以经行查询等操作。
举例解释:

imsiCode:{$regex:?0},\n

imsiCode为数据库字段;
\n ………换行符;
?0 ………对应 List<BiolandData> findPressureByISD(String imsi, String serialNumber, Date buildTimeBefore, Date buildTimeAfter)中下标为0的参数,即String imsi,也就是前端传入的imsi值;
$regex: ………为匹配符,当判断字符串是否为空时,可利用这个匹配符,可以看成mysql数据操作符like。也就是当你这个字段为空时,查询操作时,会将该字段下的所有数据查询出来。

由于

buildTime:{$gte:?2,$lte:?3}\n

这个字段中,是时间戳的形式。mogodb不会像mysql动态那样,判断某个时间节点没有传入参数也能查询出另一个时间条件下的所有记录。所以我在业务逻辑层定义了:

        if (buildTimeAfter == null || "".equals(buildTimeAfter)) {
            buildTimeAfter = "2555-12-12";
        }
        if (buildTimeBefore == null || "".equals(buildTimeBefore)) {
            buildTimeBefore = "2000-01-01";
        }

即判断传入的时间戳的值是否为空,若为空给定它一个范围较大的值,就解决了这个sql语句在一端为空时,另一端存在却无效的情况。这是个笨方法,如果有大佬有更好的方法,希望多多指教。

    原文作者:locality
    原文地址: https://www.jianshu.com/p/7ab5b497427e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞