SSM框架实现用户登录与注册功能


结构

SSM框架实现用户登录与注册功能_第1张图片

 SysUserController.java

package lx.controller;

import lx.entity.model.SysUser;
import lx.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    /**
     * 登录页面
     * @return
     */
    @GetMapping("/tologin")
    public String login() {
        System.out.println("nnn");
        return "/sysUser/login";
    }


    @PostMapping("/login")
    @ResponseBody
    public String login(SysUser sysUser, HttpServletRequest request){
        SysUser find = sysUserService.checkLogin(sysUser.getAccount(),sysUser.getPassword());
        if(find==null){
            return "登录验证失败!账号错、密码错或用户被禁用!";
        }
        //登录成功
        request.getSession().setAttribute("loginUser", find);
        return "ok";
    }
//    -----------增加用户
    @GetMapping("/add")
    public String add(Model model){
        SysUser entity=new SysUser();
        entity.setStatus(0);
        model.addAttribute("item",entity);
        return "/sysUser/register";
    }
    //新增-post
    @PostMapping("/add")
    @ResponseBody
    public String add(SysUser entity){
        System.out.println("234");
        try {
            sysUserService.insert(entity);
            return "ok";
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}
SysUser.java实体类
package lx.entity.model;
//用户实体类
public class SysUser {
    private int id;//主键
    private String name;//用户名
    private String account;//登录账号
    private String password;//登录密码
    private int status;//状态 0正常,1禁用

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}
SysUserMapper.java
package lx.mapper;

import lx.entity.model.SysUser;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Repository;

@MapperScan
@Repository
public interface SysUserMapper {

    //登录验证
    SysUser checkLogin(@Param("account")String account, @Param("password")String password);

    int insert(SysUser record);
}
SysUserServiceImpl.java
package lx.service.impl;

import lx.entity.model.SysUser;
import lx.mapper.SysUserMapper;
import lx.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {
@Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public SysUser checkLogin(String account, String password) {
        System.out.println(account+password+"lllx");
        return sysUserMapper.checkLogin(account,password);
    }

    @Override
    public int insert(SysUser record) {
        System.out.println(record+"1111");
        return sysUserMapper.insert(record);
    }
}
SysUserService.java
package lx.service;

import lx.entity.model.SysUser;

public interface SysUserService {
//    登录
    SysUser checkLogin(String account, String password);
//    增加用户
   int insert(SysUser record);
}

applicationContext.xml



    
    
    




    
    
        
        
        
    
    
    
        
            
                classpath:jdbc.properties
            
        
    
    
        
        
        
        
        
        
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
        
        
        
    
    
    
        
    
    
    
        
        
    

SysUserMapper.xml



    
        
        
        
        
        
    


    insert into sys_user (id, name, account, password, status)
    values (#{id}, #{name}, #{account}, #{password}, #{status})

jdbc.properties

driver=com.mysql.cj.jdbc.Driver
#mytest\u662F\u672C\u5730\u6570\u636E\u5E93\u540D
url=jdbc:mysql://localhost:3306/smm?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
#\u4E0B\u9762\u8F93\u5165\u81EA\u5DF1\u6570\u636E\u5E93\u7684\u8D26\u53F7\u548C\u5BC6\u7801
username=root
password=502216
#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
initialSize=0
#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
maxActive=20
#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F4
maxIdle=20
#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F4
minIdle=1
#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
maxWait=60000

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: lx
  Date: 2022/4/18
  Time: 13:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    用户登录
    <%--    --%>
    
    <%--    --%>
    <%--    --%>
    


用户登录

登录账号:
登录密码:
没有账号?注册账号

register.jsp

<%--
  Created by IntelliJ IDEA.
  User: lx
  Date: 2022/4/18
  Time: 19:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>



    
    新增
<%--    --%>
    
<%--    --%>
    


用户信息
用户名
登录账号
登录密码

views/home/index.jsp里面就是登录成功的内容


总结

记录一下

你可能感兴趣的:(intellij-idea,java,spring)