From 9f84ca14f23c1541042a41618cd0ca86908151d8 Mon Sep 17 00:00:00 2001 From: linwl <304115325@qq.com> Date: Wed, 20 Jan 2021 08:30:30 +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 --- .../entity/mongo/IpProvinceEntity.java | 6 +- .../entity/mongo/ProvinceInfoEntity.java | 5 +- .../mapper/IpProvinceMapper.java | 55 +------------------ .../mapper/ProvinceInfoMapper.java | 49 +---------------- .../service/impl/ProvinceInfoServiceImpl.java | 22 +++++--- 5 files changed, 25 insertions(+), 112 deletions(-) diff --git a/src/main/java/com/telpo/dipperposition/entity/mongo/IpProvinceEntity.java b/src/main/java/com/telpo/dipperposition/entity/mongo/IpProvinceEntity.java index 42edb3d..0bc7e99 100644 --- a/src/main/java/com/telpo/dipperposition/entity/mongo/IpProvinceEntity.java +++ b/src/main/java/com/telpo/dipperposition/entity/mongo/IpProvinceEntity.java @@ -1,10 +1,9 @@ package com.telpo.dipperposition.entity.mongo; -import com.telpo.dipperposition.vo.IPProvinceVo; import lombok.Getter; import lombok.Setter; import lombok.ToString; -import models.BaseMongoDbEntity; +import org.springframework.data.mongodb.core.mapping.Document; /** * @program: IpProvinceEntity @@ -15,7 +14,8 @@ import models.BaseMongoDbEntity; @ToString @Getter @Setter -public class IpProvinceEntity extends BaseMongoDbEntity { +@Document +public class IpProvinceEntity { /** Ip */ private String ip; diff --git a/src/main/java/com/telpo/dipperposition/entity/mongo/ProvinceInfoEntity.java b/src/main/java/com/telpo/dipperposition/entity/mongo/ProvinceInfoEntity.java index 71cc442..38ad6d2 100644 --- a/src/main/java/com/telpo/dipperposition/entity/mongo/ProvinceInfoEntity.java +++ b/src/main/java/com/telpo/dipperposition/entity/mongo/ProvinceInfoEntity.java @@ -3,7 +3,7 @@ package com.telpo.dipperposition.entity.mongo; import lombok.Getter; import lombok.Setter; import lombok.ToString; -import models.BaseMongoDbEntity; +import org.springframework.data.mongodb.core.mapping.Document; /** * @program: IpProvinceEntity @@ -14,7 +14,8 @@ import models.BaseMongoDbEntity; @ToString @Getter @Setter -public class ProvinceInfoEntity extends BaseMongoDbEntity { +@Document +public class ProvinceInfoEntity { /** 所在省份 */ private String province; diff --git a/src/main/java/com/telpo/dipperposition/mapper/IpProvinceMapper.java b/src/main/java/com/telpo/dipperposition/mapper/IpProvinceMapper.java index c615355..b794c95 100644 --- a/src/main/java/com/telpo/dipperposition/mapper/IpProvinceMapper.java +++ b/src/main/java/com/telpo/dipperposition/mapper/IpProvinceMapper.java @@ -4,6 +4,7 @@ import com.telpo.dipperposition.annotation.MongoSwitch; import com.telpo.dipperposition.entity.mongo.IpProvinceEntity; import db.BaseMongoDbDao; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; @@ -15,56 +16,4 @@ import java.util.List; * @create: 2020-07-20 11:12 */ @Repository -public class IpProvinceMapper extends BaseMongoDbDao { - -// private static IpProvinceMapper ipProvinceMapper; - /** - * spring会自动从ioc容器当中根据IPProvinceMapper类型找到iPProvinceMapper, - * 当做参数传进来 - * - */ -// @Autowired -// public void setIPProvinceMapper(IpProvinceMapper ipProvinceMapper) { -// this.ipProvinceMapper = ipProvinceMapper; -// } - - @Override - protected Class getEntityClass() { - return IpProvinceEntity.class; - } - - @MongoSwitch("common") - public void saveIp(IpProvinceEntity entity) { - this.save(entity); - } - - @MongoSwitch("common") - public void saveIp(IpProvinceEntity entity, String collectionName) { - this.save(entity, collectionName); - } - - @MongoSwitch("common") - public void updateIpFirst(IpProvinceEntity srcObj, IpProvinceEntity targetObj) { - this.updateFirst(srcObj, targetObj); - } - - @Override - public List getPage(IpProvinceEntity object, int start, int size) { - return super.getPage(object, start, size); - } - - @MongoSwitch("common") - public List queryIpList(IpProvinceEntity object) { - return this.queryList(object); - } - - @MongoSwitch("common") - public List queryIpList(IpProvinceEntity object, String collectionName) { - return this.queryList(object, collectionName); - } - - @MongoSwitch("common") - public void deleteIpById(String id) { - this.deleteById(id); - } -} +public interface IpProvinceMapper extends MongoRepository {} diff --git a/src/main/java/com/telpo/dipperposition/mapper/ProvinceInfoMapper.java b/src/main/java/com/telpo/dipperposition/mapper/ProvinceInfoMapper.java index 3530e47..5059b33 100644 --- a/src/main/java/com/telpo/dipperposition/mapper/ProvinceInfoMapper.java +++ b/src/main/java/com/telpo/dipperposition/mapper/ProvinceInfoMapper.java @@ -4,6 +4,7 @@ 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; @@ -15,49 +16,5 @@ import java.util.List; * @create: 2020-07-20 11:12 */ @Repository -public class ProvinceInfoMapper extends BaseMongoDbDao { - - - @Override - protected Class getEntityClass() { - - return ProvinceInfoEntity.class; - } - - @MongoSwitch("common") - public void saveIp(ProvinceInfoEntity entity) { - this.save(entity); - } - - @MongoSwitch("common") - public void saveIp(ProvinceInfoEntity entity, String collectionName) { - - this.save(entity, collectionName); - } - - @MongoSwitch("common") - public void updateIpFirst(ProvinceInfoEntity srcObj, ProvinceInfoEntity targetObj) { - this.updateFirst(srcObj, targetObj); - } - - @Override - public List getPage(ProvinceInfoEntity object, int start, int size) { - return super.getPage(object, start, size); - } - - @MongoSwitch("common") - public List queryIpList(ProvinceInfoEntity object) { - - return this.queryList(object); - } - - @MongoSwitch("common") - public List queryIpList(ProvinceInfoEntity object, String collectionName) { - return this.queryList(object, collectionName); - } - - @MongoSwitch("common") - public void deleteIpById(String id) { - this.deleteById(id); - } -} +public interface ProvinceInfoMapper + extends MongoRepository {} 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 657868a..da3f873 100644 --- a/src/main/java/com/telpo/dipperposition/service/impl/ProvinceInfoServiceImpl.java +++ b/src/main/java/com/telpo/dipperposition/service/impl/ProvinceInfoServiceImpl.java @@ -1,14 +1,18 @@ package com.telpo.dipperposition.service.impl; -import com.telpo.dipperposition.entity.mongo.IpProvinceEntity; +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.collection.CollectionUtil; import com.telpo.dipperposition.entity.mongo.ProvinceInfoEntity; -import com.telpo.dipperposition.mapper.IpProvinceMapper; import com.telpo.dipperposition.mapper.ProvinceInfoMapper; import com.telpo.dipperposition.service.IProvinceInfoService; 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.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; +import tools.BeanTools; import java.util.List; @@ -39,7 +43,7 @@ public class ProvinceInfoServiceImpl implements IProvinceInfoService { @Override public boolean romveById(String id) { - provinceInfoMapper.deleteIpById(id); + provinceInfoMapper.deleteById(id); return false; } @@ -48,9 +52,12 @@ public class ProvinceInfoServiceImpl implements IProvinceInfoService { try { ProvinceInfoEntity query = new ProvinceInfoEntity(); query.setProvince(provicne); - List records = provinceInfoMapper.queryIpList(query); - if (ObjectUtils.isNotEmpty(records)) { - return records.get(0); + Example example = Example.of(query); + PageRequest pageable = PageRequest.of(0, 1); + Page data = provinceInfoMapper.findAll(example, pageable); + if (CollectionUtil.isNotEmpty(data.getContent())) { + List provinceInfos = BeanTools.copyList(data.getContent(), ProvinceInfoEntity.class); + return provinceInfos.get(0); } else { return null; } @@ -63,8 +70,7 @@ public class ProvinceInfoServiceImpl implements IProvinceInfoService { @Override public List getProvinceInfoEntitys() { try { - ProvinceInfoEntity query = new ProvinceInfoEntity(); - List records = provinceInfoMapper.queryIpList(query); + List records = provinceInfoMapper.findAll(); if (ObjectUtils.isNotEmpty(records)) { return records; } else {