一、问题
- 前端所需数据:
但成绩表中只有考生的id及试卷的id,此时就需要进行多表查询,通过考生的id获取姓名字段,通过试卷的id获取试卷名称。分别需要使用到三张表,成绩表,试卷表,考生表。同时需要做到可以多条件查询,且带分页。
- 以前遇到的分页基本都是多条件单表分页,实现起来相对比较容易。现在使用多表带分页带多条件就需要自行进行编写了。
二、具体解决及参考博客
具体参考:https://blog.csdn.net/weixin_38111957/article/details/91554108
- 新建一个实体类,包含前端所需的数据实体。
- Mapper层接口及xml文件
//Mapper接口层
IPage<CandidateScoreInfoVo> selectMyPage(IPage<CandidateScoreInfoVo> page, @Param(Constants.WRAPPER) Wrapper<CandidateScoreInfoVo> queryWrapper);
//XML具体实现
<select id="selectMyPage" resultType="com.jqh.candidateservice.entity.vo.CandidateScoreInfoVo">
select
cs.id as id,
c.name as candidateName,
e.paper_name as paperName,
cs.subj_score as subjScore,
cs.obj_score as objScore,
cs.total_score as totalScore,
cs.gmt_create as gmt_create
from candidate_score cs
left join candidate c on cs.candidate_id=c.id
left join exam_paper e on cs.paper_id=e.id
${ew.customSqlSegment}
</select>
- 最好进行使用,跟单表多条件分页查询的一致,注意queryWrapper中字段的选择。
//page对象
Page<CandidateScoreInfoVo> pageCanScoreInfo=new Page<>(current,limit);
....
//这里的字段应选你在Mapper层所需要的字段
if(!StringUtils.isEmpty(candidateName)){
queryWrapper.like("c.name",candidateName);
}
if(!StringUtils.isEmpty(paperName)){
queryWrapper.like("e.paper_name",paperName);
}
...
//进行使用
baseMapper.selectMyPage(pageCanScoreInfo,queryWrapper);
- swagger测试
三、其他相关问题解决
- 'ew.customSqlSegment问题
Error evaluating expression ‘ew.customSqlSegment’. 版本过低,更换版本。
- Invalid bound statement (not found): …问题
编译后找不到mapper.xml文件,即在target中找不到xml的存在,因为maven工程在默认情况下src/main/java目录下的所有资源文件是不发布到target目录下的。
- 在pom文件中配置如下节点,并重新加载Maven。
<!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
- 在项目配置文件中添加配置,最后重启项目就可以发现target中出现了XML文件。
#配置mapper xml文件的路径
mybatis-plus.mapper-locations=classpath:com/guli/edu/mapper/xml/*.xml