发表于: 2018-01-10 21:58:04
1 679
今天完成的事情
写了用户列表和用户详情部分
采用sql语句拼接的方式,编写查询
public static Map<String, Object> getUserIdsByDynamicCondition(
String nickname, String mobile, String email, String area, Integer sex, Integer grade,Integer status,
Integer minBean, Integer maxBean)
{
Map<String, Object> params = new HashMap<>();
params.put("@query", "id");
params.put("@table", "user");
if (nickname != null) {
// params.put("nickname", nickname);
params.put("2", "2 and nickname like '%" + nickname + "%'");
}
if (status != null) {
params.put("status", status);
}
if (mobile != null) {
params.put("2", "2 and mobile like '%" + mobile + "%'");
}
if (email != null) {
params.put("2", "2 and email like '%" + email + "%'");
}
if (minBean != null) {
params.put("bean & >", minBean);
}
if (maxBean != null) {
params.put("bean & <", maxBean);
}
if (area != null) {
// params.put("area", area);
params.put("2", "2 and area like '%" + area + "%'");
}
if (sex != null) {
params.put("sex", sex);
}
if (grade != null) {
params.put("grade", grade);
}
return params;
}
用户列表:
@RequestMapping(value = "/a/u/user/list", method = RequestMethod.GET)
public String getMultiUserJson(String nickname, String mobile, String email, String area, Integer sex, Integer grade,
Integer status, Integer minBean, Integer maxBean,
HttpServletRequest request,
HttpServletResponse response, ModelMap model,Integer page)
throws Exception
{
Map<String, Object> map = SQLUtil.getUserIdsByDynamicCondition(nickname,mobile,email,area,sex,grade,
status, minBean, maxBean);
List<Long> total = userService.getIdsByDynamicCondition(User.class, map, 0, Integer.MAX_VALUE);
if (total.size() == 0) {
model.addAttribute("code", -3003);
return "/user/json/userListJson";
}
if (page == null ) {
page = 1;
}
//确定页数
page = page - 1;
//定区间
int begin = page * 10;
int end = page * 10 + 10;
//定页数,
int size = total.size();
begin = begin <= size ? begin : (size / 10) * 10;
end = end <= size ? end : size;
//子集,根据页数,获取,分页
List<Long> subIds0 = total.subList(begin,end);
//直接用subIds0,会报错,
List<Long> subIds = new ArrayList<>(subIds0);
try {
List<User> userList = userService.getObjectsByIds(subIds);
log.info("get user data is " + userList);
model.addAttribute("code", 0);
model.addAttribute("total", total.size());
model.addAttribute("userList", userList);
} catch (Throwable t) {
log.error(t.getMessage());
log.error("get user error,id is " + subIds);
model.addAttribute("code", -100000);
}
return "/user/json/userListJson";
}
用户详情:
@RequestMapping(value = "/a/u/user/{id}", method = RequestMethod.GET)
public String getUserJson(HttpServletRequest request,
HttpServletResponse response, ModelMap model, @PathVariable Long id)
throws Exception
{
log.info("get data : id= " + id);
try {
User user = userService.getObjectById(id);
log.info("get user data is " + user);
model.addAttribute("code", 0);
model.addAttribute("user", user);
} catch (Throwable t) {
t.printStackTrace();
log.error(t.getMessage());
log.error("get user error,id is " + id);
model.addAttribute("code", -100000);
}
return "/user/json/userDetailJson";
}
明天的计划
写后台登陆部分
遇到的问题
微信图片上传还有点问题
收获
sql拼接
评论