| | |
| | | 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; |
| | | |
| | | <#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}; |
| | | |
| | | @Controller |
| | | @RequestMapping("${mapping}") |
| | | public class AdminAppController { |
| | | |
| | | @Repository |
| | | public class ${daoName} extends ${baseDaoClassName}<${entityClassName}>{ |
| | | <#assign serviceObjName=${service.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> |
| | | </#if> |
| | | update.set("updateTime", new Date()); |
| | | update(query, update); |
| | | } |
| | | @ResponseBody |
| | | @RequestMapping("list") |
| | | public String list(${query.name} query, int page, int limit) { |
| | | List<${entityName}> 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>() { |
| | | |
| | | @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 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); |
| | | } |
| | | |
| | | 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); |
| | | return JsonUtil.loadTrueResult(data); |
| | | } |
| | | |
| | | 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> |
| | | public int start; |
| | | public int count; |
| | | public List<Sort.Order> sortList; |
| | | } |
| | | @ResponseBody |
| | | @RequestMapping("delete") |
| | | public String delete(String ids) { |
| | | JSONArray array = JSONArray.fromObject(ids); |
| | | List<String> idList=new ArrayList<>(); |
| | | for (int i = 0; i < array.size(); i++) { |
| | | idList.add(array.optString(i)); |
| | | } |
| | | ${serviceObjName}.delete(idList); |
| | | return JsonUtil.loadTrueResult(""); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("add") |
| | | public String add(${entity.name} bean, HttpSession session) { |
| | | try{ |
| | | ${serviceObjName}.add(bean); |
| | | return JsonUtil.loadTrueResult(""); |
| | | }catch(Exception e){ |
| | | return JsonUtil.loadFalseResult(e.getMessage); |
| | | } |
| | | } |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("get") |
| | | public String add(String id, HttpSession session) { |
| | | try{ |
| | | ${serviceObjName}.get(id); |
| | | return JsonUtil.loadTrueResult(""); |
| | | }catch(Exception e){ |
| | | return JsonUtil.loadFalseResult(e.getMessage); |
| | | } |
| | | } |
| | | |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("update") |
| | | public String update(${entity.name} bean, HttpSession session) { |
| | | if (app.getId() == null) { |
| | | return JsonUtil.loadFalseResult("ID不能为空"); |
| | | } |
| | | try{ |
| | | ${serviceObjName}.updateSelective(bean); |
| | | }catch(Exception e){ |
| | | return JsonUtil.loadFalseResult(e.getMessage); |
| | | } |
| | | return JsonUtil.loadTrueResult(""); |
| | | } |
| | | |
| | | |
| | | } |