spring boot+jpa+mysql实现简单的登录注册

Posted 尚由

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot+jpa+mysql实现简单的登录注册相关的知识,希望对你有一定的参考价值。

最近在学习JAVA的后端,感觉好多东西换汤不换药,和python的是一样的,只是java的环境配置起来要麻烦得多,个人喜好学习东西先看官方文档,如果没有官方文档再再找其他人的博客

纯小白入手踩了许多坑,改天细细盘点一下

这里简单实现了登录注册功能

项目结构

 

 

 

1使用maven管理依赖pom.mxl

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>pers.ly</groupId>
    <artifactId>community</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>community</name>
    <description>web api project for Spring Boot</description>

    <properties>
        <java.version>14</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.7.RELEASE</version>

            </plugin>
        </plugins>

    </build>

</project>

2实体User.java

package pers.ly.entity;


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

@Entity // This tells Hibernate to make a table out of this class
public class User {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer id;

    private String username;

    private String password;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

3JPA的dao层 UsersRespository.java

package pers.ly.dao;

import org.springframework.data.repository.CrudRepository;
import pers.ly.entity.User;

import java.util.List;


// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

public interface UserRepository extends CrudRepository<User, Integer> {


    User findByUsername(String username);
}

4控制层用来实现输出功能UserController.java

package pers.ly.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pers.ly.entity.User;
import pers.ly.dao.UserRepository;
import pers.ly.jsonify.jsondata;

@Controller // This means that this class is a Controller
@RequestMapping(path="/community") // This means URL\'s start with /demo (after Application path)
public class UserController {
    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;
    String username,password;
    jsondata data =new jsondata();
    @PostMapping(path = "/register") // Map ONLY POST Requests
    public @ResponseBody
    jsondata register( @RequestBody(required = false) User user)
    {
        data.setMsg("Saved");
        username=user.getName();
        password=user.getPassword();
        User n = new User();
        n.setUsername(username);
        n.setPassword(password);
        userRepository.save(n);
        return data;
    }

    @GetMapping(path = "/login")
    public @ResponseBody
    jsondata login(@RequestBody(required = false) User user) {
        username=user.getName();
        password=user.getPassword();
        User n=userRepository.findByUsername(username);
        if(n==null)
            data.setMsg("user is null");
        else if(!n.getPassword().equals(password))
            data.setMsg("password is wrong");
        else
            data.setMsg("success");
        return data;
    }
}

5为了输出为json信息创建的json类jsondata.java

package pers.ly.jsonify;

public class jsondata {
    private String msg;

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }
}

 

以上是关于spring boot+jpa+mysql实现简单的登录注册的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot访问mysql(JPA方式)最简单配置

spring-boot-jap-layui-mysql 完整的jpa多对一

spring-boot-jap-layui-mysql 完整的jpa多对一

Spring Boot 2.x基础教程:使用Spring Data JPA访问MySQL

Spring Boot+MySQL+Spring Data JPA一个Web的Demo

Spring Boot:在Spring Boot中使用Mysql和JPA