☰
Java高效查询技巧:单表多表实战解析
2026/10/7 21:25:46 网站建设 项目流程

一、在Java编程里, 针对其基本功能模块的日常操作与应用方式。

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); }"/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* from student where name = '?' and class like '%?%' and age between '?' and '?' order by '?' asc

1.关于单表查询这一项内容, 其基本的使用方式需要进行详细的了解和分析。

函数名说明例子

eq

等于

eq(“name”, “张子”)这玩意儿转换成标准写法就是, name 等于单引号括起来的字符串‘张子’。

ne

不等于

举例说明, 当执行ne函数并传入参数name以及字符串张子时, 其结果表现为字段名称为name的值不等于字符串值张子。

gt

大于

例:gt(“age”,“18”) ===> age>18

lt

小于

将“年龄”设定为小于“十八”的条件, 结果转换为“age”。

在值1到值2之间

例如, 针对像“age”和数字18到30这样的内容,其结果为18。

like

模糊查询

示例的情况是, 当输入的内容为like(“name”, “张”)的时候, 转换的结果就变成了name like ‘%张%’。

字段为NULL

当字段的值为名为“name”时, 结果会显示为name是否为空。

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);@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);

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询