springboot mybatis-plus實(shí)現(xiàn)登錄接口
下面是使用SpringBoot和MyBatis-Plus實(shí)現(xiàn)登錄接口的示例代碼:
- 添加依賴
在pom.xml文件中添加以下依賴:
<dependencies> <!-- SpringBoot --> <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.4.0</version> </dependency> <!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.199</version> </dependency> <!-- 其他依賴... --> </dependencies>
- 創(chuàng)建數(shù)據(jù)庫(kù)表
創(chuàng)建一個(gè)名為user
的數(shù)據(jù)庫(kù)表,包含以下字段:
字段名 | 類型 | 描述 |
---|---|---|
id | BIGINT | 用戶ID |
name | VARCHAR | 用戶名 |
pwd | VARCHAR | 用戶密碼 |
- 創(chuàng)建實(shí)體類
創(chuàng)建一個(gè)名為User
的實(shí)體類,映射數(shù)據(jù)庫(kù)表user
:
@Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class User implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String name; private String pwd; }
- 創(chuàng)建Mapper接口
創(chuàng)建一個(gè)名為UserMapper
的Mapper接口,使用MyBatis-Plus提供的BaseMapper
進(jìn)行CRUD操作:
public interface UserMapper extends BaseMapper<User> { }
- 編寫配置文件
在application.properties
文件中配置數(shù)據(jù)庫(kù)信息:
spring.datasource.url=jdbc:h2:mem:test spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml mybatis-plus.type-aliases-package=com.example.demo.entity mybatis-plus.configuration.map-underscore-to-camel-case=true
- 編寫登錄接口
創(chuàng)建一個(gè)名為UserController
的控制器,在其中編寫登錄接口:
@RestController public class UserController { @Autowired private UserMapper userMapper; @PostMapping("/login") public String login(@RequestBody User user) { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("name", user.getName()).eq("pwd", user.getPwd()); User dbUser = userMapper.selectOne(wrapper); if (dbUser != null) { return "登錄成功"; } else { return "登錄失敗,用戶名或密碼錯(cuò)誤"; } } }
該接口使用POST方式請(qǐng)求,接收一個(gè)User
類型的參數(shù)user
,其中包含用戶名和密碼。接口通過(guò)使用MyBatis-Plus提供的QueryWrapper
查詢用戶信息,如果查詢到了數(shù)據(jù),則返回登錄成功,否則返回登錄失敗。
- 測(cè)試登錄接口
啟動(dòng)應(yīng)用程序,在瀏覽器中訪問(wèn)http://localhost:8080/login
,進(jìn)行登錄測(cè)試。例如,以下是使用cURL工具進(jìn)行登錄測(cè)試的示例命令:
curl -X POST -H "Content-Type: application/json" -d '{"name":"admin","pwd":"123456"}' http://localhost:8080/login
如果返回登錄成功
,則表示登錄接口測(cè)試通過(guò)。
到此這篇關(guān)于springboot mybatis-plus實(shí)現(xiàn)登錄接口的文章就介紹到這了,更多相關(guān)springboot mybatis-plus 登錄接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基礎(chǔ)類庫(kù)之StringBuffer類用法詳解
String類是在所有開發(fā)項(xiàng)目開發(fā)之中一定會(huì)使用的一個(gè)功能類。雖然String類很好用,但也有弊端——內(nèi)容不允許頻繁修改,所以為了解決問(wèn)題,我們提供了StringBuffer類。本文就來(lái)講講StringBuffer類的用法2022-07-07eclipse修改jvm參數(shù)調(diào)優(yōu)方法(2種)
本篇文章主要介紹了eclipse修改jvm參數(shù)調(diào)優(yōu)方法(2種),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02struts2.5+框架使用通配符與動(dòng)態(tài)方法常見(jiàn)問(wèn)題小結(jié)
這篇文章主要介紹了struts2.5+框架使用通配符與動(dòng)態(tài)方法常見(jiàn)問(wèn)題 ,在文中給大家提到了Struts2.5框架使用通配符指定方法 ,需要的朋友可以參考下2018-09-09Rxjava+Retrofit+MVP實(shí)現(xiàn)購(gòu)物車功能
這篇文章主要為大家詳細(xì)介紹了Rxjava+Retrofit+MVP實(shí)現(xiàn)購(gòu)物車功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Java無(wú)界阻塞隊(duì)列DelayQueue詳細(xì)解析
這篇文章主要介紹了Java無(wú)界阻塞隊(duì)列DelayQueue詳細(xì)解析,DelayQueue是一個(gè)支持時(shí)延獲取元素的無(wú)界阻塞隊(duì)列,隊(duì)列使用PriorityQueue來(lái)實(shí)現(xiàn),隊(duì)列中的元素必須實(shí)現(xiàn)Delayed接口,在創(chuàng)建元素時(shí)可以指定多久才能從隊(duì)列中獲取當(dāng)前元素,需要的朋友可以參考下2023-12-12springboot使用Redis隊(duì)列實(shí)戰(zhàn)
本文主要介紹了springboot使用Redis隊(duì)列實(shí)戰(zhàn),包含四種實(shí)現(xiàn)方式,基于List的 LPUSH+BRPOP的實(shí)現(xiàn), 基于Sorted-Set的實(shí)現(xiàn),PUB/SUB訂閱/發(fā)布模式和基于Stream類型的實(shí)現(xiàn),感興趣的可以了解一下2024-07-07java如何防止表單重復(fù)提交的注解@RepeatSubmit
@RepeatSubmit是一個(gè)自定義注解,用于防止表單重復(fù)提交,它通過(guò)AOP和攔截器模式實(shí)現(xiàn),結(jié)合了線程安全和分布式環(huán)境的考慮,注解參數(shù)包括interval(間隔時(shí)間)和message(提示信息),使用時(shí)需要注意并發(fā)處理、用戶體驗(yàn)、性能和安全性等方面,失效原因是多方面的2024-11-11