springBoot中myBatisPlus的使用步驟及示例代碼
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 Spring Boot 項(xiàng)目里使用它能極大提升開發(fā)效率。下面為你詳細(xì)介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼。
1. 創(chuàng)建 Spring Boot 項(xiàng)目
你可以借助 Spring Initializr(https://start.spring.io/)來創(chuàng)建一個(gè)新的 Spring Boot 項(xiàng)目,添加以下依賴:
- Spring Web
- Spring Data JPA(雖然用 MyBatis-Plus,但這個(gè)依賴可按需添加)
- 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)建實(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 來處理 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)用后,你就能通過以下 API 來操作 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ù)庫操作了。
到此這篇關(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如何解決分頁組件IPage失效問題
- SpringBoot連接PostgreSQL+MybatisPlus入門案例(代碼詳解)
- SpringBoot項(xiàng)目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫的過程
- SpringBoot整合MybatisPlus的基本應(yīng)用詳解
- 解決SpringBoot搭建MyBatisPlus中selectList遇到LambdaQueryWrapper報(bào)錯(cuò)問題
- SpringBoot集成MyBatisPlus+MySQL的實(shí)現(xiàn)
相關(guān)文章
SpringBoot項(xiàng)目中application.yml和bootstrap.yml文件的區(qū)別及說明
`application.yml`和`bootstrap.yml`都是Spring Boot項(xiàng)目中的配置文件,但它們?cè)诩虞d時(shí)機(jī)、用途、優(yōu)先級(jí)、配置來源、適用場(chǎng)景和是否必須存在等方面存在區(qū)別2025-03-03
Java中JUC包(java.util.concurrent)下的常用子類
相信大家已經(jīng)對(duì)并發(fā)機(jī)制中出現(xiàn)的很多的常見知識(shí)點(diǎn)進(jìn)行了總結(jié),下面這篇文章主要給大家介紹了關(guān)于Java中JUC包(java.util.concurrent)下的常用子類的相關(guān)資料,文中通過圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
如何使用eclipse搭建maven多module項(xiàng)目(構(gòu)建父子項(xiàng)目)
這篇文章主要介紹了如何使用eclipse搭建maven多module項(xiàng)目(構(gòu)建父子項(xiàng)目) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
SSH框架網(wǎng)上商城項(xiàng)目第13戰(zhàn)之Struts2文件上傳功能
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第13戰(zhàn)之Struts2文件上傳功能的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06
Springboot整合Mybatis和SQLite的詳細(xì)過程
這篇文章主要介紹了Springboot整合Mybatis和SQLite的詳細(xì)過程,本文通過圖文示例相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07

