package ${packageName};
|
|
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;
|
|
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 responseBody>
|
<#else>
|
import java.io.PrintWriter;
|
</#if>
|
@Controller
|
@RequestMapping("${mapping}")
|
public class ${controllerName} {
|
|
<#assign serviceObjName="${service.name?uncap_first}">
|
<#assign entityObjName="${entity.name?uncap_first}">
|
@Resource
|
private ${service.name} ${serviceObjName};
|
|
<#macro print data>
|
<#if responseBody>
|
<#if jsonp>
|
return JsonUtil.loadJSONP(callback,${data});
|
<#else>
|
return ${data};
|
</#if>
|
<#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>() {
|
|
@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();
|
|
data.put("list", gson.toJson(list));
|
data.put("count", count);
|
<@print data="JsonUtil.loadTrueResult(data)" > </@print>
|
}
|
|
<#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>
|
|
<#if add>
|
${responseBody?string("@ResponseBody","")}
|
@RequestMapping("add")
|
public ${responseBody?string("String","void")} add(${entity.name} bean, HttpSession session${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>
|
|
${responseBody?string("@ResponseBody","")}
|
@RequestMapping("get")
|
public ${responseBody?string("String","void")} get(${identifyIdType} id, HttpSession session${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")
|
public ${responseBody?string("String","void")} update(${entity.name} bean, HttpSession session${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>
|
|
|
}
|