springboot + mybatis plus强大的条件构造器queryWrapper、updateWrapper
(五)springboot + mybatis plus强大的条件构造器queryWrapper、updateWrapper
2018年10月02日 15:23:04 青蛙与大鹅 阅读数 57178 标签: springboot UpdateWrapper QueryWrapper AbstractWrapper 更多 个人分类: 日积月累 java 程序人生 mybatis plus springboot 所属专栏: mybatis-plus从入门到精通</div> </div> <div class="operating"> </div> </div> </div> </div> <article class="baidu_pl"> <div id="article_content" class="article_content clearfix" data-track-click="{"mod":"popu_307","con":",https://blog.csdn.net/m0_37034294/article/details/82917234"}"> <div class="article-copyright"> <svg class="icon" title="CSDN认证原创" aria-hidden="true" style="width:53px; height: 18px; vertical-align: -4px;"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#CSDN_Cert"></use> </svg> 版权声明: https://blog.csdn.net/m0_37034294/article/details/82917234 </div> <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-cd6c485e8b.css"> <div id="content_views" class="markdown_views prism-tomorrow-night"> <!-- flowchart 箭头图标 勿删 --> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path> </svg> <p>注明:上篇文章介绍了springboot+mybatis-plus通用CRUD的用法,这篇文章我们来介绍一下mybatis-plus强大的条件构造器。mybatis-plus的版本为最新版3.0.3 。条件构造器咱们讲述queryWrapper和updateWrapper的用法、关系、以及强大之处。</p>
首先在这里写下官方文档的链接位置,官方文档说的很详细。如果还想知道在项目中的具体用法请往下看。
一、条件构造器关系介绍
介绍 :
- 上图绿色框为抽象类abstract
- 蓝色框为正常class类,可new对象
- 黄色箭头指向为父子类关系,箭头指向为父类
wapper介绍 :
- Wrapper : 条件构造抽象类,最顶端父类,抽象类中提供4个方法西面贴源码展示
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
- LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper
- LambdaUpdateWrapper : Lambda 更新封装Wrapper
- QueryWrapper : Entity 对象封装操作类,不是用lambda语法
- UpdateWrapper : Update 条件封装,用于Entity对象更新操作
二、项目实例
在这里我以QueryWrapper和UpdateWrapper为例,进行测试讲解。我会在上篇博客原有的基础上进行测试,如果不喜欢搭建项目的可直接下载我上个项目,上个项目的博客对应上个项目的讲解
上图表格为条件构造器使用中的各个方法格式和说明,如有不懂可参考官方文档内容
构造器条件
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> * 根据根据 entity 条件,删除记录,QueryWrapper实体对象封装操作类(可以为 null) * 下方获取到queryWrapper后删除的查询条件为name字段为null的and年龄大于等于12的and email字段不为null的 * 同理写法条件添加的方式就不做过多介绍了。 * </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> * 根据 entity 条件,查询一条记录, * 这里和上方删除构造条件一样,只是seletOne返回的是一条实体记录,当出现多条时会报错 * </p> */ @Test public void selectOne() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name", "lqf"); User user = mapper.selectOne(queryWrapper); System.out.println(user); } /** * <p> * 根据 Wrapper 条件,查询总记录数 * </p> * * @param queryWrapper 实体对象 */ @Test public void selectCount() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name", "lqf"); Integer count = mapper.selectCount(queryWrapper); System.out.println(count); } /** * <p> * 根据 entity 条件,查询全部记录 * </p> * * @param queryWrapper 实体对象封装操作类(可以为 null)为null查询全部 */ @Test public void selectList() { List<User> list = mapper.selectList(null); System.out.println(list); } /** * <p> * 根据 Wrapper 条件,查询全部记录 * </p> * * @param queryWrapper 实体对象封装操作类(可以为 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); } } /** * 打印结果 * {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类型的键值对模式 */ /** * <p> * 根据 entity 条件,查询全部记录(并翻页) * </p> * * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 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); } /** * 打印结果 * ==> 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 * * * 这里需要在项目中加入分页插件 * @Bean * public PaginationInterceptor paginationInterceptor() { * return new PaginationInterceptor(); * } */ /** * <p> * 根据 Wrapper 条件,查询全部记录(并翻页) * </p> * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 */ @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); } /** * 和上个分页同理只是返回类型不同 */ /** * <p> * 根据 whereEntity 条件,更新记录 * </p> * * @param entity 实体对象 (set 条件值,不能为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */ @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); } /** * 打印结果 * ==> 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 */
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
上方代码对通过构造器条件进行的查询、删除、修改进行是演示,构造器方法没有做过多演示,但是所有的构造器方法同理使用,如果还有不会用的点开看官方文档查看并按照上方例子使用即可。
上一篇 :(四) springboot + mybatis plus详细拆解CRUD
下一篇 : (六)springboot + mybatis plus实现多表联查分页3.X版本
</div> <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet"> </div> </article>
- springboot_mybatisplus
- 基于SpringBoot的Mybatis-Plus插件整合
- SpringBoot+JWT+Shiro+MybatisPlus实现Restful快速开发后端脚手架
- Spring-boot整合mybatis-plus
- SpringBoot整合MybatisPlus的简单教程实现(简单整合)
- SpringBoot系列2—整合Mybatis-plus
- SpringBoot整合mybatisplus
- springboot 零xml集成mybatis-plus
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- 基于SpringBoot从零构建博客网站 - 整合lombok和mybatis-plus提高开发效率
- JeeFast是一款基于SpringBoot+Mybatis-Plus+Bootstrap+Vue搭建的JAVA WEB快速开发平台
- Mybatis——plus 条件构造器(EntityWrapper)
- 使用Springboot + Gradle快速整合Mybatis-Plus
- spring-boot-mybatis-plus使用心得
- springboot+mybatisplus
- SpringBoot实践:集成Mybatis-Plus
- SpringBoot +shiro+mybatis plus+spring +redis
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- Springboot-mybatisplus-Swagger
- Spring boot 和 mybatis-plus基础整合