springBoot中myBatisPlus的使用步驟及示例代碼
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 Spring Boot 項(xiàng)目里使用它能極大提升開(kāi)發(fā)效率。下面為你詳細(xì)介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼。
1. 創(chuàng)建 Spring Boot 項(xiàng)目
你可以借助 Spring Initializr(https://start.spring.io/)來(lái)創(chuàng)建一個(gè)新的 Spring Boot 項(xiàng)目,添加以下依賴:
- Spring Web
- Spring Data JPA(雖然用 MyBatis-Plus,但這個(gè)依賴可按需添加)
- MyBatis Framework
- MySQL Driver(假設(shè)使用 MySQL 數(shù)據(jù)庫(kù))
2. 添加 MyBatis-Plus 依賴
在 pom.xml
里添加 MyBatis-Plus 的依賴:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.1</version> </dependency>
3. 配置數(shù)據(jù)庫(kù)連接
在 application.properties
或 application.yml
中配置數(shù)據(jù)庫(kù)連接信息:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
4. 創(chuàng)建實(shí)體類
創(chuàng)建一個(gè)簡(jiǎn)單的實(shí)體類,例如 User
:
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @TableName("user") public class User { @TableId private Long id; private String name; private Integer age; // Getters and Setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
5. 創(chuàng)建 Mapper 接口
創(chuàng)建一個(gè)繼承自 BaseMapper
的 Mapper 接口:
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.springframework.stereotype.Repository; @Repository public interface UserMapper extends BaseMapper<User> { }
6. 創(chuàng)建 Service 層
創(chuàng)建一個(gè) Service 接口及其實(shí)現(xiàn)類:
import java.util.List; public interface UserService { List<User> getAllUsers(); User getUserById(Long id); boolean saveUser(User user); boolean updateUser(User user); boolean deleteUser(Long id); }
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> getAllUsers() { return userMapper.selectList(null); } @Override public User getUserById(Long id) { return userMapper.selectById(id); } @Override public boolean saveUser(User user) { return userMapper.insert(user) > 0; } @Override public boolean updateUser(User user) { return userMapper.updateById(user) > 0; } @Override public boolean deleteUser(Long id) { return userMapper.deleteById(id) > 0; } }
7. 創(chuàng)建 Controller 層
創(chuàng)建一個(gè) Controller 來(lái)處理 HTTP 請(qǐng)求:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping public List<User> getAllUsers() { return userService.getAllUsers(); } @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } @PostMapping public boolean saveUser(@RequestBody User user) { return userService.saveUser(user); } @PutMapping public boolean updateUser(@RequestBody User user) { return userService.updateUser(user); } @DeleteMapping("/{id}") public boolean deleteUser(@PathVariable Long id) { return userService.deleteUser(id); } }
8. 啟動(dòng)應(yīng)用
啟動(dòng) Spring Boot 應(yīng)用后,你就能通過(guò)以下 API 來(lái)操作 User
數(shù)據(jù):
GET /users
:獲取所有用戶信息。GET /users/{id}
:根據(jù) ID 獲取用戶信息。POST /users
:新增用戶。PUT /users
:更新用戶信息。DELETE /users/{id}
:根據(jù) ID 刪除用戶。
按照以上步驟,你就能在 Spring Boot 項(xiàng)目中使用 MyBatis-Plus 進(jìn)行數(shù)據(jù)庫(kù)操作了。
到此這篇關(guān)于springBoot中myBatisPlus的使用步驟及示例代碼的文章就介紹到這了,更多相關(guān)springBoot myBatisPlus使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot如何使用MybatisPlus
- SpringBoot3.0集成MybatisPlus的實(shí)現(xiàn)方法
- Springboot mybatisplus如何解決分頁(yè)組件IPage失效問(wèn)題
- SpringBoot連接PostgreSQL+MybatisPlus入門案例(代碼詳解)
- SpringBoot項(xiàng)目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫(kù)的過(guò)程
- SpringBoot整合MybatisPlus的基本應(yīng)用詳解
- 解決SpringBoot搭建MyBatisPlus中selectList遇到LambdaQueryWrapper報(bào)錯(cuò)問(wèn)題
- SpringBoot集成MyBatisPlus+MySQL的實(shí)現(xiàn)
相關(guān)文章
Spring mvc Json處理實(shí)現(xiàn)流程代碼實(shí)例
這篇文章主要介紹了Spring mvc Json處理實(shí)現(xiàn)流程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09詳解springboot中redis的使用和分布式session共享問(wèn)題
這篇文章主要介紹了詳解springboot中redis的使用和分布式session共享問(wèn)題,詳細(xì)的介紹了解決分布式系統(tǒng)的session如何共享問(wèn)題,有興趣的可以了解一下2017-11-11還在用if(obj!=null)做非空判斷,帶你快速上手Optional
這篇文章主要介紹了還在用if(obj!=null)做非空判斷,帶你快速上手Optional,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05java中常用的阻塞隊(duì)列與非阻塞隊(duì)列詳解
這篇文章主要介紹了java中常用的阻塞隊(duì)列與非阻塞隊(duì)列用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式
這篇文章主要介紹了spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04SpringBoot API增加version版本號(hào)方式
這篇文章主要介紹了SpringBoot API增加version版本號(hào)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot整合Jackson超詳細(xì)用法(附Jackson工具類)
這篇文章主要介紹了SpringBoot整合Jackson超詳細(xì)教程,本篇講的是Jackson的詳細(xì)用法,Jackson工具類在文章最后,直接復(fù)制粘貼即可使用,需要的朋友可以參考下2023-03-03