springboot mybatis-plus實現(xiàn)登錄接口
下面是使用SpringBoot和MyBatis-Plus實現(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ù)庫驅(qū)動 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.199</version> </dependency> <!-- 其他依賴... --> </dependencies>
- 創(chuàng)建數(shù)據(jù)庫表
創(chuàng)建一個名為user
的數(shù)據(jù)庫表,包含以下字段:
字段名 | 類型 | 描述 |
---|---|---|
id | BIGINT | 用戶ID |
name | VARCHAR | 用戶名 |
pwd | VARCHAR | 用戶密碼 |
- 創(chuàng)建實體類
創(chuàng)建一個名為User
的實體類,映射數(shù)據(jù)庫表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)建一個名為UserMapper
的Mapper接口,使用MyBatis-Plus提供的BaseMapper
進行CRUD操作:
public interface UserMapper extends BaseMapper<User> { }
- 編寫配置文件
在application.properties
文件中配置數(shù)據(jù)庫信息:
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)建一個名為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 "登錄失敗,用戶名或密碼錯誤"; } } }
該接口使用POST方式請求,接收一個User
類型的參數(shù)user
,其中包含用戶名和密碼。接口通過使用MyBatis-Plus提供的QueryWrapper
查詢用戶信息,如果查詢到了數(shù)據(jù),則返回登錄成功,否則返回登錄失敗。
- 測試登錄接口
啟動應(yīng)用程序,在瀏覽器中訪問http://localhost:8080/login
,進行登錄測試。例如,以下是使用cURL工具進行登錄測試的示例命令:
curl -X POST -H "Content-Type: application/json" -d '{"name":"admin","pwd":"123456"}' http://localhost:8080/login
如果返回登錄成功
,則表示登錄接口測試通過。
到此這篇關(guān)于springboot mybatis-plus實現(xiàn)登錄接口的文章就介紹到這了,更多相關(guān)springboot mybatis-plus 登錄接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基礎(chǔ)類庫之StringBuffer類用法詳解
String類是在所有開發(fā)項目開發(fā)之中一定會使用的一個功能類。雖然String類很好用,但也有弊端——內(nèi)容不允許頻繁修改,所以為了解決問題,我們提供了StringBuffer類。本文就來講講StringBuffer類的用法2022-07-07eclipse修改jvm參數(shù)調(diào)優(yōu)方法(2種)
本篇文章主要介紹了eclipse修改jvm參數(shù)調(diào)優(yōu)方法(2種),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02struts2.5+框架使用通配符與動態(tài)方法常見問題小結(jié)
這篇文章主要介紹了struts2.5+框架使用通配符與動態(tài)方法常見問題 ,在文中給大家提到了Struts2.5框架使用通配符指定方法 ,需要的朋友可以參考下2018-09-09Rxjava+Retrofit+MVP實現(xiàn)購物車功能
這篇文章主要為大家詳細介紹了Rxjava+Retrofit+MVP實現(xiàn)購物車功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05java如何防止表單重復(fù)提交的注解@RepeatSubmit
@RepeatSubmit是一個自定義注解,用于防止表單重復(fù)提交,它通過AOP和攔截器模式實現(xiàn),結(jié)合了線程安全和分布式環(huán)境的考慮,注解參數(shù)包括interval(間隔時間)和message(提示信息),使用時需要注意并發(fā)處理、用戶體驗、性能和安全性等方面,失效原因是多方面的2024-11-11