SpringBoot配置MyBatis-Plus實(shí)現(xiàn)增刪查改
1 MyBatis-Plus
MyBatis-Plus (opens new window)(簡(jiǎn)稱 MP)是一個(gè)MyBatis (opens new window)的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生。
特性:
(1)無(wú)侵入:只做增強(qiáng)不做改變,引入它不會(huì)對(duì)現(xiàn)有工程產(chǎn)生影響,如絲般順滑。
(2)損耗?。?jiǎn)?dòng)即會(huì)自動(dòng)注入基本 CURD,性能基本無(wú)損耗,直接面向?qū)ο蟛僮鳌?/p>
(3)強(qiáng)大的 CRUD 操作:內(nèi)置通用 Mapper、通用 Service,僅僅通過(guò)少量配置即可實(shí)現(xiàn)單表大部分 CRUD 操作,更有強(qiáng)大的條件構(gòu)造器,滿足各類使用需求。
(4)支持 Lambda 形式調(diào)用:通過(guò) Lambda 表達(dá)式,方便的編寫(xiě)各類查詢條件,無(wú)需再擔(dān)心字段寫(xiě)錯(cuò)。
(5)支持主鍵自動(dòng)生成:支持多達(dá) 4 種主鍵策略(內(nèi)含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解決主鍵問(wèn)題。
(6)支持 ActiveRecord 模式:支持 ActiveRecord 形式調(diào)用,實(shí)體類只需繼承 Model 類即可進(jìn)行強(qiáng)大的 CRUD 操作。
(7)支持自定義全局通用操作:支持全局通用方法注入( Write once, use anywhere )。
(8)內(nèi)置代碼生成器:采用代碼或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 層代碼,支持模板引擎,更有超多自定義配置等您來(lái)使用。
(9)內(nèi)置分頁(yè)插件:基于 MyBatis 物理分頁(yè),開(kāi)發(fā)者無(wú)需關(guān)心具體操作,配置好插件之后,寫(xiě)分頁(yè)等同于普通 List 查詢。
(10)分頁(yè)插件支持多種數(shù)據(jù)庫(kù):支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多種數(shù)據(jù)庫(kù)。
(11)內(nèi)置性能分析插件:可輸出 SQL 語(yǔ)句以及其執(zhí)行時(shí)間,建議開(kāi)發(fā)測(cè)試時(shí)啟用該功能,能快速揪出慢查詢。
(12)內(nèi)置全局?jǐn)r截插件:提供全表 delete 、 update 操作智能分析阻斷,也可自定義攔截規(guī)則,預(yù)防誤操作。
2 Maven依賴
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!--MySQL數(shù)據(jù)庫(kù)連接驅(qū)動(dòng)--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency>
3 Spring Boot配置
#數(shù)據(jù)庫(kù)連接池設(shè)置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 #mybatis的相關(guān)配置 mybatis.mapper-locations=classpath:mapper/*.xml
4 UserEntity
用戶信息實(shí)體類。
package com.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
/**
* 用戶信息實(shí)體類
*/
@Data
@TableName("users")
public class UserEntity {
/**
* 用戶名
*/
@TableField("username")
@TableId
private String username;
/**
* 昵稱
*/
@TableField("pickname")
private String pickname;
/**
* 密碼
*/
@TableField("password")
private String password;
/**
* 性別
*/
@TableField("sex")
private String sex;
}
5 UserMapper
用戶信息dao層。
package com.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* 用戶信息dao層
*/
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {
}
6 Service(業(yè)務(wù)邏輯層)
6.1 UserService
package com.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.entity.UserEntity;
public interface UserService extends IService<UserEntity> {
}
6.2 UserServiceImpl
package com.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.UserEntity;
import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserEntity> implements UserService {
}
7 UserController
調(diào)試代碼。
package com.controller;
import com.entity.UserEntity;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* 獲取所有用戶數(shù)據(jù)
*
* @return
*/
@GetMapping("/getList")
public List<UserEntity> getList() {
return userService.list();
}
/**
* 插入用戶數(shù)據(jù)
*
* @return
*/
@PostMapping("/create")
public boolean create(@RequestBody UserEntity userEntity) {
return userService.save(userEntity);
}
/**
* 更新用戶數(shù)據(jù)
*
* @return
*/
@PutMapping("/update")
public boolean update(@RequestBody UserEntity userEntity) {
return userService.updateById(userEntity);
}
/**
* 刪除用戶數(shù)據(jù)
*
* @return
*/
@DeleteMapping("/delete/{username}")
public boolean delete(@PathVariable("username") String username) {
return userService.removeById(username);
}
}
8 調(diào)試結(jié)果
8.1 查詢數(shù)據(jù)

8.2 新增數(shù)據(jù)

8.3 更新數(shù)據(jù)

8.4 刪除數(shù)據(jù)

到此這篇關(guān)于SpringBoot配置MyBatis-Plus實(shí)現(xiàn)增刪查改的文章就介紹到這了,更多相關(guān)SpringBoot MyBatis-Plus增刪查改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)Map集合二級(jí)聯(lián)動(dòng)示例
Java實(shí)現(xiàn)Map集合二級(jí)聯(lián)動(dòng)示例,需要的朋友可以參考下2014-03-03
解決feignclient調(diào)用服務(wù),傳遞的中文數(shù)據(jù)成???問(wèn)題
這篇文章主要介紹了解決feignclient調(diào)用服務(wù),傳遞的中文數(shù)據(jù)成???問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
java連接zookeeper的實(shí)現(xiàn)示例
ZooKeeper官方提供了Java API,可以通過(guò)Java代碼來(lái)連接zookeeper服務(wù)進(jìn)行操作,本文就來(lái)介紹一下java連接zookeeper的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
詳解IntelliJ IDEA中TortoiseSVN修改服務(wù)器地址的方法
這篇文章主要介紹了詳解IntelliJ IDEA中TortoiseSVN修改服務(wù)器地址的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
SpringCloud組件之Eureka Server詳細(xì)啟動(dòng)過(guò)程及說(shuō)明
這篇文章主要介紹了SpringCloud組件之Eureka Server詳細(xì)啟動(dòng)過(guò)程及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
詳解使用Spring Security進(jìn)行自動(dòng)登錄驗(yàn)證
本篇文章主要介紹了詳解使用Spring Security進(jìn)行自動(dòng)登錄驗(yàn)證,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn)
本文主要介紹了SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

