这篇文章主要讲解了“如何实现SSM登陆注册功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何实现SSM登陆注册功能”吧!
这几个包是自己手动创建的,然后往里面写代码
package com.maike.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.maike.dto.UserDto; import com.maike.model.User; import com.maike.service.UserService; /** * * @author tan * */ @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; /** * toRegister: 跳转到注册页面<br/> * @return */ @RequestMapping("/toRegister") public String toRegister(){ return "register"; } /** * addUser:实现注册 <br/> * @param userDto * @return */ @RequestMapping("/addUser") public ModelAndView addUser(UserDto userDto) { int k = userService.addUser(userDto); if(k > 0) { ModelAndView view = new ModelAndView("login"); return view; }else { ModelAndView view = new ModelAndView("register"); return view; } } /** * 登录判断 * @param userDto * @return */ @RequestMapping("/judgeLogin") public ModelAndView judgeLogin(UserDto userDto) { int k = userService.judgeLogin(userDto); if(k > 0) { ModelAndView view = new ModelAndView("success"); return view; }else { ModelAndView view = new ModelAndView("login"); return view; } } }
package com.maike.dto; import java.io.Serializable; public class UserDto implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String userName; private String userPwd; private String reUserPwd; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getReUserPwd() { return reUserPwd; } public void setReUserPwd(String reUserPwd) { this.reUserPwd = reUserPwd; } public static long getSerialversionuid() { return serialVersionUID; } @Override public String toString() { return "UserDto [userName=" + userName + ", userPwd=" + userPwd + ", reUserPwd=" + reUserPwd + "]"; } }
UserMapper.xml
<!-- 通过用户名查询用户 --> <select id="selectByName" parameterType="java.lang.String" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> FROM user WHERE user_name=#{userName,jdbcType=VARCHAR} </select> <!-- 通过用户名密码查询 --> <select id="selectByNamePwd" parameterType="java.lang.String" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user where user_name=#{userName,jdbcType=VARCHAR} and user_pwd=#{userPwd,jdbcType=VARCHAR} </select> <!-- 添加用户信息 --> <insert id="insert" parameterType="com.maike.model.User"> INSERT INTO user(user_id, user_name, user_pwd) values(#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{userPwd,jdbcType=VARCHAR}) </insert>
UserMapper.java
int insert(User user); User selectByName(String userName); User selectByNamePwd(@Param("userName") String userName,@Param("userPwd") String userPwd);
package com.maike.service; import com.maike.dto.UserDto; public interface UserService { int addUser(UserDto userDto); int judgeLogin(UserDto userDto); }
package com.maike.serviceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.maike.dao.UserMapper; import com.maike.dto.UserDto; import com.maike.model.User; import com.maike.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; /** * addUser: 添加用户<br/> */ public int addUser(UserDto userDto) { // TODO Auto-generated method stub int k = 0; String userName = userDto.getUserName(); String userPwd = userDto.getUserPwd(); String reUserPwd = userDto.getReUserPwd(); if("" == userName || "" == userPwd|| "" == reUserPwd) { return k; } if(!userPwd.equals(reUserPwd)) { return k; } User u = userMapper.selectByName(userName); if(null != u) { return k; } User user = new User(); user.setUserName(userName); user.setUserPwd(reUserPwd); k = userMapper.insert(user); return k; } /** * 登录判断 */ public int judgeLogin(UserDto userDto) { // TODO Auto-generated method stub int k = 0; String userName = userDto.getUserName(); String userPwd = userDto.getUserPwd(); if("" == userName || "" == userPwd) { return 0; } User user = userMapper.selectByNamePwd(userName, userPwd); if(null != user) { k = 1; return k; } return 0; } }
JSP
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>注册页面</title> </head> <body> <br/> <br/> <br/> <form action="addUser"> <table align = "center" > <tr> <td>用户名</td><td><input type = "text" name = "userName"></td> </tr> <tr> <td>密码</td><td><input type = "password" name = "userPwd"></td> </tr> <tr> <td>确认密码</td><td><input type = "password" name = "reUserPwd"></td> </tr> <tr> <td colspan="2" align = "center"><input type = "submit" value = "注册" style = "background-color: Cyan;color:blue"></td> </tr> </table> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录页面</title> </head> <body> <br/> <br/> <br/> <form action="user/judgeLogin"> <table align = "center"> <tr> <td>用户名</td><td><input type = "text" name = "userName"></td> </tr> <tr> <td>密码</td><td><input type = "password" name = "userPwd"></td> </tr> <tr> <td align = "left"><input type = "submit" value = "登录" style = "background-color: Cyan;color:blue"></td> <td align = "right" ><button style = "background-color: Cyan;color:blue"><a href = "/SSM/user/toRegister">注册</a></button></td> </tr> </table> </form> </body> </html>
感谢各位的阅读,以上就是“如何实现SSM登陆注册功能”的内容了,经过本文的学习后,相信大家对如何实现SSM登陆注册功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。