发表于: 2017-11-01 18:49:54
1 803
【今日完成】
今天写了后台管理中关于公告的五个接口。
其实四个基本的增删改查都比较简单,和昨天写的文档接口差不多。
但是这里有个动态查询,根据标题和上下架状态来返回列表。
emmm,动态查询要稍微复杂一点:
首先,建一个工具类,就在web模块里面。
public static Map<String, Object> getNoticeListParam(String noticeTitle,Integer noticeStatus) {
Map<String, Object> params = new HashMap<String, Object>();
/*Set<String> tables = new HashSet();*/
log.info("从notice表中动态查询");
/*tables.add("notice");*/
if (DataUtils.isNotNullOrEmpty(noticeTitle)) {
params.put("notice_title", noticeTitle);
}
if (DataUtils.isNotNullOrEmpty(noticeStatus)) {
params.put("notice_status", noticeStatus);
}
params.put("@order", " create_at desc ");
params.put("@query", " id ");
params.put("@table", "notice");
/* String table = SQLUtil.convertTable(tables);*/
/* params.put("@table", table);*/
log.info("getProfessionTagsList sql is " + SQLUtil.convert2Sql(params, 0, 0));
return params;
}
然后就在里面写代码了。
写好Util类里面的方法之后,就可以在接口里面调用了:
/**
* 后台-公告列表
*@param
*@author Jeff
*@date 2017-11-1 11:04
**/
@RequestMapping(value = "/a/u/notice/list", method = RequestMethod.GET)
public String getMultiNoticeJson(HttpServletRequest request,
HttpServletResponse response, ModelMap model, String noticeTitle,Integer noticeStatus,Integer page,Integer size)
throws Exception {
log.info("公告动态查询");
//1 初始化参数
if (page == null) {
page = 1;
}
if (size == null) {
size = 10;
}
Integer start = (page - 1) * size;
if (start < 0) {
start = 0;
}
log.info(size);
log.info(start);
try {
// 2 通过参数拿条件
Map<String, Object> param = DynamicUtil.getNoticeListParam(noticeTitle,noticeStatus);
log.info("公告参数是:" + param);
//3 通过条件拿Ids
List<Long> ids = noticeService.getIdsByDynamicCondition(Notice.class, param,start,size);
log.info("the ids is " + ids);
// 4 通过Ids拿公告列表
List<Notice> noticesList = noticeService.getObjectsByIds(ids);
log.info("get notice data is " + noticesList.size());
model.addAttribute("code", 0);
model.addAttribute("page", page);
model.addAttribute("size", size);
model.addAttribute("noticesList", noticesList);
} catch (Throwable t) {
log.error(t.getMessage());
log.error("get notice error" );
model.addAttribute("code", -1);
}
return "notice/json/noticeListJson";
}
其实就是先拿参数,然后通过参数拿符合条件的IDs,最后通过IDs来返回一个数组,里面包含了所有符合条件的数据。
如图:拿到了所有上架的记录
【今日收获】
学会了怎么用动态查询,基本后台的文章与公告部分over
评论