QueryWrapper
wrapper介绍
- Wrapper : 条件构造抽象类,最顶端父类
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- QueryWrapper : Entity 对象封装操作类,不是用lambda语法
- UpdateWrapper : Update 条件封装,用于Entity对象更新操作
- AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
- LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper
- LambdaUpdateWrapper : Lambda 更新封装Wrapper
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- 如果想进行复杂条件查询,那么需要使用条件构造器 Wapper,涉及到如下方法
- 拼凑条件相关关键字
条件查询 - 基本多条件查询
@Testpublic void testQueryWrapper(){// todo 拼凑条件QueryWrapper queryWrapper = new QueryWrapper();// todo 等值查询queryWrapper.eq("password","1234");// todo 模糊查询queryWrapper.like("cname","o");// todo 范围查询 大于等于queryWrapper.ge("money",800);// todo 范围查询 小于等于queryWrapper.le("money",1000);//todo 条件查询List<Customer> list = customerMapper.selectList(queryWrapper);list.forEach(System.out::println);}
- 条件判断
@Testpublic void findCondition2() {Customer customer = new Customer();customer.setPassword("777");customer.setCname("888");customer.setIdList(Arrays.asList(2,3,4));customer.setCid(3);//条件查询QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();// 1) 等值查询queryWrapper.eq( customer.getPassword()!=null ,"password", customer.getPassword());// 2) 模糊查询queryWrapper.like(customer.getCname() != null , "cname",customer.getCname());// 3) in语句queryWrapper.in(customer.getIdList() != null , "cid",customer.getIdList());// 4) 大于等于queryWrapper.ge(customer.getCid() != null , "cid" , customer.getCid());//查询List<Customer> list = customerMapper.selectList(queryWrapper);//list.forEach(customer-> System.out.println(customer));list.forEach(System.out::println);}
- 条件更新
@Testpublic void testUpdateByQueryMapper(){//1.todo 更新数据Customer customer = new Customer();customer.setVersion(1);//2.todo 更新条件UpdateWrapper<Customer> updateWrapper = new UpdateWrapper<>();updateWrapper.in("cid",1,2,3);//3.todo 更新int update = customerMapper.update(customer, updateWrapper);System.out.println(update);}
分页
主体插件: MybatisPlusInterceptor,该插件内部插件集:
- 分页插件: PaginationInnerInterceptor
- 多租户插件: TenantLineInnerInterceptor
- 动态表名插件: DynamicTableNameInnerInterceptor
- 乐观锁插件: OptimisticLockerInnerInterceptor
- sql性能规范插件: IllegalSQLInnerInterceptor
- 防止全表更新与删除插件: BlockAttackInnerInterceptor
配置类
package com.czxy.mp.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MybatisPlusConfig {/*** 配置插件* @return*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();// 分页插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return mybatisPlusInterceptor;}/*** 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)* @return*/@Beanpublic ConfigurationCustomizer configurationCustomizer() {return configuration -> configuration.setUseDeprecatedExecutor(false);}
}
分页
@Testpublic void testPage(){// 分页数据int pageNum = 1;int pageSize = 3;Page<Customer> page = new Page<>(pageNum , pageSize);page.setSearchCount(true);// 查询customerMapper.selectPage(page, null);// 分页数据System.err.println("当前页码:" + page.getCurrent());System.err.println("每页显示记录数:" + page.getSize());System.err.println("总页数:" + page.getPages());System.err.println("总记录数:" + page.getTotal());System.err.println("是否有下一页:" + page.hasNext());System.err.println("是否有上一页:" + page.hasPrevious());// 分页数据列表page.getRecords().forEach(System.err::println);}
常见注解
表名注解:@TableName
主键注解:@TableId
字段注解(非主键) : @TableField
常见配置
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出日志map-underscore-to-camel-case: true #驼峰命名global-config:db-config:id-type: auto #全局配置,id自动增强table-prefix: tmp_ #表名前缀type-aliases-package: com.czxy.mp.domain #别名包扫描路径mapper-locations: classpath*:/mapper/**/*.xml #映射文件位置