转载自:https://blog.csdn.net/alan_liuyue/article/details/121162237

用到了wrapper,整理资料记录一下,以备后续复习。

目录------------(可点击相应目录直接跳转)

目录

一、条件构造器关系介绍

条件构造器关系介绍 :

 wapper介绍 :

二、项目实例

1、根据主键或者简单的查询条件进行查询

2、MyBatis-Plus还提供了Wrapper条件构造器,具体使用看如下代码:

三、具体使用操作

1、ge、gt、le、lt、isNull、isNotNull 

2、eq、ne

3、between、notBetween

4、allEq

5、like、notLike、likeLeft、likeRight

6、in、notIn、inSql、notinSql、exists、notExists

7、or、and

8、嵌套or、嵌套and

9、orderBy、orderByDesc、orderByAsc 

10、last

11、指定要查询的列

12、set、setSql


一、条件构造器关系介绍

条件构造器关系介绍 :

  •         上图绿色框为抽象类abstract
  •         蓝色框为正常class类,可new对象
  •         黄色箭头指向为父子类关系,箭头指向为父类

 
wapper介绍 :

  • Wrapper : 条件构造抽象类,最顶端父类
  • AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
  • QueryWrapper : Entity 对象封装操作类,不是用lambda语法
  • UpdateWrapper : Update 条件封装,用于Entity对象更新操作
  • AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
  • LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper
  •  LambdaUpdateWrapper : Lambda 更新封装Wrapper

二、项目实例

 

1、根据主键或者简单的查询条件进行查询


  
  
  1. /**
  2.      * 通过单个ID主键进行查询
  3.      */
  4.     @Test
  5.     public void selectById( ) {
  6.         User user = userMapper. selectById(1094592041087729666L);
  7.         System. out. println(user);
  8.     }
  9.  
  10.     /**
  11.      * 通过多个ID主键查询
  12.      */
  13.     @Test
  14.     public void selectByList( ) {
  15.         List< Long> longs = Arrays. asList(1094592041087729666L, 1094590409767661570L);
  16.         List< User> users = userMapper. selectBatchIds(longs);
  17.         users. forEach( System. out::println);
  18.     }
  19.  
  20.     /**
  21.      * 通过Map参数进行查询
  22.      */
  23.     @Test
  24.     public void selectByMap( ) {
  25.         Map< String, Object> params = new HashMap<>();
  26.         params. put( "name", "张雨琪");
  27.         List< User> users = userMapper. selectByMap(params);
  28.         users. forEach( System. out::println);
  29.     }


