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

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

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

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

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


在serviceImpl中进行返回。

/*
 *     分页条件查询
 * */
@Override
public List queryPageListByCondition(Page 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 jobsPage = new Page<>(page,limit);
   List 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设置参考如下:


    
        src/main/java
        
            **/*.xml
        
    
    
        src/main/resources
        
            **/*
        
    

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

你可能感兴趣的:(mybatis-plus配置xml进行多表查询)