美文网首页开源框架-Mybatis系列
mybatis-plus配置xml进行多表查询

mybatis-plus配置xml进行多表查询

作者: 没有功底的学生 | 来源:发表于2020-10-17 00:52 被阅读0次

mybatis-plus多表查询,需自己写xml进行查询。

在mapper中定义,如需分页查询可添加page。

List<ViewJobsListVO> list(Page page, @Param("query") ViewJobs viewJobs);

在xml中编写sql语句,这里进行简单的自连接查询

<select id="list" parameterType="com.example.recruitment.entity.ViewJobs"
        resultType="com.example.recruitment.entity.vo.ViewJobsListVO">
    select a.*,b.job_name as job_parent_name
    from view_jobs a left join view_jobs b on a.job_parent=b.job_id
    <where>
        <if test="query.jobName !=null or query.jobName!='' ">
            <bind name="bindJobName" value="'%'+query.jobName+'%'"/>
            a.job_name like #{bindJobName}
        </if>
    </where>
</select>

在serviceImpl中进行返回。

/*
 *     分页条件查询
 * */
@Override
public List<ViewJobsListVO> queryPageListByCondition(Page<ViewJobsListVO> page, ViewJobs query) {
   return baseMapper.list(page, query);

}

controller

@PostMapping("/{page}/{limit}")
public R getPageList(@PathVariable Integer page,
                @PathVariable Integer limit,
                @RequestBody(required = false) ViewJobs query){

   Page<ViewJobsListVO> jobsPage = new Page<>(page,limit);
   List<ViewJobsListVO> list = viewJobsService.queryPageListByCondition(jobsPage,query);
   return R.ok().data("items",list).data("total",jobsPage.getTotal()).data("query",query);


}

需要注意的是,在plus中,若直接适用plus的分页方法的话,是不需要返回List的,他将list放在之前传入的page中,可直接调动page.getRecords获取list。而自己定义xml进行分页查询的话,像使用mybatis一样需要返回list,此时的page.getRecords是空的,但page的其他是有值的像total之类的。从下图中可以看出。

image-20201017000659063.png

在mybatis-plus中使用xml时需要设置mybatis-plus.mapper-locations进行xml位置的指定。否则会报以下错误

org.apache.ibatis.binding.BindingException:Invalid bound statement not found

在spring boot的配置文件中进行配置

mybatis-plus.mapper-locations=classpath:com/.../*.xml

!!!配置时请检查下编译后的target中xml路径是否存在xml文件,若不存在极有可能是maven的pom.xml中没有设置resource进行资源文件忽略。

pom.xml中resource设置参考如下:

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*</include>
        </includes>
    </resource>
</resources>

!!!设置完毕后,将target文件夹进行删除,然后重新编译。若不想设置pom.xml也可将xml文件放在resource资源文件夹中。

相关文章

网友评论

    本文标题:mybatis-plus配置xml进行多表查询

    本文链接:https://www.haomeiwen.com/subject/zvcxmktx.html