2、MyBatis-Plus还提供了Wrapper条件构造器,具体使用看如下代码:


  
  
  1. /**
  2.      * 名字包含雨并且年龄小于40
  3.      * <p>
  4.      * WHERE name LIKE '%雨%' AND age < 40
  5.      */
  6.     @Test
  7.     public void selectByWrapperOne() {
  8.         QueryWrapper< User> wrapper = new QueryWrapper();
  9.         wrapper.like( "name", "雨").lt( "age", 40);
  10.         List< User> users = userMapper.selectList(wrapper);
  11.         users.forEach( System.out::println);
  12.     }
  13.  
  14.     /**
  15.      * 名字包含雨
  16.      * 年龄大于20小于40
  17.      * 邮箱不能为空
  18.      * <p>
  19.      * WHERE name LIKE '%雨%' AND age BETWEEN 20 AND 40 AND email IS NOT NULL
  20.      */
  21.     @Test
  22.     public void selectByWrapperTwo() {
  23.         QueryWrapper< User> wrapper = Wrappers.query();
  24.         wrapper.like( "name", "雨").between( "age", 20, 40).isNotNull( "email");
  25.         List< User> users = userMapper.selectList(wrapper);
  26.         users.forEach( System.out::println);
  27.     }
  28.  
  29.     /**
  30.      * 名字为王性
  31.      * 或者年龄大于等于25
  32.      * 按照年龄降序排序,年龄相同按照id升序排序
  33.      * <p>
  34.      * WHERE name LIKE '王%' OR age >= 25 ORDER BY age DESC , id ASC
  35.      */
  36.     @Test
  37.     public void selectByWrapperThree() {
  38.         QueryWrapper< User> wrapper = Wrappers.query();
  39.         wrapper.likeRight( "name", "王").or()
  40.                 .ge( "age", 25).orderByDesc( "age").orderByAsc( "id");
  41.         List< User> users = userMapper.selectList(wrapper);
  42.         users.forEach( System.out::println);
  43.     }
  44.  
  45.     /**
  46.      * 查询创建时间为2019年2月14
  47.      * 并且上级领导姓王
  48.      * <p>
  49.      * WHERE date_format(create_time,'%Y-%m-%d') = '2019-02-14' AND manager_id IN (select id from user where name like '王%')
  50.      */
  51.     @Test
  52.     public void selectByWrapperFour() {
  53.         QueryWrapper< User> wrapper = Wrappers.query();
  54.         wrapper.apply( "date_format(create_time,'%Y-%m-%d') = {0}", "2019-02-14")
  55.                 .inSql( "manager_id", "select id from user where name like '王%'");
  56.         List< User> users = userMapper.selectList(wrapper);
  57.         users.forEach( System.out::println);
  58.     }
  59.  
  60.     /**
  61.      * 查询王姓
  62.      * 并且年龄小于40或者邮箱不为空
  63.      * <p>
  64.      * WHERE name LIKE '王%' AND ( age < 40 OR email IS NOT NULL )
  65.      */
  66.     @Test
  67.     public void selectByWrapperFive() {
  68.         QueryWrapper< User> wrapper = Wrappers.query();
  69.         wrapper.likeRight( "name", "王").and(qw -> qw.lt( "age", 40).or().isNotNull( "email"));
  70.         List< User> users = userMapper.selectList(wrapper);
  71.         users.forEach( System.out::println);
  72.     }
  73.  
  74.     /**
  75.      * 查询王姓
  76.      * 并且年龄大于20 、年龄小于40、邮箱不能为空
  77.      * <p>
  78.      * WHERE name LIKE ? OR ( age BETWEEN ? AND ? AND email IS NOT NULL )
  79.      */
  80.     @Test
  81.     public void selectByWrapperSix() {
  82.         QueryWrapper< User> wrapper = Wrappers.query();
  83.         wrapper.likeRight( "name", "王").or(
  84.                 qw -> qw.between( "age", 20, 40).isNotNull( "email")
  85.         );
  86.         List< User> users = userMapper.selectList(wrapper);
  87.         users.forEach( System.out::println);
  88.     }
  89.  
  90.     /**
  91.      * (年龄小于40或者邮箱不为空) 并且名字姓王
  92.      * WHERE ( age < 40 OR email IS NOT NULL ) AND name LIKE '王%'
  93.      */
  94.     @Test
  95.     public void selectByWrapperSeven() {
  96.         QueryWrapper< User> wrapper = Wrappers.query();
  97.         wrapper.nested(qw -> qw.lt( "age", 40).or().isNotNull( "email"))
  98.                 .likeRight( "name", "王");
  99.         List< User> users = userMapper.selectList(wrapper);
  100.         users.forEach( System.out::println);
  101.     }
  102.  
  103.     /**
  104.      * 查询年龄为30、31、32
  105.      * WHERE age IN (?,?,?)
  106.      */
  107.     @Test
  108.     public void selectByWrapperEight() {
  109.         QueryWrapper< User> wrapper = Wrappers.query();
  110.         wrapper.in( "age", Arrays.asList( 30, 31, 32));
  111.         List< User> users = userMapper.selectList(wrapper);
  112.         users.forEach( System.out::println);
  113.     }
  114.  
  115.     /**
  116.      * 查询一条数据
  117.      * limit 1
  118.      */
  119.     @Test
  120.     public void selectByWrapperNine() {
  121.         QueryWrapper< User> wrapper = Wrappers.query();
  122.         wrapper.in( "age", Arrays.asList( 30, 31, 32)).last( "limit 1");
  123.         List< User> users = userMapper.selectList(wrapper);
  124.         users.forEach( System.out::println);
  125.     }


