Mybatis-Plus之四种lambda方式
lambda四种表达形式
前言
使用了lambda表达式 可以通过方法引用的方式来使用实体字段名的操作,避免直接写数据库表字段名时的错写名字;
一、LambdaQueryWrapper<>
/*** lambda 条件构造器* 生成的sql语句 SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age < ?*/@Testpublic void selectLambda() {//创建lambda 条件构造器 的三种方法// LambdaQueryWrapper<User> lambda2 = new LambdaQueryWrapper<>();// LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();LambdaQueryWrapper<User> lambda3 = Wrappers.<User>lambdaQuery();lambda3.like(User::getName, "雨").lt(User::getAge, 40);List<User> users = userMapper.selectList(lambda3);users.forEach(System.out::println);}
二、QueryWrapper<实体>().lambda()
/*** 名字为王姓并且(年龄小于40并且邮箱不为空)* name like '王%' and (age <40 or email in not null)* 生成的sql语句 SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age < ?*/@Testpublic void selectLambda2() {LambdaQueryWrapper<User> lambda3 = Wrappers.<User>lambdaQuery();lambda3.likeRight(User::getName, "王").and(qw -> qw.lt(User::getAge, 40).or().isNotNull(User::getEmail));List<User> users = userMapper.selectList(lambda3);users.forEach(System.out::println);}
三、Wrappers.<实体>lambdaQuery(),Wrappers.lambdaQuery(实体.class)
Wrappers.<实体>lambdaQuery()
/*** 自定义sql 使用* Dao层 代码:** @Select("select * from ${ew.customSqlSegment}")* List<User> selectAll(@Param(Constants.WRAPPER)Wrapper<User> wrapper);*/@Testpublic void selectLambda4my() {LambdaQueryWrapper<User> lambda3 = Wrappers.<User>lambdaQuery();lambda3.likeRight(User::getName, "王").and(qw -> qw.lt(User::getAge, 40).or().isNotNull(User::getEmail));List<User> users = userMapper.selectAll(lambda3);users.forEach(System.out::println);}
Wrappers.lambdaQuery(实体.class)
四、LambdaQueryChainWrapper<实体>(xxxxMapper)
/*** lambda 条件构造器第四种创建方式 MP 3.0.7 新增的方式* 生成的sql语句 SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age >= ?* 这个例子可以看出 代码更简洁了* 生成的sql语句 SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age >= ?*/@Testpublic void selectLambda3() {List<User> users = new LambdaQueryChainWrapper<User>(userMapper).like(User::getName, "雨").ge(User::getAge, 20).list();users.forEach(System.out::println);}
QueryWrapper、LambdaQueryWrapper以及LambdaQueryChainWrapper用法
一、QueryWrapper
.like("email", "24252") //email对应数据库中列名
/*** 条件构造器 查询操作*/@Testvoid TestQueryWrapperSelect() {//1、条件用法List<User> userList = userMapper.selectList(new QueryWrapper<User>().like("email", "24252").between("age", 20, 22).or().eq("name", "zcx"));System.out.println("userList:" + userList);//2、排序用法List<User> users = userMapper.selectList(new QueryWrapper<User>().eq("nick_name", "xx").orderByAsc("age") //升序// .orderByDesc("age") //降序.last("limit 0,3") //last用法:在sql末尾添加sql语句,有sql注入风险);System.out.println("users:"+users);}
二、LambdaQueryWrapper
queryWrapper.eq(User::getName,"liangd1"); //可以通过Lambda获取数据库列名
@Testvoid TestLambdaQueryWrapper() {//1、查询单条LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(User::getName,"liangd1");User selectOne = userMapper.selectOne(queryWrapper);System.out.println(selectOne);//2、查询list以及统计条数queryWrapper.eq(User::getName, "zcx");List<User> userList = userMapper.selectList(queryWrapper);System.out.println("userList:" + userList);Integer result = userMapper.selectCount(queryWrapper);System.out.println("result:" + result);}
三、LambdaQueryChainWrapper
//链式查询方式User one = new LambdaQueryChainWrapper<>(userMapper).eq(User::getName, "liangd1").one();
@Testvoid TestLambdaQueryChainWrapper() {//1、eq查询单条User one = new LambdaQueryChainWrapper<>(userMapper).eq(User::getName, "liangd1").one();System.out.println("UserOne:" + one);//2、查询listList<User> users = new LambdaQueryChainWrapper<>(userMapper).eq(User::getName, "zcx").list();System.out.println("UserList:" + users);//3、模糊查询List<User> LikeList = new LambdaQueryChainWrapper<>(userMapper).like(User::getEmail, "test").list();System.out.println("LikeUser:" + LikeList);}
QueryWrapper及LambdaQueryWrapper
实体类
@Data
public class BannerItem {private Long id;private String name;private String img;private String keyword;private Integer type;private Long bannerId;
}
QueryWrapper
最基础的使用方式是这样
// 查询条件构造器
QueryWrapper<BannerItem> wrapper = new QueryWrapper<>();
wrapper.eq("banner_id", id);
// 查询操作
List<BannerItem> bannerItems = bannerItemMapper.selectList(wrapper);
然后我们可以引入lambda,避免我们在代码中写类似的于banner_id
的硬编码
QueryWrapper<BannerItem> wrapper = new QueryWrapper<>();
wrapper.lambda().eq(BannerItem::getBannerId, id);
List<BannerItem> bannerItems = bannerItemMapper.selectList(wrapper);
LambdaQueryWrapper
为了简化lambda的使用,我们可以改写成LambdaQueryWrapper构造器,语法如下:
两种写法
new QueryWrapper().lambda();
Wrappers.lambdaQuery();
LambdaQueryWrapper<BannerItem> wrapper = new QueryWrapper<BannerItem>().lambda();
// LambdaQueryWrapper<BannerItem> wrapper = Wrappers.lambdaQuery(); // 两种写法wrapper.eq(BannerItem::getBannerId, id);
List<BannerItem> bannerItems = bannerItemMapper.selectList(wrapper);
我们可以再次将QueryWrapper.lambda()
简化,变成这个样子
BannerItem bannerItem = new LambdaQueryChainWrapper<>(bannerItemMapper).eq(BannerItem::getId, id).one();
链式查询
MyBatis-Plus还提供了一种链式查询的方式,和上面的代码效果一样。
但是这种写法偏向于炫技,可读性没有上面的代码强,大家可以根据需要自行选择方式。
List<BannerItem> bannerItems = new LambdaQueryChainWrapper<>(bannerItemMapper).eq(BannerItem::getBannerId, id).list();
如果只想查询一条记录,例如通过id查询某条记录的详情,使用.one()
即可,例如
BannerItem bannerItem = new LambdaQueryChainWrapper<>(bannerItemMapper).eq(BannerItem::getId, id).one();