zoukankan      html  css  js  c++  java
  • mybatis中递归查询

    业务是这样的,一个商品有不同的规格,所有规格选择完后会出现价格,这些规格我是放在一个表里,父子级关系。mybatis做的时候传过来一个商品Id.然后根据商品id去找所有的规格。

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.***.abc.dao.GoodsSpecCategoryMapper" >
      <resultMap id="BaseResultMap" type="com.***.abc.bean.GoodsSpecCategory" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="spec_name" property="specName" jdbcType="VARCHAR" />
        <result column="pid" property="pid" jdbcType="BIGINT" />
        <result column="goods_info_id" property="goodsInfoId" jdbcType="BIGINT" />
        <collection property="categoryList" ofType="GoodsSpecCategory" javaType="java.util.List" column="id" select="getById"/>
      </resultMap>
    
      <!--根据父类id查找其子类别-->
      <select id="getById" resultMap="BaseResultMap" parameterType="long">
            SELECT *
            FROM goods_spec_category
            WHERE pid = #{id}
        </select>
    
        <!--查找所有类别(递归)-->
        <select id="querySpecCategoryListById" resultMap="BaseResultMap" parameterType="long">
          SELECT *  //这里查出来的ID会作为getById的Id去查询子类
          FROM goods_spec_category
          WHERE goods_info_id = #{infoId} AND pid IS NULL    //infoId是传过来的商品id
    
        </select>
    
    </mapper>

    商品表的xml里的

    <resultMap id="baseResultMap" type="com.***.abc.dto.GoodsInfoDto" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="goods_type" property="goodsType" jdbcType="VARCHAR" />
          <collection property="goodsSpecCategory" fetchType="eager" ofType="com.***.abc.bean.GoodsSpecCategory" column="id"
                      javaType="ArrayList" select="com.***.abc.dao.GoodsSpecCategoryMapper.querySpecCategoryListById"/>
    
      </resultMap>
    GoodsSpecCategoryMapper中添加接口
    public interface GoodsSpecCategoryMapper {
    
         public List querySpecCategoryListById(@Param("infoId") Long infoId);
    
    }
    ...
  • 相关阅读:
    【Win 10 应用开发】获取本机的IP地址
    【Win 10应用开发】延迟共享
    【Win 10 应用开发】共享目标(UWP)
    【Win 10应用开发】响应系统回退键的导航事件
    编写Windows服务疑问2:探索服务与安装器的关系
    编写Windows服务疑问1:操作过程
    服务器常见错误代码500、501、502、503、504、505
    git reset与git revert的区别
    Redis集群方案怎么做?
    ThinkPHP设计模式与Trait技术
  • 原文地址:https://www.cnblogs.com/javage/p/9651813.html
Copyright © 2011-2022 走看看