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);

}

 

posted on 2018-09-15 18:58  Java挖掘机  阅读(691)  评论(0编辑  收藏  举报

导航