SpringBoot實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的項(xiàng)目實(shí)戰(zhàn)
第一步:建立簡(jiǎn)單的項(xiàng)目



第二步:建一個(gè)簡(jiǎn)單的數(shù)據(jù)表

第三步:配置文件如下:
pom.xml文件配置:
<?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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wei</groupId>
<artifactId>demo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo01</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>application.properties文件配置:
#改變端口號(hào): server.port=8080 #給整個(gè)項(xiàng)目加個(gè)前綴,多個(gè)項(xiàng)目就不會(huì)重復(fù): #server.servlet.context-path=/first #school.grade=3 #school.classNum=4 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.password=root spring.datasource.username=root spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.thymeleaf.prefix=classpath:/templates/ #mapper.mappers=tk.mybatis.mapper.common.Mapper #mapper.not-empty=true
項(xiàng)目目錄如下:

LoginController.java文件:
注意:@Controller只能用這個(gè),不能用@RestController
package com.wei.demo01.controller;
import com.wei.demo01.entity.UserBean;
import com.wei.demo01.service.UserService;
import lombok.extern.slf4j.Slf4j;
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.RequestMethod;
import javax.annotation.Resource;
@Slf4j
@Controller
public class LoginController {
//將Service注入Web層
@Resource
UserService userService;
//實(shí)現(xiàn)登錄
@RequestMapping("/login")
public String show(){
return "login";
}
@RequestMapping(value = "/loginIn",method = RequestMethod.POST)
public String login(String name,String password){
UserBean userBean = userService.LoginIn(name, password);
log.info("name:{}",name);
log.info("password:{}",password);
if(userBean!=null){
return "success";
}else {
return "error";
}
}
@RequestMapping("/signup")
public String disp(){
return "signup";
}
//實(shí)現(xiàn)注冊(cè)功能
@RequestMapping(value = "/register",method = RequestMethod.POST)
public String signUp(String name,String password){
userService.Insert(name, password);
return "success";
}
}UserBean.java文件
package com.wei.demo01.entity;
import java.io.Serializable;
public class UserBean implements Serializable {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}UserMapper接口文件:
注意:我在這里為偷懶,開(kāi)始加入了@Mapper注解,目的就是為了不再寫(xiě)mapper映射文件(那個(gè)xml寫(xiě)的是真的是太慢了)
package com.wei.demo01.mapper;
import com.wei.demo01.entity.UserBean;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Mapper
public interface UserMapper {
//查詢,可以實(shí)現(xiàn)登錄功能
@Select("SELECT * FROM user WHERE name = #{name} AND password = #{password}")
UserBean getInfo(@Param("name") String name, @Param("password") String password);
//多個(gè)參數(shù)要加@Param修飾
//增加,可以實(shí)現(xiàn)注冊(cè)功能
@Insert("insert into user(name,password)values(#{name},#{password})")
void saveInfo(@Param("name") String name, @Param("password") String password);
}UserService.java文件
package com.wei.demo01.service;
import com.wei.demo01.entity.UserBean;
import com.wei.demo01.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
//將dao層屬性注入service層
@Resource
private UserMapper userMapper;
public UserBean LoginIn(String name, String password) {
return userMapper.getInfo(name,password);
}
public void Insert(String name,String password){
userMapper.saveInfo(name, password);
}
}下面都是一些靜態(tài)html網(wǎng)頁(yè):

下面就把所有的代碼按順序依次放在一個(gè)框子里面;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>登陸失??!</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>你好啊</h1>
</body>
</html>
<!--登錄頁(yè)面-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form role="form" action = "/loginIn" method="post">
賬號(hào):<input type="text" id="name" name = "name"> <br>
密碼:<input type="password" id = "password" name = "password"> <br>
<input type="submit" id = "login" value = "login">
<input type="button" id="register" value="注冊(cè)">
</form>
</body>
</html>
<!--注冊(cè)頁(yè)面-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注冊(cè)成功</title>
</head>
<body>
<form role="form" action="/register" method="post">
請(qǐng)輸入姓名:<input type="text" name="name" id="name"><br>
請(qǐng)輸入密碼:<input type="password" name="password" id="password"><br>
<input type="submit" name="sign" value="提交">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<h1>歡迎,恭喜登陸成功/注冊(cè)成功</h1>
</body>
</html>最后登錄效果展示:


