SpringBoot+MyBatis Plus實(shí)現(xiàn)用戶登錄認(rèn)證
本文主要介紹了 SpringBoot+MyBatis Plus實(shí)現(xiàn)用戶登錄認(rèn)證,具體如下:

一、創(chuàng)建用戶表 SQL
首先,我們創(chuàng)建一個(gè) user 表來(lái)存儲(chǔ)用戶信息:
CREATE TABLE `user` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主鍵', `username` VARCHAR(50) NOT NULL UNIQUE COMMENT '用戶名', `password` VARCHAR(100) NOT NULL COMMENT '密碼(建議加密存儲(chǔ))', `nickname` VARCHAR(50) DEFAULT NULL COMMENT '昵稱', `avatar` VARCHAR(255) DEFAULT NULL COMMENT '頭像URL', `email` VARCHAR(100) DEFAULT NULL COMMENT '郵箱', `phone` VARCHAR(20) DEFAULT NULL COMMENT '手機(jī)號(hào)', `status` TINYINT NOT NULL DEFAULT '1' COMMENT '狀態(tài):0-禁用,1-啟用', `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間', `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用戶表';
二、Spring Boot + MyBatis Plus 實(shí)現(xiàn)登錄認(rèn)證功能
1. 添加依賴([pom.xml](file://D:\workspace\vue_workspace\burns-book-backend\pom.xml))
確保你已引入以下依賴:
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- MySQL 驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!-- Lombok(可選) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
2. 配置數(shù)據(jù)庫(kù)連接(application.yml)
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath*:mapper/**/*.xml
configuration:
mapUnderscoreToCamelCase: true
3. 創(chuàng)建實(shí)體類User.java
package com.burns.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String username;
private String password;
private String nickname;
private String avatar;
private String email;
private String phone;
private Integer status;
}
4. 創(chuàng)建 Mapper 接口UserMapper.java
package com.burns.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.burns.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
5. 創(chuàng)建 Service 層UserService.java
package com.burns.service;
import com.burns.entity.User;
public interface UserService {
User login(String username, String password);
}
實(shí)現(xiàn)類 UserServiceImpl.java
package com.burns.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.burns.entity.User;
import com.burns.mapper.UserMapper;
import com.burns.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User login(String username, String password) {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("username", username).eq("password", password);
return userMapper.selectOne(wrapper);
}
}
6. 控制器層AuthController.java
package com.burns.controller;
import com.burns.entity.User;
import com.burns.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AuthController {
@Autowired
private UserService userService;
@PostMapping("/login")
public Object login(@RequestParam String username, @RequestParam String password) {
User user = userService.login(username, password);
if (user == null) {
return "登錄失敗";
}
return user;
}
}
? 測(cè)試接口
你可以使用 Postman 或 curl 測(cè)試登錄接口:
POST http://localhost:8080/login Body (form-data): username=admin password=123456
?? 總結(jié)表格
| 文件 | 說(shuō)明 |
|---|---|
| user.sql | 創(chuàng)建用戶表的 SQL 腳本 |
| User.java | 用戶實(shí)體類 |
| UserMapper.java | 數(shù)據(jù)庫(kù)操作接口 |
| UserService.java / UserServiceImpl.java | 登錄邏輯處理 |
| AuthController.java | 提供 /login 接口 |
如果你需要進(jìn)一步集成 JWT、Shiro、Spring Security 等安全框架,請(qǐng)告訴我,我可以繼續(xù)為你擴(kuò)展。
到此這篇關(guān)于SpringBoot+MyBatis Plus實(shí)現(xiàn)用戶登錄認(rèn)證的文章就介紹到這了,更多相關(guān)SpringBoot MyBatis Plus登錄認(rèn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Shiro實(shí)現(xiàn)登錄認(rèn)證的方法
- springboot前后端分離集成CAS單點(diǎn)登錄(統(tǒng)一認(rèn)證)
- SpringBoot+Vue+JWT的前后端分離登錄認(rèn)證詳細(xì)步驟
- SpringBoot?實(shí)現(xiàn)CAS?Server統(tǒng)一登錄認(rèn)證的詳細(xì)步驟
- Vue+Jwt+SpringBoot+Ldap完成登錄認(rèn)證的示例代碼
- 基于springboot實(shí)現(xiàn)整合shiro實(shí)現(xiàn)登錄認(rèn)證以及授權(quán)過(guò)程解析
- Springboot+Spring Security實(shí)現(xiàn)前后端分離登錄認(rèn)證及權(quán)限控制的示例代碼
- SpringBoot整合Sa-Token實(shí)現(xiàn)登錄認(rèn)證的示例代碼
- Springboot整合SpringSecurity實(shí)現(xiàn)登錄認(rèn)證和鑒權(quán)全過(guò)程
相關(guān)文章
mybatis-plus批量更新updateBatchById問(wèn)題
這篇文章主要介紹了mybatis-plus批量更新updateBatchById問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java在Map轉(zhuǎn)Json字符串時(shí)出現(xiàn)"\"轉(zhuǎn)義字符的解決辦法
當(dāng)一個(gè)Map被轉(zhuǎn)成Json字符串后,被添加到另一個(gè)Map中,會(huì)出現(xiàn)被加上“\”轉(zhuǎn)義字符的情況,這個(gè)時(shí)候該如何解決呢,下面就來(lái)和小編一起了解一下2023-07-07
FeignClient如何通過(guò)配置變量調(diào)用配置文件url
這篇文章主要介紹了FeignClient如何通過(guò)配置變量調(diào)用配置文件url,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java反射設(shè)置/獲取對(duì)象屬性值三種方式
這篇文章主要給大家介紹了關(guān)于Java反射設(shè)置/獲取對(duì)象屬性值的三種方式,反射機(jī)制的用途非常多,比如獲取方法,屬性名和屬性值等,甚至可以獲取標(biāo)簽等標(biāo)簽屬性,需要的朋友可以參考下2023-11-11
JVM堆內(nèi)存溢出后,其他線程是否可繼續(xù)工作的問(wèn)題解析
這篇文章主要介紹了JVM 堆內(nèi)存溢出后,其他線程是否可繼續(xù)工作?,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
springboot?vue項(xiàng)目管理前后端實(shí)現(xiàn)編輯功能
這篇文章主要為大家介紹了springboot?vue項(xiàng)目管理前后端實(shí)現(xiàn)編輯功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
eclipse maven maven-archetype-webapp 創(chuàng)建失敗問(wèn)題解決
這篇文章主要介紹了eclipse maven maven-archetype-webapp 創(chuàng)建失敗問(wèn)題解決的相關(guān)資料,需要的朋友可以參考下2016-12-12
SpringBoot實(shí)現(xiàn)異步事件驅(qū)動(dòng)的方法
本文主要介紹了SpringBoot實(shí)現(xiàn)異步事件驅(qū)動(dòng)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06

