zoukankan      html  css  js  c++  java
  • 使用Mybatis分页插件[PageHelper]处理一对多关系

    闲聊:这种问题基本上都比较常见,以前遇到这个问题的时候总是故意绕弯来避免,后来经过我一天的百度浏览,终于懂了,搜索百度,我们会看到很多关于这个问题的解决方案,基本上都是说使用resultMap的子查询方法,经过我亲身实验,确实有用,但是很多博客解决方案是提出来了,但思路不够具体,只是草草的说了几句,比如,使用resultMap子查询来解决,具体的解决方法应该是啥,我之前从来没用过resultMap的子查询,搞了半天也没弄明白,后来终于看到一个有用的博客了,现在我就来更具体的给大家分享这个解决方案

    1.resultMap

    首先给大家更具体的介绍一下resultMap里面的包含的元素以及词义:

    <!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
    <resultMap id="唯一的标识" type="映射的pojo对象">
    <id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
    <result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属                        
    性)"/>
    <association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
    <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>
    <result  column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
    </association>
    <!-- 集合中的property须为oftype定义的pojo对象的属性-->
    <collection property="pojo的集合属性" ofType="集合中的pojo对象">
    <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
    <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />  
    </collection>
    </resultMap>
    
    <strong>如果collection标签是使用嵌套查询,格式如下:</strong>
    
    <collection column="传递给嵌套查询语句的字段参数" property="pojo对象中集合属性" ofType="集合属性中的pojo对象" select="嵌套的查询语句" > 
    </collection>

    好了,理解了以上各元素的意思后,开始写代码了【注意:这里只写子查询哦!

    2.创建两个存在父子关系的表和类

    这里就不写在数据库的创表数据了

    • 主表字段信息:
    @Data
    public class TbNoticeVO extends BaseVo {
    
        @ApiModelProperty(value = "自增主键")
        private Long id;
    
        @ApiModelProperty(value = "公告内容")
        private String notice;
    
        @ApiModelProperty(value = "创建时间")
        private java.time.LocalDateTime createDate;
    
        @ApiModelProperty(value = "修改时间")
        private java.time.LocalDateTime modifyDate;
    
    }
    
    • 子表字段信息:
    @Data
    public class TbNoticeFileVO extends BaseVo {
    
        @ApiModelProperty(value = "自增主键")
        private Long id;
    
        @ApiModelProperty(value = "公告表id")
        private Long noticeId;
    
        @ApiModelProperty(value = "标题")
        private String title;
    
        @ApiModelProperty(value = "文件上传路径url")
        private String fileUrl;
    
        @ApiModelProperty(value = "创建时间")
        private java.time.LocalDateTime createDate;
    }
    

     因为要关联两张表一起查询,所以在此,我们可以创建一个bo类

    @Data
    public class TbNoticeBO implements Serializable {
        private static final Long serialVersionUID = 1L;
        @ApiModelProperty(value = "自增主键")
        private Long id;
    
        @ApiModelProperty(value = "公告内容")
        private String notice;
    
        @ApiModelProperty(value = "创建时间")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private java.time.LocalDate createDate;
    
        private List<TbNoticeFileVO> fileVOList;
    }

     3.mapper层接口

    public interface TbNoticeMapper {
    
        List<TbNoticeBO> getNoticeFile(TbNotice model);
    }
    

     4.service层接口

    public interface ITbNoticeService {
        
        Page groupbypage(TbNoticeVO model);
    
    }
    

     5.serviceImpl服务实现层

    @Service
    public class TbNoticeService extends AbstractBaseService implements ITbNoticeService {
    
        @Autowired
        private TbNoticeMapper tbNoticeMapper;
    
        @Autowired
        private TbNoticeFileMapper tbNoticeFileMapper;
      
        @Override
        public Page groupbypage(TbNoticeVO model) {
            TbNotice param = copyProperties(model, TbNotice.class);
            PageHelper.startPage(model.getPageNum(), model.getPageSize());
            List<TbNoticeBO> list = tbNoticeMapper.getNoticeFile(param);
            PageInfo pageInfo = new PageInfo(list);
            return new Page(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getNavigateLastPage(), pageInfo.getTotal(), list);
        }
    
    }
    

     6.mapping映射文件

     <!--公告通知表的resultMap映射-->
        <resultMap id="getGroupByList" type="com.bestva.service.bestvascm.entity.bo.TbNoticeBO">
            <id property="id" column="id"></id>
            <result property="notice" column="notice"></result>
            <result property="createDate" column="create_date"></result>
            <collection property="fileVOList" ofType="com.bestva.service.bestvascm.entity.vo.TbNoticeFileVO"
                        javaType="java.util.List" column="{noticeId=id}" select="getByNoticeId">
            </collection>
        </resultMap>
    
        <select id="getNoticeFile" resultMap="getGroupByList"
                parameterType="com.bestva.service.bestvascm.entity.dto.TbNotice">
            select n.id,n.notice,n.create_date
            from tb_notice n
            where 1=1
            <if test="id != null">
                n.id=#{id}
            </if>
            ORDER BY create_date DESC
        </select>
    
        <resultMap id="NoticeFileResultMap" type="com.bestva.service.bestvascm.entity.vo.TbNoticeFileVO">
            <id property="id" column="id"></id>
            <result property="noticeId" column="notice_id"></result>
            <result property="title" column="title"></result>
            <result property="fileUrl" column="file_url"></result>
            <result property="createDate" column="create_date"></result>
        </resultMap>
    
        <select id="getByNoticeId" resultMap="NoticeFileResultMap">
            select f.id,f.notice_id,f.title,f.file_url
            from tb_notice_file f
            where f.notice_id=#{noticeId}
        </select>
    

      7.子查询详解【图片版】

    8.Cotroller返回json数据

       @PostMapping("/tbNotice/groupbypage")
        @AutoLog(description = "分页查询公告通知信息接口")
        @ApiOperation(value = "分页查询公告通知信息接口")
        public BaseResponse<List<TbNoticeBO>> groupByPage(@RequestBody TbNoticeVO request) {
            return new BaseResponse(true, "查询成功", 0, tbNoticeService.groupbypage(request));
        }
    

      使用postMam得出的json数据

    --我是一只在深圳打拼的蜗牛,一起加油吧!

    未经允许不可转载哦!

         

  • 相关阅读:
    Elasticsearch入门系列(一)
    清楚Chrome缓存
    解决IIS启动站点报错
    Input type="file"上传文件change事件只触发一次解决方案
    本地计算机上的XXX服务启动后停止,某些服务在未由其它服务或程序使用时将自动停止
    SQL Server Datetime类型为NULL不能用ISNULL(datetime,' ')来判断,会导致1900-01-01
    浏览指南
    谁发明的c++
    c++的用处
    不一样的二叉树遍历(小学生都会)
  • 原文地址:https://www.cnblogs.com/wnlsq/p/13858350.html
Copyright © 2011-2022 走看看