注冊(cè)效果展示:


注冊(cè)成功!數(shù)據(jù)庫(kù)更新成功!

有網(wǎng)友說(shuō)我這個(gè)注冊(cè)按鈕打不開(kāi),跳轉(zhuǎn)不到注冊(cè)頁(yè)面,那我在這里更新一下:
在登錄頁(yè)面中加入以下內(nèi)容即可:

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的項(xiàng)目實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)SpringBoot 登錄注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot常見(jiàn)登錄(注冊(cè))幾種實(shí)現(xiàn)過(guò)程
- SpringBoot整合MD5加密完成注冊(cè)和登錄方式
- Springboot集成JWT實(shí)現(xiàn)登錄注冊(cè)的示例代碼
- SpringBoot結(jié)合JWT實(shí)現(xiàn)用戶登錄、注冊(cè)、鑒權(quán)
- 基于SpringBoot和Vue3的博客平臺(tái)的用戶注冊(cè)與登錄功能實(shí)現(xiàn)
- SpringBoot+JWT實(shí)現(xiàn)注冊(cè)、登錄、狀態(tài)續(xù)簽流程分析
- springboot+VUE實(shí)現(xiàn)登錄注冊(cè)
- SpringBoot實(shí)現(xiàn)登錄注冊(cè)常見(jiàn)問(wèn)題解決方案
- SpringBoot實(shí)現(xiàn)登錄注冊(cè)的三種基本方式
相關(guān)文章
Java使用easypoi快速導(dǎo)入導(dǎo)出的實(shí)現(xiàn)
這篇文章主要介紹了實(shí)現(xiàn)Java使用easypoi快速導(dǎo)入導(dǎo)出的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
Java簽名加密與驗(yàn)證實(shí)現(xiàn)過(guò)程
本文詳細(xì)介紹了Java中簽名加密驗(yàn)證簽名和SHA1withRSAUtil證書(shū)簽名算法的實(shí)現(xiàn)原理及流程,通過(guò)描述數(shù)據(jù)準(zhǔn)備、哈希計(jì)算、簽名生成、數(shù)據(jù)封裝、數(shù)據(jù)傳輸和服務(wù)器驗(yàn)證的完整過(guò)程,展示了如何在Java中確保數(shù)據(jù)傳輸?shù)陌踩院万?yàn)證2025-10-10
SpringBoot?JavaMailSender發(fā)送郵件功能(實(shí)例詳解)
JavaMailSender是Spring提供的,非常好用的,實(shí)現(xiàn)郵件發(fā)送的接口 ,這篇文章主要介紹了SpringBoot?JavaMailSender發(fā)送郵件功能,需要的朋友可以參考下2024-03-03
各種格式的編碼解碼工具類分享(hex解碼 base64編碼)
這篇文章主要介紹了各種格式的編碼解碼工具類,集成Commons-Codec、Commons-Lang及JDK提供的編解碼方法2014-01-01
Java日期時(shí)間與正則表達(dá)式超詳細(xì)整理(適合新手入門(mén))
如果使用得當(dāng),正則表達(dá)式是匹配各種模式的強(qiáng)大工具,下面這篇文章主要給大家介紹了關(guān)于Java日期時(shí)間與正則表達(dá)式超詳細(xì)整理的相關(guān)資料,本文非常適合新手入門(mén),需要的朋友可以參考下2023-04-04
啟用springboot security后登錄web頁(yè)面需要用戶名和密碼的解決方法
這篇文章主要介紹了啟用springboot security后登錄web頁(yè)面需要用戶名和密碼的解決方法,也就是使用默認(rèn)用戶和密碼登錄的操作方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析
這篇文章主要介紹了MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析,有時(shí)候并不需要真正的刪除數(shù)據(jù),而是想邏輯刪除,方便數(shù)據(jù)恢復(fù),MyBatis-Plus可以很方便的實(shí)現(xiàn)邏輯刪除的功能,需要的朋友可以參考下2023-11-11
已有的springcloud+mybatis項(xiàng)目升級(jí)為mybatis-plus的方法
這篇文章主要介紹了已有的springcloud+mybatis項(xiàng)目升級(jí)為mybatis-plus,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