三、具体使用操作

        注意:以下条件构造器的方法入参中的 column 均表示数据库字段

1、ge、gt、le、lt、isNull、isNotNull
 


  
  
  1. @Test
  2. public void testDelete() {
  3.  
  4.     QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  5.     queryWrapper
  6.         .isNull( "name")
  7.         .ge( "age", 12)
  8.         .isNotNull( "email");
  9.     int result = userMapper.delete(queryWrapper);
  10.     System. out.println( "delete return count = " + result);
  11. }

SQL:UPDATE user SET deleted=1 WHERE deleted=0 AND name IS NULL AND age >= ? AND email IS NOT NULL

2、eq、ne


  
  
  1. 注意:seletOne返回的是一条实体记录,当出现多条时会报错
  2. @Test
  3. public void testSelectOne( ) {
  4.     QueryWrapper< User> queryWrapper = new QueryWrapper<>();
  5.     queryWrapper. eq( "name", "Tom");
  6.  
  7.     User user = userMapper. selectOne(queryWrapper);
  8.     System. out. println(user);
  9. }

3、between、notBetween


  
  
  1. 包含大小边界
  2. @Test
  3. public void testSelectCount( ) {
  4.  
  5.     QueryWrapper< User> queryWrapper = new QueryWrapper<>();
  6.     queryWrapper. between( "age", 20, 30);
  7.  
  8.     Integer count = userMapper. selectCount(queryWrapper);
  9.     System. out. println(count);
  10. }

SELECT COUNT(1) FROM user WHERE deleted=0 AND age BETWEEN ? AND ? 

4、allEq


  
  
  1. @Test
  2. public void testSelectList( ) {
  3.  
  4.     QueryWrapper< User> queryWrapper = new QueryWrapper<>();
  5.     Map< String, Object> map = new HashMap<>();
  6.     map. put( "id", 2);
  7.     map. put( "name", "Jack");
  8.     map. put( "age", 20); 9
  9.  
  10.     queryWrapper. allEq(map);
  11.     List< User> users = userMapper. selectList(queryWrapper);
  12.  
  13.     users. forEach( System. out::println);
  14. }

SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERE deleted=0 AND name = ? AND id = ? AND age = ? 

5、like、notLike、likeLeft、likeRight


  
  
  1. selectMaps返回 Map集合列表
  2. @Test
  3. public void testSelectMaps( ) {
  4.  
  5.     QueryWrapper< User> queryWrapper = new QueryWrapper<>();
  6.     queryWrapper
  7.         . notLike( "name", "e")
  8.         . likeRight( "email", "t");
  9.  
  10.     List< Map< String, Object>> maps = userMapper. selectMaps(queryWrapper); //返回值是Map列表
  11.     maps. forEach( System. out::println);
  12. }

SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERE deleted=0 AND name NOT LIKE ? AND email LIKE ? 

6、in、notIn、inSql、notinSql、exists、notExists


  
  
  1. in、notIn:
  2. notIn( "age",{ 1, 2, 3})--->age not in (1,2,3)
  3. notIn( "age", 1, 2, 3)--->age not in (1,2,3)
  4. inSql、notinSql:可以实现子查询
  5. 例: inSql( "age", "1,2,3,4,5,6")--->age in (1,2,3,4,5,6)
  6. 例: inSql( "id", "select id from table where id < 3")--->id in (select id from table where id < 3)
  7. @Test
  8. public void testSelectObjs () {
  9.  
  10.     QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  11.     //queryWrapper.in("id", 1, 2, 3);
  12.     queryWrapper.inSql( "id", "select id from user where id < 3");
  13.  
  14.     List<Object> objects = userMapper.selectObjs(queryWrapper); //返回值是Object列表
  15.     objects.forEach(System.out::println);
  16. }

SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERE deleted=0 AND id IN (select id from user where id < 3) 

