在上一篇博客中总结了MyBatis Plus 实现多表分页模糊查询(链接在最后)。返回类型是编写一个专门的vo类。这次是返回List < Map >
前言
编写一个专门的vo返回类,主要原因是如果其他地方也要用到到这个返回结果类,相比于map,返回类比较方便处理。
但是,这次的返回类型吧并不常用,所以就考虑了List<Map<String,Object>>
解决
在上一篇博客的基础,我们需要将返回类型换成List<Map<String,Object>>
下面举一个例子,演示一下。
controller
@PostMapping("getAll")
public Result getAll(@RequestBody DormitoryStudent dormitoryStudent){
List<Map<String,Object>> stus = dormitoryStudentService.getList(dormitoryStudent.getDormitoryId());
return Result.ok().data("list",stus);
}
service
public interface DormitoryStudentService extends IService<DormitoryStudent> {
List<Map<String,Object>> getList(Integer dormitoryId);
}
serviceImpl
@Service
public class DormitoryStudentServiceImpl extends ServiceImpl<DormitoryStudentMapper, DormitoryStudent> implements DormitoryStudentService {
@Override
public List<Map<String,Object>> getList(Integer dormitoryId) {
List<Map<String,Object>> stus = this.baseMapper.getList(dormitoryId);
return stus;
}
}
mapper
public interface DormitoryStudentMapper extends BaseMapper<DormitoryStudent> {
List<Map<String,Object>> getList(@Param("dormitory_id") Integer dormitoryId);
}
mapper.xml
非常重要的一个点 resultType="java.util.HashMap"
<?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.DormitoryStudentMapper">
<select id="getList" resultType="java.util.HashMap">
select tb_user.name student_name,tb_student.sno
from tb_student
left outer join tb_user on tb_user.id = tb_student.user_id
left outer join tb_dormitory_student on tb_dormitory_student.student_id = tb_student.id
where tb_dormitory_student.dormitory_id = #{dormitory_id}
</select>
</mapper>
接口测试
知识点补充
resultType:
-
基本类型 :resultType=基本类型
-
List类型: resultType=List中元素的类型,注意:不是list
-
Map类型
- 单条记录:resultType =map
- 多条记录:resultType = Map中value的类型
参考链接: