MyBatis-Plus增刪改查(有單表分頁,3表的請看我其他的文章)
阿新 • • 發佈:2018-11-29
package cn.com.befery.dataai.controller; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import cn.com.befery.dataai.constants.AIAppConstants; import cn.com.befery.dataai.interceptor.BaseInterceptor; import cn.com.befery.dataai.po.User; import cn.com.befery.dataai.service.UserService; import cn.com.befery.dataai.utils.AIAppUtils; import cn.com.befery.dataai.vo.ResponseData; import cn.com.befery.dataai.vo.ResultCode; import cn.com.befery.dataai.vo.SearchParam; @Controller @RequestMapping("/user") public class UserController extends BaseController { private final static Logger logger = LoggerFactory.getLogger(ModelController.class); @Autowired private UserService userService; @RequestMapping("") public String user() { return "user-list"; } /** * 前臺傳遞的URL:/users/usersPage * * @param param * @param request * @return */ @ResponseBody @GetMapping(value = "/usersPage") public ResponseData usersPage(SearchParam param,String userName, HttpServletRequest request) { logger.info("usersPage ..........."); String orderNO = request.getParameter("order[0][column]"); String order = request.getParameter("columns[" + orderNO + "][name]"); String orderdir = request.getParameter("order[0][dir]"); EntityWrapper<User> ew = new EntityWrapper<>(); ew.like("user_name", userName).orderBy(order, "asc".equals(orderdir) ? true : false); Page<User> pageDate = userService.selectPage(new Page<User>(param.getStart(), param.getLength()), ew); return responseData(param.getDraw(), pageDate); } /** * 密碼重置 * * @param userID * @return */ @ResponseBody @RequestMapping(value = "/resetSecret") public ResultCode userResetPassword(Long userID,HttpSession session) { logger.info("userResetPassword ..........."); User sessionUser = (User) session.getAttribute("sessionUser"); ResultCode res = new ResultCode(); if("admin".equals(sessionUser.getUserName())) { String newpassword = "111111"; EntityWrapper<User> ew = new EntityWrapper<>(); String setSql = "pass_word = " + "'" + AIAppUtils.encrypt(newpassword) + "'"; ew.eq("user_id",userID); boolean flag = userService.updateForSet(setSql, ew); if (flag) { res.setCode(0); res.setMsg("11111111"); } else { res.setCode(1); } }else { res.setCode(1); res.setMsg("沒有許可權"); } return res; } /** * 新增使用者(admin才能操作) * * @author zhangxuewei * @param user * @return */ @ResponseBody @RequestMapping(value = "/addUser", method = RequestMethod.POST) public ResultCode addUser(User user, HttpSession session) { logger.info("addUser ..........."); User sessionUser = (User) session.getAttribute("sessionUser"); ResultCode res = new ResultCode(); if (("admin").equals(sessionUser.getUserName())) { if (StringUtils.isNotBlank(user.getUserName()) && StringUtils.isNotBlank(user.getPassWord())) { String newName = user.getUserName(); EntityWrapper<User> ew = new EntityWrapper<>(); ew.eq("user_name", newName); // 新增之前先進行判斷 給使用者是否已經存在 不存在才可以新增 if (userService.selectOne(ew) == null) { user.setCreateDate(new Date()); String pwd = AIAppUtils.encrypt(user.getPassWord()); user.setPassWord(pwd); user.setStatus(AIAppConstants.STATUS); // 1 try { userService.insert(user); res.setCode(0); } catch (Exception e) { res.setCode(1); } } else { res.setCode(1); res.setMsg("使用者名稱重複"); } } else { res.setCode(1); res.setMsg("引數不能為空"); } } else { res.setCode(1); res.setMsg("沒有許可權(admin使用者可操作)"); } return res; } /** * 刪除使用者(admin才能操作) * * @RequestParam(value = "userID") * @author zhangxuewei * @param userID */ @ResponseBody @RequestMapping(value = "/deleteUser", method = RequestMethod.POST) public ResultCode deleteUser(Long userID, HttpSession session) { logger.info("deleteUser ..........."); User user1 = (User) session.getAttribute("sessionUser"); ResultCode res = new ResultCode(); if ("admin".equals(user1.getUserName())) { if (userID == null) { // 引數不能為空 res.setCode(1); } try { EntityWrapper<User> ew = new EntityWrapper<>(); ew.eq("user_id", userID); userService.delete(ew); res.setCode(0); } catch (Exception e) { // TODO: handle exception res.setCode(1); } } else { res.setCode(1); res.setMsg("沒有許可權(admin使用者可操作)"); } return res; } /** * 修改密碼(只能修改自己的密碼) * * @author zhangxuewei * @param userID * @param password * @param session * @return */ @ResponseBody @RequestMapping(value = "/updateUserPWD", method = RequestMethod.POST) public ResultCode updateUserPWD(@RequestParam(value = "password_old") String password, @RequestParam(value = "password_new") String newpassword, HttpSession session) { logger.info("updateUserPWD ..........."); User user1 = (User) session.getAttribute("sessionUser"); ResultCode res = new ResultCode(); EntityWrapper<User> ew = new EntityWrapper<>(); ew.eq("user_name", user1.getUserName()); User user2= userService.selectOne(ew); if(user2.getPassWord().equals(AIAppUtils.encrypt(password))) { if (StringUtils.isNotBlank(password) && StringUtils.isNotBlank(newpassword)) { String setSql = "pass_word = " + "'" + AIAppUtils.encrypt(newpassword) + "'"; EntityWrapper<User> ew1 = new EntityWrapper<>(); ew.eq("user_id", user1.getUserID()); try { userService.updateForSet(setSql, ew1); session.removeAttribute("sessionUser"); res.setCode(0); } catch (Exception e) { // TODO: handle exception res.setCode(1); } } else { // 引數不能為空 res.setCode(1); res.setMsg("引數不能為空"); } }else { res.setCode(1); res.setMsg("原始密碼不正確!"); } return res; } /** * 禁用使用者 * * @author zhangxuewei * @param userID * @param status * @param session * @return */ // 禁用 @ResponseBody @RequestMapping(value = "updateStatus", method = RequestMethod.POST) public ResultCode updateStatus(@RequestParam(value = "userID") Long userID, @RequestParam(value = "status") Long status, HttpSession session) { logger.info("updateStatus ..........."); ResultCode res = new ResultCode(); User user1 = (User) session.getAttribute("sessionUser"); if (user1.getUserName().equals("admin")) { if (userID != null && status != null) { if (status == 0 || status == 1) { String setSql = "status = " + status; EntityWrapper<User> ew = new EntityWrapper<>(); ew.eq("user_id", userID); try { userService.updateForSet(setSql, ew); res.setCode(0); } catch (Exception e) { // TODO: handle exception res.setCode(1); } } else { // status引數只能為 0(禁用) 1(啟用) res.setCode(1); res.setMsg("引數不正確"); } } else { // 引數不能為空 res.setCode(1); res.setMsg("引數不能為空"); } } else { res.setCode(1); res.setMsg("沒有許可權(admin使用者可操作)"); } return res; } }
這個是單表分頁 加工返回前臺資料 的 方法
package cn.com.befery.dataai.controller; import org.springframework.stereotype.Controller; import com.baomidou.mybatisplus.plugins.Page; import cn.com.befery.dataai.vo.ResponseData; @Controller public class BaseController { /** * * @param draw 重構次數 * @param page 分頁資料 * @return */ public ResponseData responseData(String draw,Page<?> page){ ResponseData res = new ResponseData(); res.setData(page.getRecords()); res.setDraw(draw); res.setRecordsFiltered((int)page.getTotal()); res.setRecordsTotal((int)page.getTotal()); return res; } }
這個是ResponseDate類(省去了get set 方法)
package cn.com.befery.dataai.vo; import java.util.List; //@JsonInclude(Include.NON_NULL) public class ResponseData { /** * */ // private static final long serialVersionUID = 1L; private String draw; private int recordsTotal; private int recordsFiltered; @SuppressWarnings("rawtypes") private List data; }