Background code implementation of user login function:
User mapper interface query method definition
/** * * @param userName * @param userPwd * @param roleName * Query user records * @return */ User queryUser(@Param("userName")String userName); <!-- Query users --> <select id="queryUser" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from t_user where user_name =#{userName} </select>
In addition, we should note that the theory of light is not enough. By the way, I'd like to present you ten sets of practical tutorials and interview question bank of the latest JAVA architecture project in 2020, which can be found under the transformation of seven bar umbrella bar and Zero clothing umbrella (Digital homophony), and also can communicate with the old architect
Implementation of service layer login method
@Resource private UserDao userDao; /** * User login * @param userName * @param userPwd * @param roleName * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public UserModel userLoginCheck(String userName,String userPwd){ /** * 1.Parameter validity verification * 2.Query user validity verification * 3.password verifiers * 4.Return user model information */ // Parameter verification checkParam(userName,userPwd); // Execute query User user=userDao.queryUser(userName); // User existence verification AssertUtil.isTrue(null==user, "The user does not exist!"); AssertUtil.isTrue(user.getIsValid()==0, "Logged out to user!"); //password verifiers AssertUtil.isTrue(!user.getUserPwd().equals(MD5Util.md5Method(userPwd)),"Incorrect password!"); // Building user information model UserModel userModel=buildUserModel(user); return userModel; } /** * Login parameter verification * @param userName * @param userPwd * @param roleName */ private void checkParam(String userName, String userPwd, Integer roleId) { AssertUtil.isTrue(StringUtil.isNullOrEmpty(userName), "User name is not empty!"); AssertUtil.isTrue(StringUtil.isNullOrEmpty(userPwd), "Password is not empty!"); } /** * Building user login data model * @param user * @return */ private UserModel buildUserModel(User user) { UserModel userModel=new UserModel(); userModel.setRoleName(user.getRoleName()); userModel.setUserName(user.getUserName()); userModel.setTrueName(user.getTrueName()); String userIdStr=UserIDBase64.encoderUserID(user.getId()); userModel.setUserIdStr(userIdStr); return userModel; }
UserController layer
package com.shsxt.crm.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.shsxt.base.BaseController; import com.shsxt.base.ResultInfo; import com.shsxt.base.exception.ParamException; import com.shsxt.crm.model.UserModel; import com.shsxt.crm.service.UserService; @Controller @RequestMapping("user") public class UserController extends BaseController { @Resource private UserService userService; @RequestMapping("userLogin") public @ResponseBody MessageModel userLogin(String userName, String userPwd){ MessageModel resultInfo=null; try { UserModel userModel= userService.userLoginCheck(userName, userPwd); resultInfo=success(); resultInfo.setResult(userModel); } catch (ParamsException e) { e.printStackTrace(); resultInfo=failed(e.getMessage()); } return resultInfo; } }
Foreground js control form submission
Bind submit button click event
// Submit binding event $(function(){ $("#btnLogin").click(function(){ var userName=$("#userName").val(); var userPwd=$("#userPwd").val(); if(isEmpty(userName)){ alert("User name is not empty!"); return; } if(isEmpty(userPwd)){ alert("Password is not empty!"); return; } var param={}; param.userName=userName; param.userPwd=userPwd; $.ajax({ type:"post", url:"user/userLogin", data:param, dataType:"json", success:function(data){ if(data.resultCode==200){ /** * Login successful * Write cookie */ $.cookie("userName",data.result.userName); $.cookie("trueName",data.result.trueName); $.cookie("userIdStr",data.result.userIdStr); // Perform jump window.location.href="main"; }else{ alert(data.msg); } } }) }) })
Deploy project, perform login operation
Login success cookie write success
Login succeeded, jump to background management page
Finally note: the theory of light is not enough. By the way, I'd like to present you ten sets of practical tutorials and interview question bank of the latest JAVA architecture project in 2020, which can be found under the transformation of seven bar umbrella bar and Zero clothing umbrella (Digital homophony), and also can communicate with the old architect
The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling