mybatis plus條件構(gòu)造器queryWrapper、updateWrapper
注明:上篇文章介紹了springboot+mybatis-plus通用CRUD的用法,這篇文章我們來(lái)介紹一下mybatis-plus強(qiáng)大的條件構(gòu)造器。mybatis-plus的版本為最新版3.0.3 。條件構(gòu)造器咱們講述queryWrapper和updateWrapper的用法、關(guān)系、以及強(qiáng)大之處。
首先在這里寫(xiě)下官方文檔的鏈接位置,官方文檔說(shuō)的很詳細(xì)。如果還想知道在項(xiàng)目中的具體用法請(qǐng)往下看。
一、條件構(gòu)造器關(guān)系介紹
介紹 :
1.上圖綠色框?yàn)槌橄箢恆bstract
2.藍(lán)色框?yàn)檎lass類,可new對(duì)象
3.黃色箭頭指向?yàn)楦缸宇愱P(guān)系,箭頭指向?yàn)楦割?br />
wapper介紹 :
1.Wrapper : 條件構(gòu)造抽象類,最頂端父類,抽象類中提供4個(gè)方法西面貼源碼展示
2.AbstractWrapper : 用于查詢條件封裝,生成 sql 的 where 條件
3.AbstractLambdaWrapper : Lambda 語(yǔ)法使用 Wrapper統(tǒng)一處理解析 lambda 獲取 column。
4.LambdaQueryWrapper :看名稱也能明白就是用于Lambda語(yǔ)法使用的查詢Wrapper
5.LambdaUpdateWrapper : Lambda 更新封裝Wrapper
6.QueryWrapper : Entity 對(duì)象封裝操作類,不是用lambda語(yǔ)法
7.UpdateWrapper : Update 條件封裝,用于Entity對(duì)象更新操作
二、項(xiàng)目實(shí)例
在這里我以QueryWrapper和UpdateWrapper為例,進(jìn)行測(cè)試講解。我會(huì)在上篇博客原有的基礎(chǔ)上進(jìn)行測(cè)試,如果不喜歡搭建項(xiàng)目的可直接下載我上個(gè)項(xiàng)目,上個(gè)項(xiàng)目的博客對(duì)應(yīng)上個(gè)項(xiàng)目的講解
上圖表格為條件構(gòu)造器使用中的各個(gè)方法格式和說(shuō)明,如有不懂可參考官方文檔內(nèi)容
構(gòu)造器條件
package com.lqf.crud; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.lqf.crud.bean.crm.User; import com.lqf.crud.dao.crm.UserMapper; import com.sun.org.apache.xerces.internal.util.EntityResolverWrapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.session.RowBounds; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.jsf.el.WebApplicationContextFacesELResolver; import javax.naming.Name; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest public class QueryWrapperTests { @Autowired private UserMapper mapper; /** * <p> * 根據(jù)根據(jù) entity 條件,刪除記錄,QueryWrapper實(shí)體對(duì)象封裝操作類(可以為 null) * 下方獲取到queryWrapper后刪除的查詢條件為name字段為null的and年齡大于等于12的and email字段不為null的 * 同理寫(xiě)法條件添加的方式就不做過(guò)多介紹了。 * </p> */ @Test public void delete() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper .isNull("name") .ge("age", 12) .isNotNull("email"); int delete = mapper.delete(queryWrapper); System.out.println("delete return count = " + delete); } /** * <p> * 根據(jù) entity 條件,查詢一條記錄, * 這里和上方刪除構(gòu)造條件一樣,只是seletOne返回的是一條實(shí)體記錄,當(dāng)出現(xiàn)多條時(shí)會(huì)報(bào)錯(cuò) * </p> */ @Test public void selectOne() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name", "lqf"); User user = mapper.selectOne(queryWrapper); System.out.println(user); } /** * <p> * 根據(jù) Wrapper 條件,查詢總記錄數(shù) * </p> * * @param queryWrapper 實(shí)體對(duì)象 */ @Test public void selectCount() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name", "lqf"); Integer count = mapper.selectCount(queryWrapper); System.out.println(count); } /** * <p> * 根據(jù) entity 條件,查詢?nèi)坑涗? * </p> * * @param queryWrapper 實(shí)體對(duì)象封裝操作類(可以為 null)為null查詢?nèi)? */ @Test public void selectList() { List<User> list = mapper.selectList(null); System.out.println(list); } /** * <p> * 根據(jù) Wrapper 條件,查詢?nèi)坑涗? * </p> * * @param queryWrapper 實(shí)體對(duì)象封裝操作類(可以為 null) */ @Test public void selectMaps() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.isNotNull("name"); List<Map<String, Object>> maps = mapper.selectMaps(queryWrapper); for (Map<String, Object> map : maps) { System.out.println(map); } } /** * 打印結(jié)果 * {name=lqf, id=1046282328366391406, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391407, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391408, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391409, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391410, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391411, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391412, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391413, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391414, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391415, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391416, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391417, age=12, email=lqf@163.com, status=false} * {name=lqf, id=1046282328366391418, age=12, email=lqf@163.com, status=false} * json類型的鍵值對(duì)模式 */ /** * <p> * 根據(jù) entity 條件,查詢?nèi)坑涗洠ú⒎?yè)) * </p> * * @param page 分頁(yè)查詢條件(可以為 RowBounds.DEFAULT) * @param queryWrapper 實(shí)體對(duì)象封裝操作類(可以為 null) */ @Test public void selectPage() { Page<User> page = new Page<>(1, 5); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); IPage<User> userIPage = mapper.selectPage(page, queryWrapper); System.out.println(userIPage); } /** * 打印結(jié)果 * ==> Preparing: SELECT COUNT(1) FROM user * ==> Parameters: * <== Columns: COUNT(1) * <== Row: 100 * ==> Preparing: SELECT id,name,age,email,status FROM user LIMIT 0,5 * ==> Parameters: * <== Columns: id, name, age, email, status * <== Row: 1046282328366391319, lqf, 12, lqf@163.com, 0 * <== Row: 1046282328366391320, lqf, 12, lqf@163.com, 0 * <== Row: 1046282328366391321, lqf, 12, lqf@163.com, 0 * <== Row: 1046282328366391322, lqf, 12, lqf@163.com, 0 * <== Row: 1046282328366391323, lqf, 12, lqf@163.com, 0 * <== Total: 5 * * * 這里需要在項(xiàng)目中加入分頁(yè)插件 * @Bean * public PaginationInterceptor paginationInterceptor() { * return new PaginationInterceptor(); * } */ /** * <p> * 根據(jù) Wrapper 條件,查詢?nèi)坑涗洠ú⒎?yè)) * </p> * * @param page 分頁(yè)查詢條件 * @param queryWrapper 實(shí)體對(duì)象封裝操作類 */ @Test public void selectMapsPage() { Page<User> page = new Page<>(1, 5); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); IPage<Map<String, Object>> mapIPage = mapper.selectMapsPage(page, queryWrapper); System.out.println(mapIPage); } /** * 和上個(gè)分頁(yè)同理只是返回類型不同 */ /** * <p> * 根據(jù) whereEntity 條件,更新記錄 * </p> * * @param entity 實(shí)體對(duì)象 (set 條件值,不能為 null) * @param updateWrapper 實(shí)體對(duì)象封裝操作類(可以為 null,里面的 entity 用于生成 where 語(yǔ)句) */ @Test public void update() { //修改值 User user = new User(); user.setStatus(true); user.setName("zhangsan"); //修改條件s UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>(); userUpdateWrapper.eq("name", "lqf"); int update = mapper.update(user, userUpdateWrapper); System.out.println(update); } /** * 打印結(jié)果 * ==> Preparing: UPDATE user SET name=?, status=? WHERE name = ? * ==> Parameters: zhangsan(String), true(Boolean), lqf(String) * <== Updates: 100 * Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@56a4f272] * 100 * 2018-10-02 15:08:03.928 INFO 7972 --- [ Thread-2] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@37313c65: startup date [Tue Oct 02 15:08:00 CST 2018]; root of context hierarchy * 2018-10-02 15:08:03.937 INFO 7972 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... * 2018-10-02 15:08:04.053 INFO 7972 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. * * Process finished with exit code 0 */ }
上方代碼對(duì)通過(guò)構(gòu)造器條件進(jìn)行的查詢、刪除、修改進(jìn)行是演示,構(gòu)造器方法沒(méi)有做過(guò)多演示,但是所有的構(gòu)造器方法同理使用,如果還有不會(huì)用的點(diǎn)開(kāi)看官方文檔查看并按照上方例子使用即可。
到此這篇關(guān)于mybatis plus條件構(gòu)造器queryWrapper、updateWrapper的文章就介紹到這了,更多相關(guān)queryWrapper、updateWrapper內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中@RequestBody不能和Multipart同時(shí)傳遞的問(wèn)題解決
本文主要介紹了SpringBoot中@RequestBody不能和Multipart同時(shí)傳遞的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04基于令牌桶的限流器注解的簡(jiǎn)單實(shí)現(xiàn)詳解
令牌桶算法是一種常用的流量控制算法,用于限制請(qǐng)求或事件的發(fā)生速率,這篇文章主要介紹了如何基于令牌桶實(shí)現(xiàn)限流器注解,需要的可以參考一下2023-08-08Mybatis自定義SQL的關(guān)系映射、分頁(yè)、排序功能的實(shí)現(xiàn)
這篇文章主要介紹了Mybatis自定義SQL的關(guān)系映射、分頁(yè)、排序功能的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01javax.net.ssl.SSLException: java.lang.RuntimeException: Coul
這篇文章主要介紹了javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair 解決方法,有需要的朋友們可以學(xué)習(xí)下。2019-08-08詳解Java如何優(yōu)雅的調(diào)用dubbo同時(shí)不使用其它jar包
這篇文章主要介紹了如何在不使用他人jar包的情況下優(yōu)雅的進(jìn)行dubbo調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02