1.0 wapper的介绍
(网上找的图片)
名词 | 释义 |
Wrapper : | 条件构造抽象类,最顶端父类 |
AbstractWrapper : | 用于查询条件封装,生成 sql 的 where 条件 |
QueryWrapper : | 查询条件封装 |
UpdateWrapper : | Update 条件封装 |
AbstractLambdaWrapper : | 使用Lambda 语法 |
LambdaQueryWrapper : | 用于Lambda语法使用的查询Wrapper |
LambdaUpdateWrapper : | Lambda 更新封装Wrapper |
其中洗下面的这些方法全部都是以为wapper方法体
2.0 wapper的使用
2.1 查询 中使用 wapper (selectList)
按照上面的图形结构结合我们使用的查询功能 故用QueryWrapper ;
@Test public void slelectWrapper(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //此处指明column 指的是数据库的字段并非实体类对象请大家注意 // like:模糊查询 between 范围 大于10小于30 isnull 字段不为空的数据 queryWrapper.like("user_name","老王") .between("age",10,30) .isNotNull("email"); ///查询用户为 老王的 年龄范围在10-30之间的并且邮箱不为空的数据 List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
运行结果为:
==> Preparing: SELECT uid AS id,user_name AS name,age,email,is_delect FROM t_user WHERE is_delect=0 AND (user_name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL) ==> Parameters: %老王%(String), 10(Integer), 30(Integer) <== Columns: id, name, age, email, is_delect <== Row: 4, 我是老王, 18, test4@baomidou.com, 0 <== Row: 1577321026130407426, 老王, 10, 757631644001, 0 <== Row: 1577321026298179585, 老王超1, 11, 757631644111, 0 <== Total: 3 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3ce53f6a] User(id=4, name=我是老王, age=18, email=test4@baomidou.com, isDelect=0) User(id=1577321026130407426, name=老王, age=10, email=757631644001, isDelect=0) User(id=1577321026298179585, name=老王超1, age=11, email=757631644111, isDelect=0)
2.2 升序降序 中使用 wapper (selectList)
@Test public void slelectWrapperAsc(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //orderByDesc 降序 orderByAsc 升序 queryWrapper.orderByDesc("age") .orderByAsc("id"); //查询用户年龄并降序排列,年龄相同的按照id升序排序 List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
运行结果入如下:
==> Preparing: SELECT uid AS id,user_name AS name,age,email,is_delect FROM t_user WHERE is_delect=0 ORDER BY age DESC,id ASC ==> Parameters: <== Columns: id, name, age, email, is_delect <== Row: 15, 闫文超9, 19, 757631644991, 0 <== Row: 4, 我是老王, 18, test4@baomidou.com, 0 <== Row: 14, 闫文超8, 18, 757631644881, 0 <== Row: 13, 闫文超7, 17, 757631644771, 0 <== Row: 12, 闫文超6, 16, 757631644661, 0 <== Row: 11, 闫文超5, 15, 757631644551, 0 <== Row: 10, 闫文超4, 14, 757631644441, 0 <== Row: 5, Billie, 10, test5@baomidou.com, 0 <== Row: 6, 老王, 10, 757631644001, 0 <== Row: 7, 老王超1, 10, 757631644111, 0 <== Row: 8, 闫文超2, 10, 757631644221, 0 <== Row: 9, 闫文超3, 10, 757631644331, 0 <== Total: 12 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19650aa6] User(id=15, name=闫文超9, age=19, email=757631644991, isDelect=0) User(id=4, name=我是老王, age=18, email=test4@baomidou.com, isDelect=0) User(id=14, name=闫文超8, age=18, email=757631644881, isDelect=0) User(id=13, name=闫文超7, age=17, email=757631644771, isDelect=0) User(id=12, name=闫文超6, age=16, email=757631644661, isDelect=0) User(id=11, name=闫文超5, age=15, email=757631644551, isDelect=0) User(id=10, name=闫文超4, age=14, email=757631644441, isDelect=0) User(id=5, name=Billie, age=10, email=test5@baomidou.com, isDelect=0) User(id=6, name=老王, age=10, email=757631644001, isDelect=0) User(id=7, name=老王超1, age=10, email=757631644111, isDelect=0) User(id=8, name=闫文超2, age=10, email=757631644221, isDelect=0) User(id=9, name=闫文超3, age=10, email=757631644331, isDelect=0)
2.3 删除中使用 wapper (delect)
@Test public void delectWrapperAsc(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //orderByDesc 降序 orderByAsc 升序 queryWrapper.eq("email","757631644771"); //删除邮箱为空的数据(因为我们加入了逻辑删除的注解实体类属性我们实现的删除也只是 修改他的字段为假删除 不需要删除即可) int result = userMapper.delete(queryWrapper); System.out.println(result); }
如果不行实现逻辑删除则把实体相管内容注解去掉即可;
运行结果为
==> Preparing: UPDATE t_user SET is_delect=1 WHERE is_delect=0 AND (email = ?) ==> Parameters: 757631644771(String) <== Updates: 2
2.4 修改中使用 wapper (update)
@Test public void UpdatetWrapperAsc(){ //将用户名中包含有a并且年龄大于20或邮箱为null的用户信息修改 QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //并且 是and 默认就是and 但是 或的话就需要 手动添加or 条件了 //gt 是大于 queryWrapper.gt("age",20) .like("user_name","a") .or() .isNull("email"); User user = new User(); user.setEmail("757631644@qq.com"); user.setName("老闫"); user.setAge(666); //两个参数第一个 修改的用户体对象 第二天是判断的条件 int result = userMapper.update(user,queryWrapper); //三目运算 System.out.println(result>0 ? "修改成功" :"修改失败"); System.out.println("修改条数"+result); }
运行结果为:
==> Preparing: UPDATE t_user SET user_name=?, age=?, email=? WHERE is_delect=0 AND (age >= ? AND user_name LIKE ? OR email IS NULL) ==> Parameters: 老闫(String), 666(Integer), 757631644@qq.com(String), 20(Integer), %a%(String) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7fd8c559] 修改成功 修改条数1
2.5 条件优先级 中使用 wapper (update)
lambda 优先运行在and 包围的情况下;
@Test public void UpdatetWrapper(){ //将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 // 并且用and 包住mybatis中 lambda 优先运行 //将 queryWrapper .like(a) and (年龄大于20或邮箱为null ==> // (i->i.ge("age",20) .or().isNull("email"))的 用户的用户的信息进行修改 QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper .like("user_name","a") .and(i->i.gt("age",20) .or() .isNull("email")); User user = new User(); user.setEmail("757631644@qq.com"); user.setName("老闫"); user.setAge(666); //两个参数第一个 修改的用户体对象 第二天是判断的条件 int result = userMapper.update(user,queryWrapper); //三目运算 System.out.println(result>0 ? "修改成功" :"修改失败"); System.out.println("修改条数"+result); }
运行结果为:
==> Preparing: UPDATE t_user SET user_name=?, age=?, email=? WHERE is_delect=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL)) ==> Parameters: 老闫(String), 666(Integer), 757631644@qq.com(String), %a%(String), 20(Integer) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@8f2098e] 修改成功 修改条数1
@Test public void slelectWrappers(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //根据需求查询部分字段 queryWrapper.select("age","user_name"); //查询用户年龄并降序排列,年龄相同的按照id升序排序 List<Map<String, Object>> list = userMapper.selectMaps(queryWrapper); list.forEach(System.out::println); }
运行结果为
==> Preparing: SELECT age,user_name FROM t_user WHERE is_delect=0 ==> Parameters: <== Columns: age, user_name <== Row: 18, 我是老王 <== Row: 10, Billie <== Row: 10, 老王 <== Row: 10, 老王超1 <== Row: 10, 闫文超2 <== Row: 10, 闫文超3 <== Row: 14, 闫文超4 <== Row: 16, 闫文超6 <== Row: 666, 老闫 <== Row: 666, 老闫 <== Total: 10 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67a3bd51] {user_name=我是老王, age=18} {user_name=Billie, age=10} {user_name=老王, age=10} {user_name=老王超1, age=10} {user_name=闫文超2, age=10} {user_name=闫文超3, age=10} {user_name=闫文超4, age=14} {user_name=闫文超6, age=16} {user_name=老闫, age=666} {user_name=老闫, age=666}