項目中使用springboot+mybatis-plus來實現。
但是之前處理的時候都是一個功能,比如分頁查詢,條件查詢,模糊查詢。
這次將這個幾個功能合起來就有點頭疼,寫下這邊博客來記錄自己碰到的問題
我們如果要實現多表分頁模糊查詢,需要按照下面的步驟進行。
配置分頁插件
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
請求類和結果返回類
對於多表關聯的查詢時,還是需要編寫 Req類和VO 類和 手動的在Mapper.xml 中編寫sql
雖然是可以不用創建這兩個類,用Map 的方式接受返回的結果,但這樣只會更麻煩
請求類
@Data
public class StuReq {
private String key;
private Integer page;
private Integer limit;
}
結果返回類
@Data
public class StuVo {
private Integer id;
private String username;
private String password;
private String name;
private String phone;
private String idcard;
private Integer sex;
private Integer sno;
private Integer gradeId;
private String gradeName;
private Integer classId;
private String className;
}
mapper.xml
因為要涉及到多表連接,所以我們就不能使用basemapper
,要手動編寫mapper文件
- 需要注意
resultType
和parameterType
- 模糊查詢的時候,采用
"%"#{key}"%"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dj.dormitory.mapper.StudentMapper">
<select id="getStuPageList" resultType="com.dj.dormitory.entity.vo.StuVo" parameterType="com.dj.dormitory.entity.req.StuReq">
select tb_student.id,tb_user.user_name username,tb_user.`password`,tb_user.`name`,tb_user.phone
,tb_student.sno,tb_student.idcard,tb_student.grade_id,tb_grade.`name` grade_name,tb_student.sex
,tb_student.class_id,tb_org.`name` class_name
from tb_student
left outer join tb_user on tb_student.user_id = tb_user.id
left outer join tb_grade on tb_student.grade_id=tb_grade.id
left outer join tb_org on tb_student.class_id = tb_org.id
<if test="stuReq.key!=null">
where tb_student.sno like "%"#{stuReq.key}"%" or tb_user.name like "%"#{stuReq.key}"%"
</if>
</select>
</mapper>
Mapper接口
-
方法名需要和
StudentMapper.xml
的id
相同 -
引入參數page,為了分頁
-
@Param("stuReq")
為了后續xml的識別
public interface StudentMapper extends BaseMapper<Student> {
List<Map> getAllStu();
List<StuVo> getStuPageList(Page page, @Param("stuReq")StuReq stuReq);
}
Service業務接口
public interface StudentService extends IService<Student> {
Map<String,Object> getList(StuReq stuReq);
}
ServiceImpl業務接口實現類
@Override
public Map<String, Object> getList(StuReq stuReq) {
Page<StuVo> page = new Page<>(stuReq.getPage(),stuReq.getLimit());
List<StuVo> list = this.baseMapper.getStuPageList(page,stuReq);
Map<String,Object> map = new HashMap();
map.put("count",page.getTotal());//獲得總共的數目
map.put("stus",list);//獲得當前頁的數據
return map;
}
Controller控制類
- 前端使用了
layui
的表格分頁,攜帶limit
和page
。以及當我們點擊搜索按鈕進行模糊查詢的傳入的關鍵詞key
。所以采用了post
和@RequestBody
- 另外一點需要注意,count指示的是查詢出的所有數據的條數,而不是當前頁的數目,即
list.size()
@PostMapping("/list")
public Result getList(@RequestBody StuReq stuReq){
Map<String, Object> map = studentService.getList(stuReq);
return Result.ok().data("stus",map.get("stus")).data("count",map.get("count"));
}
測試
‘
如果存在不足之處,評論區指正哦。
如果對你有幫助,給我一個點贊或推薦哦!
參考鏈接:
https://blog.csdn.net/womenyiqilalala/article/details/95073892
https://blog.csdn.net/weixin_40569991/article/details/88724739