UserMapper:
@Repository //代表持久层
public interface UserMapper extends BaseMapper<User> {
}
如果queryWrapper调用了 .isNotNull("name")、 .isNotNull("email")、 .ge("age",12)
@Test
void contextLoads() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.isNotNull("name")
.isNotNull("email")
.ge("age",12);
userMapper.selectList(queryWrapper).forEach(System.out::println);
}
那么运行的sql就是这样的:(注意where字句)
==> Preparing: SELECT id,name,age,email,version,deleted,create_time,update_time FROM user WHERE deleted=0
AND (name IS NOT NULL AND email IS NOT NULL AND age >= ?)
==> Parameters: 12(Integer)
如果queryWrapper只是调用了.isNotNull("name")
@Test
void contextLoads() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.isNotNull("name"); //只调用了一个非空条件
userMapper.selectList(queryWrapper).forEach(System.out::println);
}
那么运行结果就是这样的:(注意where子句)
==> Preparing: SELECT id,name,age,email,version,deleted,create_time,update_time FROM user WHERE deleted=0
AND (name IS NOT NULL)
==> Parameters:
找到规律了没?
其实很简单,QueryWraper就是增加where字句的。queryWrapper调用了一下 .isNotNull("name"),那么sql语句的where条件里面就加一句and name IS NOT NUL,queryWrapper再调用一下.ge("age",12),那么sql语句得where条件里面就再加一句and age >12。
mybatisplus自定义sql
参考:https://www.cnblogs.com/baiyifengyun/p/13756653.html
mybatisplus自定义sql+QueryWrapper
这里举一个多表联查的例子
实体类: