idea+springboot+MySQL+jdk1.8实现简单的注册登录功能

1、先来一个项目整体结构
idea+springboot+MySQL+jdk1.8实现简单的注册登录功能_第1张图片2、pom


```java

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

3、搞一下user,这里简单,就搞了一个name和password

```java
package com.example.demo.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    @GeneratedValue
    private int id;
    private String username;
    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    public User(){

    }

    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;
    }
}


4、编辑dao层代码

package com.example.demo.dao;

import com.example.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface UserDao extends JpaRepository<User,Integer> {

    @Query(value = "select * from  user where username=:username and password=:password",nativeQuery = true)
    User findUserByUsernameAndPassword(String username,String password);
}

5、编辑service层代码
首先是UserService

package com.example.demo.service;

import com.example.demo.entity.User;

public interface UserService {

    User findUserByUsernameAndPassowrd(String username,String password);

    String addUser(User u);
}

然后UserServiceImp

package com.example.demo.service;

import com.example.demo.dao.UserDao;
import com.example.demo.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImp implements UserService{
    @Resource
    private UserDao ud;
    public User findUserByUsernameAndPassowrd(String username, String password) {
        return ud.findUserByUsernameAndPassword(username,password);
    }

    public String addUser(User u) {
        if(ud.save(u) != null){
            return "注册成功!";
        }
        else{
            return "注册失败!";
        }

    }
}

6、Controller

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserServiceImp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class UserController {

    @Resource
    private UserServiceImp usi;

    //以下是登录
    @RequestMapping(value = "/userLogin/submit", method = RequestMethod.POST)
    public String userLogin(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (usi.findUserByUsernameAndPassowrd(username, password) == null) {
            return "登陆失败!";
        }
        return "登陆成功!";
    }

    //以下是注册
    @RequestMapping(value = "/userRegister/submit", method = RequestMethod.POST)
    public String userRegister(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User u = new User(username, password);
        return usi.addUser(u);
    }

}

```java
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserNodeController {
    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }
    @RequestMapping(value = "/register")
    public String register(){
        return "register";
    }
    //去注册页面
    @GetMapping("/register")
    public String toRegister(){
        return "register";
    }
    //去登陆页面
    @GetMapping("/login")
    public String toLogin(){
        return "login";
    }

}


``
7、前端代码(略~)

8、效果图
idea+springboot+MySQL+jdk1.8实现简单的注册登录功能_第2张图片idea+springboot+MySQL+jdk1.8实现简单的注册登录功能_第3张图片
idea+springboot+MySQL+jdk1.8实现简单的注册登录功能_第4张图片idea+springboot+MySQL+jdk1.8实现简单的注册登录功能_第5张图片
idea+springboot+MySQL+jdk1.8实现简单的注册登录功能_第6张图片idea+springboot+MySQL+jdk1.8实现简单的注册登录功能_第7张图片9、控制器没有添加,小伙伴们自行解决哦!下面奉上源代码
想要源代码点击这里自行下载

你可能感兴趣的:(idea+springboot+MySQL+jdk1.8实现简单的注册登录功能)