Mybatis-plus中QueryWrapper的多種用法小結(jié)
一、 MyBatis-Plus
官網(wǎng)地址:https://baomidou.com/
MyBatis-Plus (opens new window)(簡(jiǎn)稱 MP)是一個(gè) MyBatis (opens new window)的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生。
mp的簡(jiǎn)單使用
現(xiàn)有一張 User 表,其表結(jié)構(gòu)如下:
其對(duì)應(yīng)的數(shù)據(jù)庫(kù) Schema 腳本如下:
DROP TABLE IF EXISTS user; CREATE TABLE user ( id BIGINT(20) NOT NULL COMMENT '主鍵ID', name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名', age INT(11) NULL DEFAULT NULL COMMENT '年齡', email VARCHAR(50) NULL DEFAULT NULL COMMENT '郵箱', PRIMARY KEY (id) );
其對(duì)應(yīng)的數(shù)據(jù)庫(kù) Data 腳本如下:
DELETE FROM user; INSERT INTO user (id, name, age, email) VALUES (1, 'Jone', 18, 'test1@baomidou.com'), (2, 'Jack', 20, 'test2@baomidou.com'), (3, 'Tom', 28, 'test3@baomidou.com'), (4, 'Sandy', 21, 'test4@baomidou.com'), (5, 'Billie', 24, 'test5@baomidou.com');
初始化工程
創(chuàng)建一個(gè)空的 Spring Boot 工程(工程將以 H2 作為默認(rèn)數(shù)據(jù)庫(kù)進(jìn)行演示)
提示
可以使用 Spring Initializer (opens new window)快速初始化一個(gè) Spring Boot 工程
#添加依賴
引入 Spring Boot Starter 父工程:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5+ 版本</version> <relativePath/> </parent>
引入 spring-boot-starter、spring-boot-starter-test、mybatis-plus-boot-starter、h2 依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies>
配置
在 application.yml 配置文件中添加 H2 數(shù)據(jù)庫(kù)的相關(guān)配置:
# DataSource Config spring: datasource: driver-class-name: org.h2.Driver schema: classpath:db/schema-h2.sql username: root password: test sql: init: schema-locations: classpath:db/schema-h2.sql data-locations: classpath:db/data-h2.sql
在 Spring Boot 啟動(dòng)類中添加 @MapperScan 注解,掃描 Mapper 文件夾:
@SpringBootApplication @MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
#編碼
編寫實(shí)體類 User.java(此處使用了 Lombok (opens new window)簡(jiǎn)化代碼)
@Data public class User { private Long id; private String name; private Integer age; private String email; }
編寫 Mapper 包下的 UserMapper接口
public interface UserMapper extends BaseMapper<User> {<!--{C}%3C!%2D%2D%20%2D%2D%3E-->}
開始使用
添加測(cè)試類,進(jìn)行功能測(cè)試:
@SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { System.out.println(("----- selectAll method test ------")); List<User> userList = userMapper.selectList(null); Assert.assertEquals(5, userList.size()); userList.forEach(System.out::println); } }
提示
UserMapper 中的 selectList() 方法的參數(shù)為 MP 內(nèi)置的條件封裝器 Wrapper,所以不填寫就是無(wú)任何條件
控制臺(tái)輸出:
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
提示
完整的代碼示例請(qǐng)移步:Spring Boot 快速啟動(dòng)示例 (opens new window)| Spring MVC 快速啟動(dòng)示例(opens new window)
小結(jié)
通過以上幾個(gè)簡(jiǎn)單的步驟,我們就實(shí)現(xiàn)了 User 表的 CRUD 功能,甚至連 XML 文件都不用編寫!
從以上步驟中,我們可以看到集成MyBatis-Plus非常的簡(jiǎn)單,只需要引入 starter 工程,并配置 mapper 掃描路徑即可。
但 MyBatis-Plus 的強(qiáng)大遠(yuǎn)不止這些功能,JAVA開發(fā)愛好者在使用mybatis-plus的時(shí)候,經(jīng)常使用的是QueryWrapper,QueryWrapper繼承自 AbstractWrapper ,自身的內(nèi)部屬性 entity 也用于生成 where 條件及 LambdaQueryWrapper, 可以通過 new QueryWrapper().lambda() 方法獲取,下面總結(jié)了幾種不同的用法:
二、MP–>QueryWrapper 5種更新語(yǔ)句不同寫法:
/** * 第一種,常用寫法 */ public void updateUser1(){ //方式一: User user = new User(); user.setAge(29); user.setEmail("111111111111.com"); QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.eq("name","Tom"); update(user,queryWrapper); } /** * 第二種 UpdateWrapper */ public void updateUser2(){ update(null,new UpdateWrapper<User>().set("age",29) .set("email","22222222222222.com").eq("name","Tom")); } /** * 第三種實(shí)體類+LambdaUpdateWrapper */ public void updateUser3(){ User user = new User(); user.setAge(29); user.setEmail("3333333.com"); update(user,new LambdaUpdateWrapper<User>( ).eq(User::getName,"Tom")); } /** * 第四種 LambdaUpdateWrapper */ public void updateUser4(){ update(null,new LambdaUpdateWrapper<User>().set(User::getAge,29) .set(User::getEmail,"4444444.com").eq(User::getName,"Tom")); } /** * 第五種:Wrappers */ public void updateUser5(){ update(null,Wrappers.<User>update().lambda() .set(User::getAge,29) .set(User::getEmail,"555555555.com") .eq(User::getName,"Tom")); } /** * 第五種:實(shí)體類+Wrappers */ public void updateUser6(){ User user = new User(); user.setAge(29); user.setEmail("6666666.com"); update(user,Wrappers.<User>update().lambda() .eq(User::getName,"Tom")); }
三、三種查詢語(yǔ)句不同寫法:
/** * 第一種查詢 * @return */ public List<User> selectListUser(){ List<User> list = baseMapper.selectList( Wrappers.<User>lambdaQuery() .eq(User::getName, "Tom")); return list; } /** * 第二種查詢 * @return */ public IPage<Map<String, Object>> listPageUser(){ Page<Map<String, Object>> mapPage = baseMapper.selectMapsPage( new Page<>(1, 5), Wrappers.<User>query() .orderByAsc("id")); return mapPage; } /** * 第三種查詢 * @return */ public List<User> listUser(){ LambdaQueryWrapper<User> lambdaQueryWrapper = new QueryWrapper<User>() .lambda(); List<User> users = baseMapper.selectList(lambdaQueryWrapper); return users; }
通過上面代碼的演示,我們對(duì)使用MP的條件構(gòu)造器進(jìn)行數(shù)據(jù)庫(kù)的delete和insert操作有了更深一步的理解。
到此這篇關(guān)于Mybatis-plus中QueryWrapper的多種用法小結(jié)的文章就介紹到這了,更多相關(guān)Mybatis-plus QueryWrapper用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis-plus自帶QueryWrapper自定義sql實(shí)現(xiàn)復(fù)雜查詢實(shí)例詳解
- Mybatis-plus動(dòng)態(tài)條件查詢QueryWrapper的使用案例
- MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用詳解
- 基于mybatis-plus QueryWrapper 排序的坑
- mybatis-plus QueryWrapper and or 連用并且實(shí)現(xiàn)分頁(yè)
- mybatis-plus QueryWrapper 添加limit方式
- Mybatis-Plus 條件構(gòu)造器 QueryWrapper 的基本用法
- MyBatis-Plus實(shí)現(xiàn)2種分頁(yè)方法(QueryWrapper查詢分頁(yè)和SQL查詢分頁(yè))
- mybatis-plus QueryWrapper自定義查詢條件的實(shí)現(xiàn)
相關(guān)文章
淺談StringEntity 和 UrlEncodedFormEntity之間的區(qū)別
這篇文章主要介紹了StringEntity 和 UrlEncodedFormEntity之間的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Spring Boot使用GridFS實(shí)現(xiàn)文件的上傳和下載方式
這篇文章主要介紹了Spring Boot使用GridFS實(shí)現(xiàn)文件的上傳和下載方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Sentinel的熔斷降級(jí)、資源規(guī)則詳解與實(shí)例
這篇文章主要介紹了Sentinel的熔斷降級(jí)、資源規(guī)則詳解與實(shí)例,Sentinel是阿里巴巴開源的一款流量控制和熔斷降級(jí)的框架,它主要用于保護(hù)分布式系統(tǒng)中的服務(wù)穩(wěn)定性,Sentinel通過對(duì)服務(wù)進(jìn)行流量控制和熔斷降級(jí),可以有效地保護(hù)系統(tǒng)的穩(wěn)定性,需要的朋友可以參考下2023-09-09mybatis-plus 查詢時(shí)排除字段方法的兩種方法
我們?cè)陂_發(fā)應(yīng)用時(shí),在某些應(yīng)用場(chǎng)景下查詢有時(shí)需要排除某些字段,本文主要介紹了兩種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09