欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springBoot中myBatisPlus的使用步驟及示例代碼

 更新時(shí)間:2025年03月21日 09:20:49   作者:無(wú)足鳥(niǎo)丶  
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 Spring Boot 項(xiàng)目里使用它能極大提升開(kāi)發(fā)效率,下面為你詳細(xì)介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼,感興趣的朋友一起看看吧

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.propertiesapplication.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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring mvc Json處理實(shí)現(xiàn)流程代碼實(shí)例

    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)題

    這篇文章主要介紹了詳解springboot中redis的使用和分布式session共享問(wèn)題,詳細(xì)的介紹了解決分布式系統(tǒng)的session如何共享問(wèn)題,有興趣的可以了解一下
    2017-11-11
  • 還在用if(obj!=null)做非空判斷,帶你快速上手Optional

    還在用if(obj!=null)做非空判斷,帶你快速上手Optional

    這篇文章主要介紹了還在用if(obj!=null)做非空判斷,帶你快速上手Optional,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • java中常用的阻塞隊(duì)列與非阻塞隊(duì)列詳解

    java中常用的阻塞隊(duì)列與非阻塞隊(duì)列詳解

    這篇文章主要介紹了java中常用的阻塞隊(duì)列與非阻塞隊(duì)列用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式

    spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式

    這篇文章主要介紹了spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • mybatis返回key value map集合方式

    mybatis返回key value map集合方式

    這篇文章主要介紹了mybatis返回key value map集合方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • SpringBoot API增加version版本號(hào)方式

    SpringBoot API增加version版本號(hào)方式

    這篇文章主要介紹了SpringBoot API增加version版本號(hào)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Spring?IOC容器使用詳細(xì)講解

    Spring?IOC容器使用詳細(xì)講解

    IOC-Inversion?of?Control,即控制反轉(zhuǎn)。它不是什么技術(shù),而是一種設(shè)計(jì)思想。這篇文章將為大家介紹一下Spring控制反轉(zhuǎn)IOC的原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-12-12
  • java發(fā)送內(nèi)嵌圖片郵件

    java發(fā)送內(nèi)嵌圖片郵件

    這篇文章主要介紹了java發(fā)送內(nèi)嵌圖片郵件,在博客系統(tǒng)中需要郵件服務(wù)的功能,僅僅是發(fā)送文本內(nèi)容,現(xiàn)在嘗試一下發(fā)送內(nèi)嵌圖片郵件,感興趣的小伙伴們可以參考一下
    2016-01-01
  • SpringBoot整合Jackson超詳細(xì)用法(附Jackson工具類)

    SpringBoot整合Jackson超詳細(xì)用法(附Jackson工具類)

    這篇文章主要介紹了SpringBoot整合Jackson超詳細(xì)教程,本篇講的是Jackson的詳細(xì)用法,Jackson工具類在文章最后,直接復(fù)制粘貼即可使用,需要的朋友可以參考下
    2023-03-03

最新評(píng)論