ys.artifact-orm.1.0.1.source-code.dao-mongodb.btl Maven / Gradle / Ivy
<%
var packages = [];
array.addOnly(packages,"org.artifact.core.db.MongoDBDao");
array.addOnly(packages,"org.bson.Document");
array.addOnly(packages,"com.mongodb.client.MongoCollection");
array.addOnly(packages,"com.mongodb.client.model.IndexOptions");
array.addOnly(packages,"com.mongodb.client.model.Filters");
array.addOnly(packages,"cn.hutool.core.util.ObjectUtil");
array.addOnly(packages,"org.bson.Document");
array.addOnly(packages,"java.util.ArrayList");
array.addOnly(packages,"java.util.List");
array.addOnly(packages,"com.mongodb.client.MongoCursor");
array.addOnly(packages,templates['BeanTemplate'].package + "." + table.className);
%>
<%for(package in packages){%>
import ${package};
<%}%>
import static cn.hutool.core.util.ObjectUtil.equal;
public abstract class ${table.className+templates['DaoTemplate'].classSuffix} extends MongoDBDao<${table.className}>{
<%include("dao-common.btl"){}%>
@Override
public boolean createTable(){
<%for(index in table.indexs){
var indexColumns = "new Document()";
for(column in index.columns){
indexColumns = indexColumns+'.append("'+ column.fieldName +'", 1)';
}
%>
getCollection().createIndex(${indexColumns},new IndexOptions().unique(${index.unique}));
<%}%>
return true;
}
<%
for(index in table.indexs){
var methodSuffix = "";
var parameter = "";
var filters = "";
var equals = "";
for(column in index.columns){
var ColumnName = uf(column.fieldName);
var columnName = lf(column.fieldName);
methodSuffix = methodSuffix + ColumnName;
parameter = parameter + column.type + " " + columnName;
filters = filters + 'eq("'+columnName+'", '+columnName+")";
equals = equals + 'equal(t.get'+ColumnName+'(), '+columnName+')';
if(!columnLP.last){
parameter = parameter + " ,";
filters = filters + ".";
equals = equals + " && ";
}
}
%>
<%if(index.unique){%>
public ${table.className} findBy${methodSuffix}(${parameter}){
return resultFirst(()->{
Document document = getCollection().find(Filters.${filters}).first();
if (document!=null) {
return createEntity().read(document);
}
return null;
}, t -> {
return ${equals};
}) ;
}
<%}else{%>
public List<${table.className}> findBy${methodSuffix}(${parameter}){
return resultList(()->{
List<${table.className}> list = new ArrayList<>();
MongoCursor mongoCursor = getCollection().find(Filters.${filters}).iterator();
while (mongoCursor.hasNext()) {
list.add(createEntity().read(mongoCursor.next()));
}
return list;
}, t -> {
return ${equals};
}) ;
}
<%}%>
<%}%>
}