最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 中整合 MyBatis-Plus詳細(xì)步驟(最新推薦)

 更新時(shí)間:2025年01月03日 11:24:25   作者:顏淡慕瀟  
本文詳細(xì)介紹了如何在SpringBoot項(xiàng)目中整合MyBatis-Plus,包括整合步驟、基本CRUD操作、分頁(yè)查詢、批量操作、自定義SQL操作等,通過(guò)這些步驟,開(kāi)發(fā)者可以快速實(shí)現(xiàn)數(shù)據(jù)庫(kù)操作,提高開(kāi)發(fā)效率,感興趣的朋友一起看看吧

在 Spring Boot 中整合 MyBatis-Plus 可以按照以下步驟進(jìn)行:

一、整合步驟

1. 創(chuàng)建 Spring Boot 項(xiàng)目

首先,使用 Spring Initializr(https://start.spring.io/)創(chuàng)建一個(gè)新的 Spring Boot 項(xiàng)目。在創(chuàng)建過(guò)程中,選擇以下依賴:

  • Spring Web
  • MySQL Driver
  • MyBatis-Plus

2. 配置項(xiàng)目依賴

如果手動(dòng)創(chuàng)建 pom.xml 文件,確保添加以下依賴:

<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- MyBatis-Plus Starter -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>最新版本</version>
    </dependency>
    <!-- MySQL Driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- Spring Boot Starter Test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

請(qǐng)將 最新版本 替換為 MyBatis-Plus 的實(shí)際最新版本號(hào)。

3. 配置數(shù)據(jù)源

application.yml 文件中配置數(shù)據(jù)庫(kù)連接信息:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/yourdatabase
    username: yourusername
    password: yourpassword
    driver-class-name: com.mysql.cj.jdbc.Driver

yourdatabaseyourusernameyourpassword 替換為實(shí)際的數(shù)據(jù)庫(kù)名稱、用戶名和密碼。

4. 創(chuàng)建實(shí)體類

創(chuàng)建一個(gè) Java 實(shí)體類,用于映射數(shù)據(jù)庫(kù)表。例如,創(chuàng)建一個(gè) User 實(shí)體類:

package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class User {
    private Long id;
    private String username;
    private String password;
}

@Data 是 Lombok 注解,用于自動(dòng)生成 gettersetter、equals、hashCodetoString 方法。@TableName 注解指定實(shí)體類對(duì)應(yīng)的數(shù)據(jù)庫(kù)表名。

5. 創(chuàng)建 Mapper 接口

創(chuàng)建一個(gè) Mapper 接口,繼承 BaseMapper 接口,BaseMapper 是 MyBatis-Plus 提供的基礎(chǔ)接口,包含了常用的 CRUD 方法。例如,創(chuàng)建一個(gè) UserMapper 接口:

package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 可以在這里定義自定義的 SQL 方法
}

@Mapper 注解用于將該接口標(biāo)記為 MyBatis 的 Mapper 接口。

6. 創(chuàng)建 Service 層

創(chuàng)建一個(gè) Service 接口和其實(shí)現(xiàn)類。例如,創(chuàng)建一個(gè) UserService 接口和 UserServiceImpl 實(shí)現(xiàn)類:

package com.example.demo.service;
import com.example.demo.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
public interface UserService extends IService<User> {
    // 可以在這里定義自定義的業(yè)務(wù)方法
}
package com.example.demo.service.impl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    // 如果需要,可以重寫或?qū)崿F(xiàn)自定義方法
}

ServiceImpl 是 MyBatis-Plus 提供的基礎(chǔ) Service 實(shí)現(xiàn)類,它提供了基本的 CRUD 方法實(shí)現(xiàn)。

7. 創(chuàng)建 Controller 層

創(chuàng)建一個(gè) Controller 層,用于處理客戶端請(qǐng)求。例如,創(chuàng)建一個(gè) UserController

