Skip to content

Commit

Permalink
1.7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
T-baby authored Sep 26, 2016
2 parents 118fa83 + fbefdde commit a83a0a2
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 29 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.cybermkd</groupId>
<artifactId>MongodbPlugin</artifactId>
<version>1.0.7</version>
<version>1.0.7.1</version>
<packaging>jar</packaging>

<name>MongodbPlugin</name>
Expand Down Expand Up @@ -85,13 +85,14 @@
<scope>provided</scope>
</dependency>

<!--ICEREST-->
<dependency>
<groupId>com.cybermkd</groupId>
<artifactId>ICEREST</artifactId>
<version>1.0.1.3</version>
<scope>provided</scope>
</dependency>


<!--日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down
53 changes: 37 additions & 16 deletions src/main/java/com/cybermkd/kit/MongoKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -50,17 +50,13 @@ public MongoCollection<Document> getCollection(String collectionName) {
return defaultDb.getCollection(collectionName);
}

public long insert(String collectionName, List<Document> docs) {
long before = getCollection(collectionName).count();
getCollection(collectionName).insertMany(docs);
return getCollection(collectionName).count() - before;
public void insert(String collectionName, List<Document> docs) {
getCollection(collectionName).insertMany(uniding(docs));
}


public long insert(String collectionName, Document doc) {
long before = getCollection(collectionName).count();
getCollection(collectionName).insertOne(doc);
return getCollection(collectionName).count() - before;
public void insert(String collectionName, Document doc) {
getCollection(collectionName).insertOne(uniding(doc));
}

public List<JSONObject> aggregate(String collectionName, List<Bson> query, boolean allowDiskUse) {
Expand Down Expand Up @@ -106,7 +102,7 @@ public List<JSONObject> find(String collectionName, int limit, Bson sort, Bson p
}

public List<JSONObject> 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 <T> List<T> find(String collectionName, int limit, Bson sort, Bson projection, Class<T> clazz) {
Expand Down Expand Up @@ -143,7 +139,7 @@ public <T> T findOne(String collectionName, Bson query, Bson sort, String join,
, clazz);
}

public List<JSONObject> find(String collectionName, Bson query, Bson projection, Bson sort, int limit,
public List<JSONObject> find(String collectionName, Bson query, Bson sort, Bson projection, int limit,
int skip, String join) {

final List<JSONObject> list = new ArrayList<JSONObject>();
Expand All @@ -162,7 +158,7 @@ public void apply(Document document) {

}

public <T> List<T> find(String collectionName, Bson query, Bson projection, Bson sort, int limit, int skip,
public <T> List<T> find(String collectionName, Bson query, Bson sort, Bson projection, int limit, int skip,
String join, Class<T> clazz) {

final List list = new ArrayList();
Expand Down Expand Up @@ -193,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);
Expand Down Expand Up @@ -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<Document> uniding(List<Document> list) {
List<Document> newList = new ArrayList<Document>();
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) {
Expand Down Expand Up @@ -313,7 +334,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();
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/cybermkd/kit/MongoPaginate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
31 changes: 22 additions & 9 deletions src/main/java/com/cybermkd/kit/MongoQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}


Expand Down Expand Up @@ -390,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));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/cybermkd/plugin/MongoPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ public MongoPlugin writeConcern() {
return this;
}

/*在分布式下写到多个节点后才进行返回*/
public MongoPlugin writeSafe() {
this.options.writeConcern(WriteConcern.SAFE);
this.options.writeConcern(WriteConcern.MAJORITY);
return this;
}

Expand Down
4 changes: 4 additions & 0 deletions target/classes/ValidationMessages.properties
Original file line number Diff line number Diff line change
@@ -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 '}

1 comment on commit a83a0a2

@T-baby
Copy link
Owner

@T-baby T-baby commented on a83a0a2 Sep 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

版本号应当是1.0.7.1

Please sign in to comment.