| | |
| | | package ${packageName}; |
| | | |
| | | import org.springframework.data.mongodb.core.query.Criteria; |
| | | import org.springframework.data.mongodb.core.query.Query; |
| | | import org.springframework.data.mongodb.core.query.Update; |
| | | import org.springframework.data.domain.Sort; |
| | | import org.springframework.stereotype.Repository; |
| | | import com.google.gson.*; |
| | | import net.sf.json.JSONArray; |
| | | import net.sf.json.JSONObject; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.yeshi.utils.JsonUtil; |
| | | import org.yeshi.utils.TimeUtil; |
| | | import com.google.gson.reflect.TypeToken; |
| | | |
| | | <#list importClasses as value> |
| | | import ${value}; |
| | | </#list> |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.lang.reflect.Type; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import ${entity.clazz}; |
| | | import ${service.clazz}; |
| | | import ${query.clazz}; |
| | | <#if adminVO??> |
| | | import ${adminVO.clazz}; |
| | | </#if> |
| | | |
| | | <#if responseBody> |
| | | <#else> |
| | | import java.io.PrintWriter; |
| | | </#if> |
| | | @Controller |
| | | @RequestMapping("${mapping}") |
| | | public class ${controllerName} { |
| | | |
| | | @Repository |
| | | public class ${daoName} extends ${baseDaoClassName}<${entityClassName}>{ |
| | | <#assign serviceObjName="${service.name?uncap_first}"> |
| | | <#assign entityObjName="${entity.name?uncap_first}"> |
| | | @Resource |
| | | private ${service.name} ${serviceObjName}; |
| | | |
| | | public void updateSelective(${entityClassName} bean) { |
| | | Query query = new Query(); |
| | | Update update=new Update(); |
| | | query.addCriteria(Criteria.where("${identityColumn.columnName}").is(bean.${identityColumn.columnMehtod})); |
| | | <#if columnDataList??> |
| | | <#list columnDataList as value> |
| | | if(bean.${value.columnMehtod} != null) { |
| | | update.set("${value.columnName}", bean.${value.columnMehtod}); |
| | | } |
| | | </#list> |
| | | <#macro print data> |
| | | <#if responseBody> |
| | | <#if jsonp> |
| | | return JsonUtil.loadJSONP(callback,${data}); |
| | | <#else> |
| | | return ${data}; |
| | | </#if> |
| | | update.set("updateTime", new Date()); |
| | | update(query, update); |
| | | } |
| | | <#else> |
| | | <#if jsonp> |
| | | out.print(JsonUtil.loadJSONP(callback,${data})); |
| | | return; |
| | | <#else> |
| | | out.print(${data}); |
| | | return; |
| | | </#if> |
| | | </#if> |
| | | </#macro> |
| | | |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("list") |
| | | public ${responseBody?string("String","void")} list(${query.name} query, int page, int limit${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")} ) { |
| | | List<${entity.name}> list = ${serviceObjName}.list(query,page,limit); |
| | | long count = ${serviceObjName}.count(query); |
| | | JSONObject data = new JSONObject(); |
| | | Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() { |
| | | |
| | | public List<${entityClassName}> list(DaoQuery daoQuery){ |
| | | Query query=getQuery(daoQuery); |
| | | if(daoQuery.sortList!=null&&daoQuery.sortList.size()>0){ |
| | | query.with(new Sort(daoQuery.sortList)); |
| | | } |
| | | query.skip(daoQuery.start); |
| | | query.limit(daoQuery.count); |
| | | return findList(query); |
| | | } |
| | | @Override |
| | | public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) { |
| | | return date == null ? new JsonPrimitive("") : new JsonPrimitive(TimeUtil.getGernalTime(date.getTime(), "yyyy.MM.dd HH:mm")); |
| | | } |
| | | }).create(); |
| | | |
| | | public long count(DaoQuery daoQuery){ |
| | | Query query=getQuery(daoQuery); |
| | | return count(query); |
| | | } |
| | | |
| | | private Query getQuery(DaoQuery daoQuery){ |
| | | List<Criteria> andList=new ArrayList<>(); |
| | | <#list daoQueryColumnDataList as value> |
| | | <#if (value.type!"") == "Date"> |
| | | if(daoQuery.min${value.columnName?cap_first}!=null){ |
| | | andList.add(Criteria.where("${value.columnName}").gte(daoQuery.min${value.columnName?cap_first})); |
| | | } |
| | | if(daoQuery.max${value.columnName?cap_first}!=null){ |
| | | andList.add(Criteria.where("${value.columnName}").lt(daoQuery.max${value.columnName?cap_first})); |
| | | } |
| | | <#else> |
| | | if(daoQuery.${value.columnName}!=null){ |
| | | <#if (value.queryType!"") == "equal"> |
| | | andList.add(Criteria.where("${value.columnName}").is(daoQuery.${value.columnName})); |
| | | <#else> |
| | | andList.add(Criteria.where("${value.columnName}").regex(daoQuery.${value.columnName})); |
| | | </#if> |
| | | } |
| | | </#if> |
| | | </#list> |
| | | Query query=new Query(); |
| | | Criteria[] ands=new Criteria[andList.size()]; |
| | | andList.toArray(ands); |
| | | if(ands.length>0){ |
| | | query.addCriteria(new Criteria().andOperator(ands)); |
| | | } |
| | | return query; |
| | | data.put("list", gson.toJson(list)); |
| | | data.put("count", count); |
| | | <@print data="JsonUtil.loadTrueResult(data)" > </@print> |
| | | } |
| | | |
| | | public static class DaoQuery{ |
| | | <#if daoQueryColumnDataList??> |
| | | <#list daoQueryColumnDataList as value> |
| | | <#if (value.type!"") == "Date"> |
| | | public ${value.type} min${value.columnName?cap_first}; |
| | | public ${value.type} max${value.columnName?cap_first}; |
| | | <#else> |
| | | public ${value.type} ${value.columnName}; |
| | | </#if> |
| | | </#list> |
| | | <#if delete> |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("delete") |
| | | public ${responseBody?string("String","void")} delete(String ids${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | Type type = new TypeToken<List<${identifyIdType}>>(){}.getType(); |
| | | List<${identifyIdType}> idList=new Gson().fromJson(ids,type); |
| | | ${serviceObjName}.delete(idList); |
| | | <@print data="JsonUtil.loadTrueResult(\"\")" > </@print> |
| | | } |
| | | </#if> |
| | | public int start; |
| | | public int count; |
| | | public List<Sort.Order> sortList; |
| | | } |
| | | |
| | | <#if add> |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("add") |
| | | |
| | | <#if adminVO??> |
| | | public ${responseBody?string("String","void")} add(${adminVO.name} vo,AcceptAdminData acceptAdminData${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | try{ |
| | | ${serviceObjName}.add(vo.toEntity(acceptAdminData.getSystem())); |
| | | <@print data="JsonUtil.loadTrueResult(\"\")" > </@print> |
| | | }catch(Exception e){ |
| | | <@print data="JsonUtil.loadFalseResult(e.getMessage())" > </@print> |
| | | } |
| | | } |
| | | <#else> |
| | | public ${responseBody?string("String","void")} add(${entity.name} bean, AcceptAdminData acceptAdminData${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | try{ |
| | | ${serviceObjName}.add(bean); |
| | | <@print data="JsonUtil.loadTrueResult(\"\")" > </@print> |
| | | }catch(Exception e){ |
| | | <@print data="JsonUtil.loadFalseResult(e.getMessage())" > </@print> |
| | | } |
| | | } |
| | | </#if> |
| | | </#if> |
| | | |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("get") |
| | | public ${responseBody?string("String","void")} get(${identifyIdType} id, AcceptAdminData acceptAdminData${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | ${entity.name} entity = ${serviceObjName}.get(id); |
| | | if (entity !=null){ |
| | | <@print data="JsonUtil.loadTrueResult(entity)" > </@print> |
| | | } else { |
| | | <@print data="JsonUtil.loadFalseResult(\"ID不存在\")" > </@print> |
| | | } |
| | | } |
| | | |
| | | |
| | | <#if update> |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("update") |
| | | <#if adminVO??> |
| | | |
| | | public ${responseBody?string("String","void")} update( ${adminVO.name} vo ,AcceptAdminData acceptAdminData${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | if (vo.getId() == null) { |
| | | <@print data="JsonUtil.loadFalseResult(\"ID不能为空\")" > </@print> |
| | | } |
| | | try{ |
| | | ${serviceObjName}.update(vo.toEntity(null)); |
| | | }catch(Exception e){ |
| | | <@print data="JsonUtil.loadFalseResult(e.getMessage())" > </@print> |
| | | } |
| | | <@print data="JsonUtil.loadTrueResult(\"\")" > </@print> |
| | | } |
| | | |
| | | <#else> |
| | | |
| | | public ${responseBody?string("String","void")} update( ${entity.name} bean , AcceptAdminData acceptAdminData${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | if (bean.getId() == null) { |
| | | <@print data="JsonUtil.loadFalseResult(\"ID不能为空\")" > </@print> |
| | | } |
| | | try{ |
| | | ${serviceObjName}.update(bean); |
| | | }catch(Exception e){ |
| | | <@print data="JsonUtil.loadFalseResult(e.getMessage())" > </@print> |
| | | } |
| | | <@print data="JsonUtil.loadTrueResult(\"\")" > </@print> |
| | | } |
| | | </#if> |
| | | </#if> |
| | | |
| | | |
| | | } |