mybatis中遞歸查詢
阿新 • • 發佈:2018-09-15
mybatis utf-8 type vat ltm *** base ima myba
業務是這樣的,一個商品有不同的規格,所有規格選擇完後會出現價格,這些規格我是放在一個表裏,父子級關系。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); }
mybatis中遞歸查詢