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利用Spire.Doc for Java實現(xiàn)Word轉(zhuǎn) PCL打印格式
在日常的企業(yè)級開發(fā)中,文檔處理是一個繞不開的話題,本文將介紹如何在 Java 后端環(huán)境中,利用 Spire.Doc for Java 庫,在不依賴 Microsoft Office 軟件的情況下,實現(xiàn) Word 文檔到 PCL 格式的轉(zhuǎn)換,感興趣的可以了解下2026-04-04
基于SpringBoot實現(xiàn)動態(tài)配置數(shù)據(jù)庫的加載
這篇文章主要介紹了Spring?Boot?如何動態(tài)配置數(shù)據(jù)庫的加載,現(xiàn)項目有一個需求,期望通過在application.yml配置文件中設(shè)置一個開關(guān),來決定是否加載數(shù)據(jù)庫,文中通過代碼示例講解的非常詳細,需要的朋友可以參考下2024-10-10
MyBatis TypeHandler自定義類型轉(zhuǎn)換與實戰(zhàn)案例解析
本文將介紹MyBatis中TypeHandler的基礎(chǔ)概念,并通過具體的使用案例,展示如何自定義并應(yīng)用TypeHandler以提高數(shù)據(jù)處理的靈活性和可維護性,感興趣的朋友跟隨小編一起看看吧2026-01-01
SpringBoot整合Druid數(shù)據(jù)源的方法實現(xiàn)
Druid是阿里開發(fā)的一款開源的數(shù)據(jù)源,被很多人認為是Java語言中最好的數(shù)據(jù)庫連接池,本文主要介紹了SpringBoot整合Druid數(shù)據(jù)源的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06

