实现SSM登陆注册功能 - 极悦
专注Java教育14年 全国咨询/投诉热线:444-1124-454
极悦LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 实现SSM登陆注册功能

实现SSM登陆注册功能

更新时间:2022-01-14 10:43:06 来源:极悦 浏览1414次

如何实现SSM登陆注册功能?小编来告诉大家具体步骤。

1.现在来写一个简单的登录注册功能

这几个包是自己手动创建的,然后往里面写代码

2.代码详情

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>

3.最好部署到Tomcat上运行成功

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>