springBoot中myBatisPlus的使用步驟及示例代碼
MyBatis-Plus 是一個 MyBatis 的增強工具,在 Spring Boot 項目里使用它能極大提升開發(fā)效率。下面為你詳細(xì)介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼。
1. 創(chuàng)建 Spring Boot 項目
你可以借助 Spring Initializr(https://start.spring.io/)來創(chuàng)建一個新的 Spring Boot 項目,添加以下依賴:
- Spring Web
- Spring Data JPA(雖然用 MyBatis-Plus,但這個依賴可按需添加)
- MyBatis Framework
- MySQL Driver(假設(shè)使用 MySQL 數(shù)據(jù)庫)
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ù)庫連接
在 application.properties 或 application.yml 中配置數(shù)據(jù)庫連接信息:
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)建實體類
創(chuàng)建一個簡單的實體類,例如 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)建一個繼承自 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)建一個 Service 接口及其實現(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)建一個 Controller 來處理 HTTP 請求:
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. 啟動應(yīng)用
啟動 Spring Boot 應(yīng)用后,你就能通過以下 API 來操作 User 數(shù)據(jù):
GET /users:獲取所有用戶信息。GET /users/{id}:根據(jù) ID 獲取用戶信息。POST /users:新增用戶。PUT /users:更新用戶信息。DELETE /users/{id}:根據(jù) ID 刪除用戶。
按照以上步驟,你就能在 Spring Boot 項目中使用 MyBatis-Plus 進(jìn)行數(shù)據(jù)庫操作了。
到此這篇關(guān)于springBoot中myBatisPlus的使用步驟及示例代碼的文章就介紹到這了,更多相關(guān)springBoot myBatisPlus使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot如何使用MybatisPlus
- SpringBoot3.0集成MybatisPlus的實現(xiàn)方法
- Springboot mybatisplus如何解決分頁組件IPage失效問題
- SpringBoot連接PostgreSQL+MybatisPlus入門案例(代碼詳解)
- SpringBoot項目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫的過程
- SpringBoot整合MybatisPlus的基本應(yīng)用詳解
- 解決SpringBoot搭建MyBatisPlus中selectList遇到LambdaQueryWrapper報錯問題
- SpringBoot集成MyBatisPlus+MySQL的實現(xiàn)
相關(guān)文章
Spring mvc Json處理實現(xiàn)流程代碼實例
這篇文章主要介紹了Spring mvc Json處理實現(xiàn)流程代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
詳解springboot中redis的使用和分布式session共享問題
這篇文章主要介紹了詳解springboot中redis的使用和分布式session共享問題,詳細(xì)的介紹了解決分布式系統(tǒng)的session如何共享問題,有興趣的可以了解一下2017-11-11
還在用if(obj!=null)做非空判斷,帶你快速上手Optional
這篇文章主要介紹了還在用if(obj!=null)做非空判斷,帶你快速上手Optional,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
spring+apollo動態(tài)獲取yaml格式的配置方式
這篇文章主要介紹了spring+apollo動態(tài)獲取yaml格式的配置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
SpringBoot整合Jackson超詳細(xì)用法(附Jackson工具類)
這篇文章主要介紹了SpringBoot整合Jackson超詳細(xì)教程,本篇講的是Jackson的詳細(xì)用法,Jackson工具類在文章最后,直接復(fù)制粘貼即可使用,需要的朋友可以參考下2023-03-03

