From 53b19e53c5860549fc407763414926030d541138 Mon Sep 17 00:00:00 2001 From: linwl <304115325@qq.com> Date: Wed, 20 Jan 2021 21:23:52 +0800 Subject: [PATCH] =?UTF-8?q?mongodb=E8=AE=BF=E9=97=AE=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/ProvinceInfoMapper.java | 5 ----- .../service/impl/IpProvinceServiceImpl.java | 17 ++++++++--------- .../service/impl/ProvinceInfoServiceImpl.java | 15 ++++++--------- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/telpo/dipperposition/mapper/ProvinceInfoMapper.java b/src/main/java/com/telpo/dipperposition/mapper/ProvinceInfoMapper.java index 5059b33..2033a8e 100644 --- a/src/main/java/com/telpo/dipperposition/mapper/ProvinceInfoMapper.java +++ b/src/main/java/com/telpo/dipperposition/mapper/ProvinceInfoMapper.java @@ -1,14 +1,9 @@ package com.telpo.dipperposition.mapper; -import com.telpo.dipperposition.annotation.MongoSwitch; -import com.telpo.dipperposition.entity.mongo.IpProvinceEntity; import com.telpo.dipperposition.entity.mongo.ProvinceInfoEntity; -import db.BaseMongoDbDao; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; -import java.util.List; - /** * @program: ProvinceInfoMapper * @description: 省份位置记录mapper diff --git a/src/main/java/com/telpo/dipperposition/service/impl/IpProvinceServiceImpl.java b/src/main/java/com/telpo/dipperposition/service/impl/IpProvinceServiceImpl.java index b47baed..deb0626 100644 --- a/src/main/java/com/telpo/dipperposition/service/impl/IpProvinceServiceImpl.java +++ b/src/main/java/com/telpo/dipperposition/service/impl/IpProvinceServiceImpl.java @@ -5,16 +5,19 @@ import com.telpo.dipperposition.entity.mongo.IpProvinceEntity; import com.telpo.dipperposition.entity.mongo.ProvinceInfoEntity; import com.telpo.dipperposition.mapper.IpProvinceMapper; import com.telpo.dipperposition.service.IpProvinceService; +import jdk.nashorn.internal.runtime.options.Option; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; +import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import tools.BeanTools; import java.util.List; +import java.util.Optional; /** * @program: DataPushServer @@ -56,15 +59,11 @@ public class IpProvinceServiceImpl implements IpProvinceService { try { IpProvinceEntity query = new IpProvinceEntity(); query.setIp(ipAddress); - Example example = Example.of(query); - PageRequest pageable = PageRequest.of(0, 1); - Page data = ipProvinceMapper.findAll(example, pageable); - if (CollectionUtil.isNotEmpty(data.getContent())) { - List records = BeanTools.copyList(data.getContent(), IpProvinceEntity.class); - return records.get(0); - } else { - return null; - } + //忽略_class属性,不参与查询 + ExampleMatcher matcher = ExampleMatcher.matching().withIgnorePaths("_class"); + Example example = Example.of(query, matcher); + Optional data = ipProvinceMapper.findOne(example); + return data.get(); } catch (Exception e) { log.error("获取IP省份异常:", e); return null; diff --git a/src/main/java/com/telpo/dipperposition/service/impl/ProvinceInfoServiceImpl.java b/src/main/java/com/telpo/dipperposition/service/impl/ProvinceInfoServiceImpl.java index 6ccb38a..668f2ad 100644 --- a/src/main/java/com/telpo/dipperposition/service/impl/ProvinceInfoServiceImpl.java +++ b/src/main/java/com/telpo/dipperposition/service/impl/ProvinceInfoServiceImpl.java @@ -10,6 +10,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; +import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; @@ -57,15 +58,11 @@ public class ProvinceInfoServiceImpl implements IProvinceInfoService { try { ProvinceInfoEntity query = new ProvinceInfoEntity(); query.setProvince(provicne); - Example example = Example.of(query); - PageRequest pageable = PageRequest.of(0, 1); - Page data = provinceInfoMapper.findAll(example, pageable); - if (CollectionUtil.isNotEmpty(data.getContent())) { - List records = BeanTools.copyList(data.getContent(), ProvinceInfoEntity.class); - return records.get(0); - } else { - return null; - } + //忽略_class属性,不参与查询 + ExampleMatcher matcher = ExampleMatcher.matching().withIgnorePaths("_class"); + Example example = Example.of(query, matcher); + Optional data = provinceInfoMapper.findOne(example); + return data.get(); } catch (Exception e) { log.error("获取省份异常:", e); return null;