From f25689aed4bcbfa018359b0ba6b1552d6a261747 Mon Sep 17 00:00:00 2001 From: t-baby Date: Tue, 6 Sep 2016 04:30:44 +0800 Subject: [PATCH 1/5] fix bug --- src/main/java/com/cybermkd/kit/MongoKit.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/cybermkd/kit/MongoKit.java b/src/main/java/com/cybermkd/kit/MongoKit.java index cf273d6..1e12ddd 100644 --- a/src/main/java/com/cybermkd/kit/MongoKit.java +++ b/src/main/java/com/cybermkd/kit/MongoKit.java @@ -106,7 +106,7 @@ public List find(String collectionName, int limit, Bson sort, Bson p } public List find(String collectionName, int limit, int skip, Bson sort, Bson projection, String join) { - return find(collectionName, new BsonDocument(), projection, sort, limit, 0, join); + return find(collectionName, new BsonDocument(), projection, sort, limit, skip, join); } public List find(String collectionName, int limit, Bson sort, Bson projection, Class clazz) { @@ -143,7 +143,7 @@ public T findOne(String collectionName, Bson query, Bson sort, String join, , clazz); } - public List find(String collectionName, Bson query, Bson projection, Bson sort, int limit, + public List find(String collectionName, Bson query, Bson sort, Bson projection, int limit, int skip, String join) { final List list = new ArrayList(); @@ -162,7 +162,7 @@ public void apply(Document document) { } - public List find(String collectionName, Bson query, Bson projection, Bson sort, int limit, int skip, + public List find(String collectionName, Bson query, Bson sort, Bson projection, int limit, int skip, String join, Class clazz) { final List list = new ArrayList(); From 308014ef21c598f27ecfe92dce607466e7641210 Mon Sep 17 00:00:00 2001 From: t-baby Date: Wed, 7 Sep 2016 14:07:33 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E5=9C=A8=E8=BE=93=E5=85=A5=E7=9A=84count?= =?UTF-8?q?=E8=B6=85=E8=BF=87=E6=80=BB=E6=95=B0=E6=97=B6=E4=BC=9A=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=BF=94=E5=9B=9E=E6=89=80=E6=9C=89=E3=80=82=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=85=A8=E6=96=B0=E7=9A=84id=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/cybermkd/kit/MongoKit.java | 27 ++++++++++++++++--- .../java/com/cybermkd/kit/MongoPaginate.java | 8 +++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/cybermkd/kit/MongoKit.java b/src/main/java/com/cybermkd/kit/MongoKit.java index 1e12ddd..8ae7673 100644 --- a/src/main/java/com/cybermkd/kit/MongoKit.java +++ b/src/main/java/com/cybermkd/kit/MongoKit.java @@ -52,14 +52,14 @@ public MongoCollection getCollection(String collectionName) { public long insert(String collectionName, List docs) { long before = getCollection(collectionName).count(); - getCollection(collectionName).insertMany(docs); + getCollection(collectionName).insertMany(uniding(docs)); return getCollection(collectionName).count() - before; } public long insert(String collectionName, Document doc) { long before = getCollection(collectionName).count(); - getCollection(collectionName).insertOne(doc); + getCollection(collectionName).insertOne(uniding(doc)); return getCollection(collectionName).count() - before; } @@ -282,7 +282,28 @@ private Document iding(Document document) { if (document == null || document.get("_id") == null) { return document; } else { - document.put("id", document.get("_id").toString()); + document.put("_id", document.get("_id").toString()); + } + } catch (ClassCastException e) { + /*如果转换出错直接返回原本的值,不做任何处理*/ + + } + return document; + } + + private List uniding(List list) { + List newList=new ArrayList(); + for (Document doc:list) { + newList.add(uniding(doc)); + } + return newList; + } + + private Document uniding(Document document) { + try { + if (document == null || document.get("_id") == null) { + return document; + } else { document.remove("_id"); } } catch (ClassCastException e) { diff --git a/src/main/java/com/cybermkd/kit/MongoPaginate.java b/src/main/java/com/cybermkd/kit/MongoPaginate.java index 25cf571..4ef7bf1 100644 --- a/src/main/java/com/cybermkd/kit/MongoPaginate.java +++ b/src/main/java/com/cybermkd/kit/MongoPaginate.java @@ -31,10 +31,16 @@ public MongoPaginate(MongoQuery query, int count, int page) { ++totalPage; } - if (count <= 0 || count > totalRow || page <= 0 || page > totalPage || (long) page * (long) count > totalRow) { + if (count > totalRow) { + this.count = (int) totalRow; + } + + if (this.count <= 0 || this.page <= 0 || this.page > totalPage || (long) this.page * (long) this.count > totalRow) { throw new RuntimeException("MongPage tips: (づ ̄ 3 ̄)づ count or page is error !"); } + + this.firstPage = this.page == 1; this.lastPage = this.page == this.totalPage; From 446060547322b3f3bbc218dbced5f47561f24d21 Mon Sep 17 00:00:00 2001 From: t-baby Date: Thu, 15 Sep 2016 22:18:43 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7=E3=80=81=E4=B8=8D=E5=86=8D=E6=89=93=E5=8C=85ICEREST?= =?UTF-8?q?=E3=80=81=E6=9B=BF=E6=8D=A2=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E3=80=81=E4=BF=AE=E6=94=B9=E5=AE=89=E5=85=A8=E5=86=99=E5=85=A5?= =?UTF-8?q?=E7=9A=84=E7=BA=A7=E5=88=AB=E3=80=81=E4=BF=AE=E6=94=B9save?= =?UTF-8?q?=E7=9A=84=E6=88=90=E5=8A=9F=E4=B8=8E=E5=90=A6=E7=9A=84=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 ++-- src/main/java/com/cybermkd/kit/MongoKit.java | 20 ++++++-------- .../java/com/cybermkd/kit/MongoQuery.java | 26 ++++++++++++------- .../java/com/cybermkd/plugin/MongoPlugin.java | 3 ++- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index df86616..af28d4f 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.cybermkd MongodbPlugin - 1.0.7 + 1.0.7.1 jar MongodbPlugin @@ -85,13 +85,14 @@ provided - com.cybermkd ICEREST 1.0.1.3 + provided + ch.qos.logback diff --git a/src/main/java/com/cybermkd/kit/MongoKit.java b/src/main/java/com/cybermkd/kit/MongoKit.java index 8ae7673..a3b12a5 100644 --- a/src/main/java/com/cybermkd/kit/MongoKit.java +++ b/src/main/java/com/cybermkd/kit/MongoKit.java @@ -2,8 +2,6 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.cybermkd.common.util.Stringer; -import com.cybermkd.log.Logger; import com.mongodb.Block; import com.mongodb.DBRef; import com.mongodb.MongoClient; @@ -15,6 +13,8 @@ import org.bson.BsonDocument; import org.bson.Document; import org.bson.conversions.Bson; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.validation.ConstraintViolation; import javax.validation.Validation; @@ -35,7 +35,7 @@ public enum MongoKit { INSTANCE; private static MongoClient client; private static MongoDatabase defaultDb; - private static Logger logger = Logger.getLogger(MongoKit.class); + private static Logger logger = LoggerFactory.getLogger(MongoKit.class.getName()); public MongoClient getClient() { return client; @@ -50,17 +50,13 @@ public MongoCollection getCollection(String collectionName) { return defaultDb.getCollection(collectionName); } - public long insert(String collectionName, List docs) { - long before = getCollection(collectionName).count(); + public void insert(String collectionName, List docs) { getCollection(collectionName).insertMany(uniding(docs)); - return getCollection(collectionName).count() - before; } - public long insert(String collectionName, Document doc) { - long before = getCollection(collectionName).count(); + public void insert(String collectionName, Document doc) { getCollection(collectionName).insertOne(uniding(doc)); - return getCollection(collectionName).count() - before; } public List aggregate(String collectionName, List query, boolean allowDiskUse) { @@ -292,8 +288,8 @@ private Document iding(Document document) { } private List uniding(List list) { - List newList=new ArrayList(); - for (Document doc:list) { + List newList = new ArrayList(); + for (Document doc : list) { newList.add(uniding(doc)); } return newList; @@ -334,7 +330,7 @@ private Document jointing(Document document, String join) { private JSONObject parseObject(String json) { try { - if (Stringer.notBlank(json)) { + if (json!=null&&!json.isEmpty()) { return JSON.parseObject(json); } return new JSONObject(); diff --git a/src/main/java/com/cybermkd/kit/MongoQuery.java b/src/main/java/com/cybermkd/kit/MongoQuery.java index 628ddf1..0ca3adb 100644 --- a/src/main/java/com/cybermkd/kit/MongoQuery.java +++ b/src/main/java/com/cybermkd/kit/MongoQuery.java @@ -278,17 +278,25 @@ public MongoQuery byId(String id) { return this; } - public long save() { - long row = MongoKit.INSTANCE.insert(collectionName, document); - this.id = this.document.getObjectId("_id").toString(); - document.clear(); - return row; + public boolean save() { + try { + MongoKit.INSTANCE.insert(collectionName, document); + this.id = this.document.getObjectId("_id").toString(); + document.clear(); + return true; + } catch (RuntimeException e) { + return false; + } } - public long saveList() { - long row = MongoKit.INSTANCE.insert(collectionName, documents); - documents.clear(); - return row; + public boolean saveList() { + try { + MongoKit.INSTANCE.insert(collectionName, documents); + documents.clear(); + return true; + } catch (RuntimeException e) { + return false; + } } diff --git a/src/main/java/com/cybermkd/plugin/MongoPlugin.java b/src/main/java/com/cybermkd/plugin/MongoPlugin.java index 0f0d8b5..e45196a 100644 --- a/src/main/java/com/cybermkd/plugin/MongoPlugin.java +++ b/src/main/java/com/cybermkd/plugin/MongoPlugin.java @@ -103,8 +103,9 @@ public MongoPlugin writeConcern() { return this; } + /*在分布式下写到多个节点后才进行返回*/ public MongoPlugin writeSafe() { - this.options.writeConcern(WriteConcern.SAFE); + this.options.writeConcern(WriteConcern.MAJORITY); return this; } From ca64dca33326bd4333aaac93b708edc2b49005ff Mon Sep 17 00:00:00 2001 From: t-baby Date: Mon, 19 Sep 2016 15:24:20 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E5=99=A8=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- target/classes/ValidationMessages.properties | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 target/classes/ValidationMessages.properties diff --git a/target/classes/ValidationMessages.properties b/target/classes/ValidationMessages.properties new file mode 100644 index 0000000..4a70809 --- /dev/null +++ b/target/classes/ValidationMessages.properties @@ -0,0 +1,4 @@ +com.cybermkd.constraints.Chinese.message=${validatedValue} must ${value == true ? 'be chinese' : ' not be chinese'} +com.cybermkd.constraints.Type.message=${validatedValue} must be {value} +com.cybermkd.constraints.Inside.message=${validatedValue} must be in {value} +com.cybermkd.constraints.Exist.message=${validatedValue} ${value == true ? 'must be exist' : 'must not repeat '} \ No newline at end of file From fbefdded66424cc06ebddd3d61c52ccf5e81cdb6 Mon Sep 17 00:00:00 2001 From: t-baby Date: Mon, 19 Sep 2016 16:20:50 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0replace=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/cybermkd/kit/MongoKit.java | 4 ++++ src/main/java/com/cybermkd/kit/MongoQuery.java | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/com/cybermkd/kit/MongoKit.java b/src/main/java/com/cybermkd/kit/MongoKit.java index a3b12a5..11046d8 100644 --- a/src/main/java/com/cybermkd/kit/MongoKit.java +++ b/src/main/java/com/cybermkd/kit/MongoKit.java @@ -189,6 +189,10 @@ public long updateOne(String collectionName, Bson queue, Bson data) { return updateResult.getModifiedCount(); } + public long replaceOne(String collectionName, Bson queue, Document document) { + UpdateResult updateResult = getCollection(collectionName).replaceOne(queue, document); + return updateResult.getModifiedCount(); + } public long delete(String collectionName, Bson queue) { DeleteResult deleteResult = getCollection(collectionName).deleteMany(queue); diff --git a/src/main/java/com/cybermkd/kit/MongoQuery.java b/src/main/java/com/cybermkd/kit/MongoQuery.java index 0ca3adb..1ac1625 100644 --- a/src/main/java/com/cybermkd/kit/MongoQuery.java +++ b/src/main/java/com/cybermkd/kit/MongoQuery.java @@ -398,6 +398,11 @@ public long updateOne() { return MongoKit.INSTANCE.updateOne(collectionName, and(query), Updates.combine(data)); } + + public long replace(Object obj) { + return MongoKit.INSTANCE.replaceOne(collectionName, and(query), Document.parse(JSON.toJSONString(obj))); + } + public long delete() { return MongoKit.INSTANCE.delete(collectionName, and(query)); }