MybatisPlus-实现基于分页的多表关联查询

MybatisPlus-实现基于分页的多表关联查询介绍

新手入门,目录结构可能不够严谨,我这边没有用mybatis-plus自带的代码生成器,但是大致原理不变,这边我会重点介绍下我踩的坑,比较难找的几个问题,我有单独写在java常见问题里,大家可以去翻阅

1.目录结构

这边用红色框出来的,是我这边即将用到的,下面直接上代码
MybatisPlus-实现基于分页的多表关联查询_第1张图片

2.对应文件代码

项目/config/MybatisPlusConfig,其中MapeerScan路径记得别写错

@Configuration
@MapperScan("com.mk.springboot.mapper")
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

项目/pojo/Category.class, 这边我有用到lombok,如果没有用到到话,得自己把无参构造,有参构造,getter,setter方法补齐

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "category")
public class Category {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
    private Integer articleNum;
    private Integer plateId;
}

项目/mapper/CategoryMapper,一开始我是用注解写的,后面要关联其它表,这边是用xml来写,这边我有两个查询参数,一个是category表的名称name,一个是板块的plate_id

@Repository
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
    // @Select("SELECT category.*, plate.`name` as `plateName` FROM category,plate WHERE category.plate_id=plate.id and category.name = #{name} and category.plate_id = #{plate_id}")

    List<CategoryPlateVO> getCategoryPlate(Page<CategoryPlateVO> page, @Param("name") String name, @Param("plateId") Integer plateId);
}

项目/resources/mapper/CategoryMapper.xml,这边xml有几个注意事项,如果name为string 类型的话,千万不能在if条件中写name != 0,这样会报错



<mapper namespace="com.mk.springboot.mapper.CategoryMapper">
    
    <select id="getCategoryPlate" resultType="com.mk.springboot.vo.CategoryPlateVO" >
        SELECT category.*, plate.`name` as `plateName` FROM category,plate WHERE category.plate_id=plate.id
        <if test="name != null and name != ''">
            AND category.name = #{name}
        if>
        <if test='plateId != 0 and plateId != null'>
            AND category.plate_id = #{plateId}
        if>
    select>
mapper>

项目/service/CategoryService

@Service
public interface CategoryService extends IService<Category> {
   Page<CategoryPlateVO> getCategoryPlate(Page<CategoryPlateVO> page ,String name, Integer plateId);
}

项目/service/impl/CategoryService

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
   @Override
   public Page<CategoryPlateVO> getCategoryPlate(Page<CategoryPlateVO> page ,String name, Integer plateId) {
       return page.setRecords(this.baseMapper.getCategoryPlate(page, name, plateId));
   }
}

项目/vo/CategoryPlateVO

@Data
public class CategoryPlateVO implements Serializable {
   @TableId
   private Integer id;
   private String name;
   private Integer articleNum;
   private Integer plateId;
   private String plateName;
}

项目/controller/CategoryController,这边代码写的有点冗余,功能大致实现了,大家可以自行封装

@RequestMapping("/category/list")
   @ResponseBody
   public PageFactory list(HttpServletRequest httpServletRequest) {
       String name = httpServletRequest.getParameter("name");
       String plateIdString = httpServletRequest.getParameter("plateId");
       Integer plateId = plateIdString != null && plateIdString != "" ? Integer.valueOf(plateIdString) : null;
       Integer start = Integer.parseInt(httpServletRequest.getParameter("start"));
       Integer size = Integer.parseInt(httpServletRequest.getParameter("length"));
       Integer current = start / size + 1;

       Page<CategoryPlateVO> pageParam = new Page<>(current, size);// 当前页,总条数 构造 page 对象
       Page<CategoryPlateVO> page = categoryService.getCategoryPlate(pageParam, name, plateId);

       PageFactory pageFac = new PageFactory();
       pageFac.setRecordsFiltered(page.getTotal());
       pageFac.setRecordsTotal(page.getTotal());
       pageFac.setData(page.getRecords());

       return pageFac;
   }

浅谈

这期间还有遇见几个非常愚蠢的问题,折腾了很久,一个yaml的配置问题,一开始我是用mybaits的,后面改成mybatisPlus,但是对应的配置没有更新,导致一直报错,如下
在这里插入图片描述
这个出现,基本就是配置问题了,大家得仔细检查下,当然大家可以加我Q来问我,一起排查

你可能感兴趣的:(springboot)