7、or、and


  
  
  1. 注意:这里使用的是 UpdateWrapper 不调用 or则默认为使用 and
  2. @Test
  3. public void testUpdate1() {
  4.  
  5.     //修改值
  6.     User user = new User();
  7.     user.setAge( 99);
  8.     user.setName( "Andy");
  9.  
  10.     //修改条件
  11.     UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
  12.     userUpdateWrapper
  13.         .like( "name", "h")
  14.         . or()
  15.         .between( "age", 20, 30);
  16.  
  17.     int result = userMapper.update(user, userUpdateWrapper);
  18.     System. out.println(result);
  19. }

UPDATE user SET name=?, age=?, update_time=? WHERE deleted=0 AND name LIKE ? OR age BETWEEN ? AND ?

8、嵌套or、嵌套and


  
  
  1. 这里使用了lambda表达式, or中的表达式最后翻译成sql时会被加上圆括号
  2. @Test
  3. public void testUpdate2() {
  4.  
  5.     //修改值
  6.     User user = new User();
  7.     user.setAge( 99);
  8.     user.setName( "Andy");
  9.  
  10.     //修改条件
  11.     UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
  12.     userUpdateWrapper
  13.         .like( "name", "h")
  14.         . or(i -> i.eq( "name", "李白").ne( "age", 20));
  15.  
  16.     int result = userMapper.update(user, userUpdateWrapper);
  17.     System. out.println(result);
  18. }

UPDATE user SET name=?, age=?, update_time=? 

WHERE deleted=0 AND name LIKE ? 

OR ( name = ? AND age <> ? ) 

9、orderBy、orderByDesc、orderByAsc
 


  
  
  1. @Test
  2. public void testSelectListOrderBy( ) {
  3.  
  4.     QueryWrapper< User> queryWrapper = new QueryWrapper<>();
  5.     queryWrapper. orderByDesc( "id");
  6.  
  7.     List< User> users = userMapper. selectList(queryWrapper);
  8.     users. forEach( System. out::println);
  9. }

SELECT id,name,age,email,create_time,update_time,deleted,version 

FROM user WHERE deleted=0 ORDER BY id DESC

10、last


  
  
  1. 直接拼接到 sql 的最后
  2. 注意:只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
  3. @Test
  4. public void testSelectListLast( ) {
  5.     QueryWrapper< User> queryWrapper = new QueryWrapper<>();
  6.     queryWrapper. last( "limit 1");
  7.  
  8.     List< User> users = userMapper. selectList(queryWrapper);
  9.     users. forEach( System. out::println);
  10. }

SELECT id,name,age,email,create_time,update_time,deleted,version 

FROM user WHERE deleted=0 limit 1

 

11、指定要查询的列


  
  
  1. @Test
  2. public void testSelectListColumn( ) {
  3.  
  4.     QueryWrapper< User> queryWrapper = new QueryWrapper<>();
  5.     queryWrapper. select( "id", "name", "age");
  6.  
  7.     List< User> users = userMapper. selectList(queryWrapper);
  8.     users. forEach( System. out::println);
  9. }

SELECT id,name,age FROM user WHERE deleted=0

12、set、setSql


  
  
  1. 最终的sql会合并 user.setAge(),以及 userUpdateWrapper. set()  和 setSql() 中 的字段
  2. @Test
  3. public void testUpdateSet() {
  4.     //修改值
  5.     User user = new User();
  6.     user.setAge( 99);
  7.  
  8.     //修改条件
  9.     UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
  10.     userUpdateWrapper
  11.         .like( "name", "h")
  12.         . set( "name", "老李头") //除了可以查询还可以使用set设置修改的字段
  13.         .setSql( " email = '123@qq.com'"); //可以有子查询
  14.     int result = userMapper.update(user, userUpdateWrapper);
  15. }

UPDATE user SET age=?, update_time=?, name=?, email = '123@qq.com' WHERE deleted=0 AND name LIKE ?

-------------------------------------------------------------------------------------以下无正文-------------------

参考文档

1、https://blog.csdn.net/m0_37034294/article/details/82917234

2、https://blog.csdn.net/kepengs/article/details/112345870

3、https://blog.csdn.net/weixin_39615889/article/details/107086931

4、https://blog.csdn.net/weixin_38111957/article/details/91447509

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