Mybatis plus結(jié)合springboot使用
使用 MyBatis Plus 結(jié)合 Spring Boot 可以極大地簡(jiǎn)化數(shù)據(jù)庫(kù)操作。MyBatis Plus 是 MyBatis 的增強(qiáng)工具,旨在簡(jiǎn)化開(kāi)發(fā)、提高效率。
1. 添加依賴(lài)
首先,在 pom.xml 文件中添加 MyBatis Plus 和 Spring Boot 的相關(guān)依賴(lài)。
<dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Starter Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> <!-- Lombok for getter/setter/toString/etc. --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> <scope>provided</scope> </dependency> <!-- Spring Boot Starter Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2. 配置數(shù)據(jù)庫(kù)連接
在 application.properties 或 application.yml 文件中配置數(shù)據(jù)庫(kù)連接信息。
application.properties spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # MyBatis Plus 配置 mybatis-plus.mapper-locations=classpath*:mapper/*.xml mybatis-plus.type-aliases-package=com.yourpackage.entity application.yml spring: datasource: url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC username: your_username password: your_password driver-class-name: com.mysql.cj.jdbc.Driver # MyBatis Plus 配置 mybatis-plus: mapper-locations: classpath*:mapper/*.xml type-aliases-package: com.yourpackage.entity
3. 創(chuàng)建實(shí)體類(lèi)
使用 Lombok 注解來(lái)減少樣板代碼。
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("user") public class User { @TableId private Long id; private String name; private Integer age; private String email; }
4. 創(chuàng)建 Mapper 接口
繼承 BaseMapper 接口,MyBatis Plus 提供了許多便捷的方法。
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseMapper<User> { }
5. 創(chuàng)建 Service 層
創(chuàng)建服務(wù)層來(lái)處理業(yè)務(wù)邏輯。
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; @Service public class UserService extends ServiceImpl<UserMapper, User> { // 可以添加自定義的業(yè)務(wù)方法 }
6. 創(chuàng)建 Controller 層
創(chuàng)建控制器層來(lái)處理 HTTP 請(qǐng)求。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping public List<User> getAllUsers() { return userService.list(); } @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getById(id); } @PostMapping public boolean addUser(@RequestBody User user) { return userService.save(user); } @PutMapping("/{id}") public boolean updateUser(@PathVariable Long id, @RequestBody User user) { user.setId(id); return userService.updateById(user); } @DeleteMapping("/{id}") public boolean deleteUser(@PathVariable Long id) { return userService.removeById(id); } }
7. 啟動(dòng)類(lèi)
確保啟動(dòng)類(lèi)位于正確的包路徑下,以便 Spring Boot 自動(dòng)掃描到所有組件。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
8. 測(cè)試
啟動(dòng)應(yīng)用并測(cè)試各個(gè)接口是否正常工作。
mvn spring-boot:run
快捷方式和最佳實(shí)踐
- Lombok:使用 Lombok 可以大大減少樣板代碼,如 getter、setter、toString 等。
- 全局異常處理:使用 @ControllerAdvice 注解創(chuàng)建全局異常處理器,統(tǒng)一處理異常。
- 分頁(yè)查詢(xún):MyBatis Plus 提供了內(nèi)置的分頁(yè)支持,使用 Page 對(duì)象即可輕松實(shí)現(xiàn)分頁(yè)查詢(xún)。
- 事務(wù)管理:使用 @Transactional 注解管理事務(wù),確保數(shù)據(jù)的一致性和完整性。
- 日志記錄:使用 logback 或 log4j2 配置日志記錄,方便調(diào)試和監(jiān)控。
到此這篇關(guān)于Mybatis plus結(jié)合springboot使用的文章就介紹到這了,更多相關(guān)Mybatis plus結(jié)合springboot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot整合mybatis-plus使用pageHelper進(jìn)行分頁(yè)(使用步驟)
- SpringBoot與MyBatis-Plus的高效集成方式
- SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)的項(xiàng)目實(shí)踐
- springboot集成mybatis-plus全過(guò)程
- springboot如何使用MybatisPlus
- SpringBoot整合Mybatis-Plus實(shí)現(xiàn)關(guān)聯(lián)查詢(xún)
- SpringBoot3.0集成MybatisPlus的實(shí)現(xiàn)方法
相關(guān)文章
springboot項(xiàng)目打成jar包后無(wú)法獲取static下的靜態(tài)資源文件的問(wèn)題分析
這篇文章主要介紹了springboot項(xiàng)目打成jar包后無(wú)法獲取static下的靜態(tài)資源文件的問(wèn)題分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08深入解析Java的設(shè)計(jì)模式編程中的模板方法模式
這篇文章主要介紹了深入解析Java的設(shè)計(jì)模式編程中的模板方法模式, 模版方法模式由一個(gè)抽象類(lèi)和一個(gè)(或一組)實(shí)現(xiàn)類(lèi)通過(guò)繼承結(jié)構(gòu)組成,需要的朋友可以參考下2016-02-02mybatis查詢(xún)語(yǔ)句揭秘之參數(shù)解析
這篇文章主要給大家介紹了關(guān)于mybatis查詢(xún)語(yǔ)句之參數(shù)解析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04SpringBoot項(xiàng)目中使用@Scheduled讀取動(dòng)態(tài)參數(shù)
這篇文章主要介紹了SpringBoot項(xiàng)目中使用@Scheduled讀取動(dòng)態(tài)參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11netty-grpc一次DirectByteBuffer內(nèi)存泄露問(wèn)題
這篇文章主要介紹了netty-grpc一次DirectByteBuffer內(nèi)存泄露問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12