package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
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> findAll() {
        return userService.list();
    }
    @GetMapping("/{id}")
    public User findById(@PathVariable Long id) {
        return userService.getById(id);
    }
    @PostMapping
    public boolean save(@RequestBody User user) {
        return userService.save(user);
    }
    @PutMapping
    public boolean update(@RequestBody User user) {
        return userService.updateById(user);
    }
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return userService.removeById(id);
    }
}

8. 配置 MyBatis-Plus 掃描

在 Spring Boot 主應(yīng)用類上添加 @MapperScan 注解,掃描 Mapper 接口所在的包:

package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

9. 測(cè)試

可以使用 Spring Boot 的測(cè)試框架來(lái)測(cè)試各個(gè)接口:

package com.example.demo;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
public class DemoApplicationTests {
    @Autowired
    private UserService userService;
    @Test
    public void testFindAll() {
        assertTrue(userService.list().size() >= 0);
    }
    @Test
    public void testFindById() {
        User user = new User();
        user.setUsername("testuser");
        user.setPassword("testpassword");
        userService.save(user);
        assertNotNull(userService.getById(user.getId()));
    }
    @Test
    public void testSave() {
        User user = new User();
        user.setUsername("newuser");
        user.setPassword("newpassword");
        assertTrue(userService.save(user));
    }
    @Test
    public void testUpdate() {
        User user = new User();
        user.setUsername("updateuser");
        user.setPassword("updatepassword");
        userService.save(user);
        user.setPassword("newupdatepassword");
        assertTrue(userService.updateById(user));
    }
    @Test
    public void testDelete() {
        User user = new User();
        user.setUsername("deleteuser");
        user.setPassword("deletepassword");
        userService.save(user);
        assertTrue(userService.removeById(user.getId()));
    }
}

通過(guò)以上步驟,你就可以在 Spring Boot 項(xiàng)目中成功整合 MyBatis-Plus,并實(shí)現(xiàn)基本的 CRUD 操作。在實(shí)際項(xiàng)目中,你可以根據(jù)需求進(jìn)一步擴(kuò)展和優(yōu)化代碼。

二、 基本 CRUD 操作

在前面整合的基礎(chǔ)上,我們已經(jīng)有了基本的 CRUD 方法。

新增操作

UserService 中調(diào)用 save 方法:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    // 新增操作
    @Override
    public boolean saveUser(User user) {
        return this.save(user);
    }
}

查詢操作

根據(jù) ID 查詢:調(diào)用 getById 方法。

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    // 根據(jù) ID 查詢
    @Override
    public User getUserById(Long id) {
        return this.getById(id);
    }
}

查詢所有:調(diào)用 list 方法。

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    // 查詢所有
    @Override
    public List<User> getAllUsers() {
        return this.list();
    }
}

更新操作

調(diào)用 updateById 方法:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    // 更新操作
    @Override
    public boolean updateUser(User user) {
        return this.updateById(user);
    }
}

刪除操作

根據(jù) ID 刪除:調(diào)用 removeById 方法。

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    // 根據(jù) ID 刪除
    @Override
    public boolean deleteUserById(Long id) {
        return this.removeById(id);
    }
}

條件查詢

使用 QueryWrapper 構(gòu)建查詢條件。

簡(jiǎn)單條件查詢

例如,查詢用戶名等于某個(gè)值的用戶:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public List<User> findUsersByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return this.list(queryWrapper);
    }
}

復(fù)雜條件查詢

多個(gè)條件組合,例如查詢用戶名包含某個(gè)字符串且年齡大于某個(gè)值的用戶:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public List<User> findComplexUsers(String usernameLike, Integer ageGreaterThan) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", usernameLike);
        queryWrapper.gt("age", ageGreaterThan);
        return this.list(queryWrapper);
    }
}

分頁(yè)查詢

使用 Page 類實(shí)現(xiàn)分頁(yè)查詢。

