SpringBoot实现登录注册

话不多说,一个小小登录注册的案例向你展示SpringBoot的魅力

1 设计数据库

1.1表结构

SpringBoot实现登录注册_第1张图片

1.2属性

id自增主键,name非空约束,唯一约束,password

2 IDEA配置

2.1 pom文件


    org.springframework.boot
    spring-boot-starter-web
 

        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    
        com.alibaba
        druid
        1.1.14
    
    
        mysql
        mysql-connector-java
        5.1.39
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        1.2.0
    



    
        src/main/java
        
            **/*.xml
            
    

2.2 yml文件配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    url:jdbc:mysql://localhost:3306/user?characterEncoding=utf8&useUnicode=true
    username: root
    password: 1234

mvc:
  static-path-pattern: /static/**  #配置静态路径

mybatis:
  type-aliases-package: day2.demo.test.bean
  mapper-locations: classpath:day2/demo/test/mapper/*.xml

3 bean层

3.1 项目结构

SpringBoot实现登录注册_第2张图片

3.2 user实体类

package day2.demo.test.bean;

public class User {
    private int id;
    private String name;
    private String password;

    public  User(){

    }

    public User(int id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    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 getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

4 mapper层

4.1 usermapper

package day2.demo.test.mapper;

import day2.demo.test.bean.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {
    public User selectUserById(int id);
    public User selectUserByName(String name);
    public int insertUser(User user);
}

4.2 usermapper.xml




    
    
    
        insert into  u(name,password) values(#{name},#{password})
    

5 service层

package day2.demo.test.service;

import day2.demo.test.bean.User;
import day2.demo.test.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public User selectUserById(int id){
        return userMapper.selectUserById(id);
    }

    public boolean login(User user){
        String name = user.getName();
        String password = user.getPassword();
        User u1 =  userMapper.selectUserByName(name);
        if(u1==null){
            return false;
        }else{
            if(u1.getPassword().equals(password)){
                return true;
            }else{
                return false;
            }
        }
    }

    public boolean zhuCe(User user){
        int x = userMapper.insertUser(user);
        if(x>0){
            return true;
        }else {
            return false;
        }
    }
}

6 controller层

package day2.demo.test.controller;

import com.sun.deploy.net.HttpResponse;
import day2.demo.test.bean.User;
import day2.demo.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
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.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserController {
    @Autowired
    UserService userService;
 //-----------------------------登录注册功能
    //登录:
    @RequestMapping("/Login")
    public String login(){
        return "Login";
    }
    @GetMapping("/result")
    public String result(HttpServletRequest req, HttpServletResponse resp){

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User();
        user.setName(username);
        user.setPassword(password);

        boolean flag = userService.login(user);

        if(flag==true){
            return "Success";
        }else {
            return "Middle";
        }
    }
    @GetMapping("/middle")
    public String middle(){
        return "Login";
    }
    //注册:
    @GetMapping("/zhuCe")
    public String zhuCe(){
        return "ZhuCe";
    }
    @GetMapping("/end")
    public String end(HttpServletRequest req, HttpServletResponse resp){

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User();
        user.setName(username);
        user.setPassword(password);
        boolean flag = userService.zhuCe(user);
        if(flag==true){
            return "End1";
        }else{
            return "End2";
        }
    }
}

7 html页面

SpringBoot实现登录注册_第3张图片

7.1 login页面




    
    登录页面
   
    
    
    


用户名:
密码:

7.2 middle页面




    
    
    登录失败
    
    


    

登录失败,还有5秒跳转回登录界面

7.3 success页面




    
    Title


    

登录成功

7.4 zhuce页面




    
    注册页面

    
    
    


用户名:
密码:

7.5 end1页面




    
    注册成功
    
    


    

注册成功,还有5秒跳转回登录界面

7.6 end2页面




    
    注册失败
    
    


    

注册失败,还有5秒跳转回登录界面

大功告成,哈哈

 

 

 

你可能感兴趣的:(Spring,SpringBoot)