160 lines
4.9 KiB
Plaintext
160 lines
4.9 KiB
Plaintext
package ${package.Controller};
|
|
|
|
import com.hx.slj.common.json.AjaxJson;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import org.springframework.ui.Model;
|
|
import java.util.UUID;
|
|
#if(${restControllerStyle})
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
#else
|
|
import org.springframework.stereotype.Controller;
|
|
#end
|
|
#if(${superControllerClassPackage})
|
|
import ${superControllerClassPackage};
|
|
#end
|
|
|
|
/**
|
|
* <p>
|
|
* $!{table.comment} 前端控制器
|
|
* </p>
|
|
*
|
|
* @author ${author}
|
|
* @since ${date}
|
|
*/
|
|
#if(${restControllerStyle})
|
|
@RestController
|
|
#else
|
|
@Controller
|
|
#end
|
|
#set($entityPath = ${cfg.tool.toLine(${table.entityPath}, "/")})
|
|
@RequestMapping("${adminPath}/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${entityPath}#end")
|
|
#if(${kotlin})
|
|
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
|
|
|
|
#else
|
|
#if(${superControllerClass})
|
|
public class ${table.controllerName} extends ${superControllerClass} {
|
|
#else
|
|
public class ${table.controllerName} {
|
|
#end
|
|
## 获取mapper名称并将首字母小写
|
|
#set($mapperName = ${cfg.tool.lowerFirst(${table.mapperName})})
|
|
#set($entityName = ${cfg.tool.lowerFirst(${table.entityName})})
|
|
|
|
|
|
@Autowired
|
|
private ${table.mapperName} ${mapperName};
|
|
|
|
## 视图
|
|
@RequestMapping(value = "/list", method = RequestMethod.GET)
|
|
public String list() {
|
|
return "${cfg.model}/${entityPath}/list";
|
|
}
|
|
|
|
## 数据
|
|
@RequestMapping(value = "/list", method = RequestMethod.POST)
|
|
@ResponseBody
|
|
public AjaxJson list(@PathVariable(required = false) ${table.entityName} ${entityName}) throws NoSuchFieldException {
|
|
## 如果是逻辑删除
|
|
#if(${logicDeleteFieldName})
|
|
Cnd cnd = Cnd.where(${table.entityName}.${logicDeleteFieldName.toUpperCase()}, "=", ${table.entityName}.DEL_FLAG_NORMAL);
|
|
#else
|
|
Cnd cnd = Cnd.NEW();
|
|
#end
|
|
Pager pager = PageHelpers.setPageDao(${table.entityName}.class, dao, cnd);
|
|
List<${table.entityName}> ${entityName}List = dao.query(${table.entityName}.class, cnd, pager);
|
|
AjaxJson ajaxJson = AjaxJson.SUCCESS();
|
|
ajaxJson.put("code", 0);
|
|
ajaxJson.put("count", pager.getRecordCount());
|
|
ajaxJson.put("data", ${entityName}List);
|
|
return ajaxJson;
|
|
}
|
|
|
|
/**
|
|
* 增加/修改页面
|
|
*
|
|
* @param id 主键
|
|
* @return
|
|
*/
|
|
@RequestMapping(value = {"/add", "/edit"}, method = RequestMethod.GET)
|
|
public String edit(@RequestParam(required = false) String id, Model model) {
|
|
${table.entityName} ${entityName} = new ${table.entityName}();
|
|
// 如果id是空,则跳转修改页面
|
|
if (Strings.isNullOrEmpty(id)) {
|
|
model.addAttribute("url", "add");
|
|
} else {
|
|
model.addAttribute("url", "edit");
|
|
## 循环找到主键的字段,如果没有,则不进行查询
|
|
#foreach($field in ${table.fields})
|
|
#if(${field.keyFlag})
|
|
|
|
Cnd cnd = Cnd.where(${table.entityName}.${field.name.toUpperCase()}, "=", id);
|
|
${entityName} = dao.fetch(${table.entityName}.class, cnd);
|
|
#end
|
|
#end
|
|
}
|
|
model.addAttribute("${entityName}", ${entityName});
|
|
return "${cfg.model}/${entityPath}/form";
|
|
}
|
|
|
|
/**
|
|
* 添加
|
|
*
|
|
* @param ${entityName}
|
|
* @return
|
|
*/
|
|
@RequestMapping(value = "/add", method = RequestMethod.POST)
|
|
@ResponseBody
|
|
public AjaxJson add(${table.entityName} ${entityName}) {
|
|
#foreach($field in ${table.fields})
|
|
#if(${field.keyFlag})
|
|
${entityName}.set${field.capitalName}(UUID.randomUUID().toString());
|
|
#end
|
|
#end
|
|
dao.insert(${entityName});
|
|
return AjaxJson.SUCCESS();
|
|
}
|
|
|
|
/**
|
|
* 保存修改
|
|
*
|
|
* @param ${entityName}
|
|
* @return
|
|
*/
|
|
@RequestMapping(value = "/edit", method = RequestMethod.POST)
|
|
@ResponseBody
|
|
public AjaxJson edit(${table.entityName} ${entityName}) {
|
|
dao.updateIgnoreNull(${entityName});
|
|
return AjaxJson.SUCCESS();
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @param ${entityName}
|
|
* @return
|
|
*/
|
|
@RequestMapping(value = "/delete", method = RequestMethod.POST)
|
|
@ResponseBody
|
|
public AjaxJson delete(${table.entityName} ${entityName}) {
|
|
## 如果不是逻辑删除
|
|
#if(!${logicDeleteFieldName})
|
|
#foreach($field in ${table.fields})
|
|
#if(${field.keyFlag})
|
|
dao.delete(${table.entityName}.class, ${entityName}.get${field.capitalName}());
|
|
#end
|
|
#end
|
|
#else
|
|
${entityName}.set${cfg.tool.underscoreToCamelCase(${logicDeleteFieldName}, '_', 0)}("1");
|
|
dao.updateIgnoreNull(${entityName});
|
|
#end
|
|
return AjaxJson.SUCCESS();
|
|
}
|
|
|
|
}
|
|
|
|
#end
|