首先,在 UserService 中定義分頁(yè)查詢方法:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public Page<User> getUserPage(int current, int size) {
        Page<User> page = new Page<>(current, size);
        return this.page(page);
    }
}

批量操作

批量插入

使用 saveBatch 方法批量插入用戶:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public boolean saveUserBatch(List<User> userList) {
        return this.saveBatch(userList);
    }
}

批量更新

使用 updateBatchById 方法批量更新用戶:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public boolean updateUserBatch(List<User> userList) {
        return this.updateBatchById(userList);
    }
}

批量刪除

使用 removeByIds 方法批量刪除用戶:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public boolean deleteUserBatch(List<Long> idList) {
        return this.removeByIds(idList);
    }
}

自定義 SQL 操作

如果 MyBatis-Plus 提供的內(nèi)置方法無(wú)法滿足需求,可以在 Mapper 接口中定義自定義 SQL 方法。

在 Mapper 接口中定義方法

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("SELECT * FROM user WHERE age > #{age}")
    List<User> findUsersByAge(int age);
}

在 Service 層調(diào)用自定義方法

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public List<User> findUsersByAge(int age) {
        return userMapper.findUsersByAge(age);
    }
}

示例代碼總結(jié)

上述示例展示了在 Spring Boot 中使用 MyBatis-Plus 進(jìn)行各種數(shù)據(jù)庫(kù)操作的方法。在實(shí)際項(xiàng)目中,可以根據(jù)具體需求,在 Service 層和 Mapper 層靈活組合和擴(kuò)展這些方法,以實(shí)現(xiàn)復(fù)雜的業(yè)務(wù)邏輯。

注意事項(xiàng)

  • 確保數(shù)據(jù)庫(kù)表結(jié)構(gòu)與實(shí)體類屬性對(duì)應(yīng),否則可能導(dǎo)致數(shù)據(jù)操作異常。
  • 在使用 QueryWrapper 構(gòu)建條件時(shí),注意條件的正確性和安全性,防止 SQL 注入。
  • 對(duì)于分頁(yè)操作,要合理設(shè)置分頁(yè)參數(shù),避免數(shù)據(jù)量過(guò)大導(dǎo)致性能問(wèn)題。

通過(guò)以上內(nèi)容,你可以全面掌握在 Spring Boot 中使用 MyBatis-Plus 進(jìn)行數(shù)據(jù)庫(kù)操作的方法。

到此這篇關(guān)于Spring Boot 中整合 MyBatis-Plus詳細(xì)步驟的文章就介紹到這了,更多相關(guān)Spring Boot 整合 MyBatis-Plus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一文解決pom.xml報(bào)錯(cuò)Dependency "xxx" not found的問(wèn)題

    一文解決pom.xml報(bào)錯(cuò)Dependency "xxx" not f

    我們?cè)谑褂胢aven進(jìn)行jar包管理時(shí)有時(shí)會(huì)遇到pom.xml中報(bào)錯(cuò)Dependency “XXX” not found,所以在本文中將給大家介紹一下pom.xml報(bào)錯(cuò)Dependency "xxx" not found的解決方案,需要的朋友可以參考下
    2024-01-01
  • java測(cè)試框架的方法

    java測(cè)試框架的方法

    這篇文章主要介紹了java測(cè)試框架的方法,文中代碼非常詳細(xì),供大家學(xué)習(xí)和參考,感興趣的朋友可以了解下
    2020-06-06
  • 從java源碼分析線程池(池化技術(shù))的實(shí)現(xiàn)原理

    從java源碼分析線程池(池化技術(shù))的實(shí)現(xiàn)原理

    這篇文章主要介紹了從java源碼分析線程池(池化技術(shù))的實(shí)現(xiàn)原理,池化技術(shù)是一種編程技巧,當(dāng)程序出現(xiàn)高并發(fā)時(shí),能夠明顯的優(yōu)化程序,降低系統(tǒng)頻繁創(chuàng)建銷毀連接等額外開(kāi)銷,下文更多的相關(guān)介紹需要的小伙伴可以參考一下
    2022-04-04
  • Java List的remove()方法陷阱以及性能優(yōu)化

    Java List的remove()方法陷阱以及性能優(yōu)化

    Java List在進(jìn)行remove()方法是通常容易踩坑,本文就詳細(xì)的介紹一下陷阱以及性能優(yōu)化,感興趣的可以了解一下
    2021-10-10
  • IDEA使用jformdesigner插件做管理系統(tǒng)MVC架構(gòu)的步驟和實(shí)現(xiàn)思路

    IDEA使用jformdesigner插件做管理系統(tǒng)MVC架構(gòu)的步驟和實(shí)現(xiàn)思路

    在?IntelliJ?IDEA?中結(jié)合?JFormDesigner?插件,通過(guò)?Swing?框架實(shí)現(xiàn)一個(gè)管理系統(tǒng)的?MVC?架構(gòu)是一種經(jīng)典的開(kāi)發(fā)方式,以下是具體的步驟和實(shí)現(xiàn)思路,包含從項(xiàng)目創(chuàng)建到?MVC?架構(gòu)的核心代碼實(shí)現(xiàn),需要的朋友可以參考下
    2024-12-12
  • Spring?boot2.0?實(shí)現(xiàn)日志集成的方法(2)

    Spring?boot2.0?實(shí)現(xiàn)日志集成的方法(2)

    這篇文章主要介紹了Spring?boot2.0?實(shí)現(xiàn)日志集成的方法,上一章講解了spring?boot日志簡(jiǎn)單集成,這篇我們將日志進(jìn)行分類,常規(guī)日志、異常日志、監(jiān)控日志等,需要將日志輸出到不同的文件,具體內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • Spring Boot 整合 SSE的高級(jí)實(shí)踐(Server-Sent Events)

    Spring Boot 整合 SSE的高級(jí)實(shí)踐(Server-Sent Even

    SSE(Server-Sent Events)是一種基于HTTP協(xié)議的單向通信機(jī)制,允許服務(wù)器向?yàn)g覽器持續(xù)發(fā)送實(shí)時(shí)更新,這篇文章主要介紹了Spring Boot 整合 SSE的高級(jí)實(shí)踐(Server-Sent Events),需要的朋友可以參考下
    2025-04-04
  • java懶惰評(píng)估實(shí)現(xiàn)方法

    java懶惰評(píng)估實(shí)現(xiàn)方法

    這篇文章主要介紹了java懶惰評(píng)估如何實(shí)現(xiàn)的相關(guān)內(nèi)容及實(shí)例,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-05-05
  • OGNL表達(dá)式基本語(yǔ)法與用法詳解

    OGNL表達(dá)式基本語(yǔ)法與用法詳解

    這篇文章主要介紹了OGNL表達(dá)式基本語(yǔ)法與用法詳解,具有一定參考價(jià)值。需要的朋友可以了解下。
    2017-09-09
  • Java對(duì)時(shí)間的簡(jiǎn)單操作實(shí)例

    Java對(duì)時(shí)間的簡(jiǎn)單操作實(shí)例

    這篇文章主要介紹了Java對(duì)時(shí)間的簡(jiǎn)單操作,實(shí)例分析了針對(duì)java.util.Date的各類常見(jiàn)操作,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01

最新評(píng)論

岚皋县| 鄢陵县| 清徐县| 浦北县| 中山市| 太白县| 肇东市| 上高县| 临颍县| 昌乐县| 新安县| 卢湾区| 承德市| 隆林| 普定县| 那曲县| 什邡市| 德庆县| 鄢陵县| 博野县| 博湖县| 新安县| 高碑店市| 阿拉善左旗| 慈溪市| 大城县| 罗源县| 临汾市| 巴中市| 皋兰县| 启东市| 五莲县| 平谷区| 金山区| 永修县| 越西县| 哈巴河县| 景德镇市| 大埔区| 长顺县| 罗江县|