SpringBoot教学资料6-SpringBoot登录注册功能实现(带简单前端)

 项目样式:

SpringBoot教学资料6-SpringBoot登录注册功能实现(带简单前端)_第1张图片

SpringBoot教学资料6-SpringBoot登录注册功能实现(带简单前端)_第2张图片

SpringBoot教学资料6-SpringBoot登录注册功能实现(带简单前端)_第3张图片

SpringBoot教学资料6-SpringBoot登录注册功能实现(带简单前端)_第4张图片

SpringBoot教学资料6-SpringBoot登录注册功能实现(带简单前端)_第5张图片

SpringBoot教学资料6-SpringBoot登录注册功能实现(带简单前端)_第6张图片

 SQL:

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

项目结构:

SpringBoot教学资料6-SpringBoot登录注册功能实现(带简单前端)_第7张图片

pom.xml:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.13
         
    
    com.bai
    login
    0.0.1-SNAPSHOT
    login
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.3.1
        

        
            com.mysql
            mysql-connector-j
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter-test
            2.3.1
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


application.properties:

# 数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/companydb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

# mapper映射
mybatis.type-aliases-package=com.bai.login.pojo
mybatis.mapper-locations=classpath:mapper/*.xml

 login.html:




    
    login


用户名:
密码:

regist.html:




    
    Title


用户名:
密码:

User.java:

package com.bai.login.pojo;

public class User {
    private Integer id;
    private String username;
    private String password;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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


}

UserMapper.xml:





    
    
    
    
        insert into t_user(username,password) value (#{username},#{password})
    

UserMapper.java:

package com.bai.login.mapper;

import com.bai.login.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    List findAll();
    User findByName(String name);
    String findPswByName(String UserName);
    void save(User user);
}

UserService.java:

package com.bai.login.service;

import com.bai.login.mapper.UserMapper;
import com.bai.login.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public String login(User user){
        try {
            User userExistN=userMapper.findByName(user.getUsername());
            if (userExistN!=null){
                String userExistP=userMapper.findPswByName(user.getUsername());
                if (userExistP.equals(user.getPassword())){
                    return user.getUsername()+"用户登录成功,欢迎您!";
                }else {
                    return "登录失败,密码错误";
                }
            }else {
                return "登录失败,账户不存在";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public String regist(User user){
        try {
            User userExist=userMapper.findByName(user.getUsername());
            if (user.getUsername().equals("")){
                return "账户名不能为空";
            }else if (user.getPassword().equals("")){
                return "密码不能为空";
            }else if (userExist!=null) {
                return "账户已经存在";
            }else {
                userMapper.save(user);
                return "注册成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


    public List findAll(){
        List list=userMapper.findAll();
        return list;
    }



}

UserController.java:

package com.bai.login.controller;

import com.bai.login.pojo.User;
import com.bai.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user){
        return userService.login(user);
    }

    @RequestMapping("/regist")
    public String regist(User user){
        return userService.regist(user);
    }

    /**
     * 解决查询数据库中文出现乱码问题
     * @return
     */
    @RequestMapping(value = "/alluser",method = RequestMethod.GET,produces = "application/json;charset=UTF-8" )
    public List findAll(){
        return userService.findAll();
    }


}

LoginApplication.java:

package com.bai.login;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LoginApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoginApplication.class, args);
    }

}
 访问地址:http://localhost:8080/login.html

你可能感兴趣的:(spring,boot,前端,java,后端,spring)