一、Java中QueryWrapper的基本使用
1.单表查询
@GetMapping("/list")
public TableDataInfo list(Student student){
LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<Student>();
lqw.eq(Student::getName, student.getName());
lqw.like(Student::getClass,student.getClass());
lqw.between("age",student.getAge1(),student.getAge2());
lqw.orderByAsc("age");
List<Student> list = studentService.list(lqw);
}
对应的sql语句为:
select * from student where name = '?' and class like '%?%' and age between '?' and '?' order by '?' asc
1.1、单表查询的基本用法
函数名 | 说明 | 例子 |
---|---|---|
eq | 等于 | 例:eq(“name”,“张子”) ===> name = ‘张子’ |
ne | 不等于 | 例:ne(“name”,“张子”) ===> name <> ‘张子’ |
gt | 大于 | 例:gt(“age”,“18”) ===> age>18 |
lt | 小于 | 例:lt(“age”,“18”) ===> age<18 |
between | 在值1到值2之间 | 例:between(“age”,18,30) ===> 18<age<30’ |
like | 模糊查询 | 例:like(“name”,“张”) ===> name like ‘%张%’ |
isNull | 字段为NULL | 例:isNull(“name”) ===> name is null |
2、多表查询
//Controller
@GetMapping("/listAndClass")
public TableDataInfo listAndClass(Student student)
{
QueryWrapper<Student > qw = new QueryWrapper<Student >();
if(StringUtils.isNotBlank(student.getName())){
qw.eq("s.name",student.getName());
}
if(StringUtils.isNotBlank(student.getClassName())){
qw.like("c.name",student.getClassName());
}
startPage();
List<Student > list = studentService.listAndClass(qw);
return getDataTable(list);
}
//Service
List<Student> listAndClass(QueryWrapper<Student> qw);
//Service impl
@Override
public List<Student> listAndClass(QueryWrapper<Student> qw) {
return this.getBaseMapper().listAndClass(qw);
}
//Mapper
@Select("select s.*,c.name from student s left join class c on s.id = c.student_id "+
"${ew.customSqlSegment}")
List<YwSpaqjgDj> listAndClass(@Param(Constants.WRAPPER) QueryWrapper<Student> qw);
标签:基本,QueryWrapper,Java,name,qw,age,listAndClass,student From: https://www.cnblogs.com/gongss/p/16727090.html