mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map

时间:2023-06-17 11:52:32

用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量、价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错

商品表的mapper文件为GooodsMapper.xml 规格表的mapper 文件为GoodsSpecificationsMapper.xml

java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map

GooodsMapper.xml的内容如下

<?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.chengpin.mapper.GoodsMapper">
<insert id="save" parameterType="Goods">
insert into t_goods
(no,category,title,selling_point,cover_picture,unit_price,freight_template,shop_category,
service_commitment,return_commitment,guarantee_service,
provide_invoice,is_shelf,shelf_time,state,purchasing_place,release_time,publisher)
values
(GOODS_SEQUENCE.nextval,#{category},#{title},#{selling_point},#{cover_picture},#{unit_price},
#{freight_template},#{shop_category},#{service_commitment},
#{return_commitment},#{guarantee_service},#{provide_invoice},#{is_shelf},#{shelf_time},#{state},#{purchasing_place},
#{release_time},#{publisher})
<selectKey keyProperty="no" order="AFTER" resultType="java.lang.String">
select GOODS_SEQUENCE.CURRVAL from DUAL
</selectKey>
</insert> <select id="selectGoodsInfoTotalSize" parameterType="Map" resultType="Integer">
select
count(*)
from
T_GOODS
WHERE
category = #{categoryno}
</select> <select id="selectGoodsInfo" parameterType="Map" resultType="Map">
select
goodsno,goodstitle,goodsprice,goodsimgurl
from (
select
g.cover_picture goodsimgurl,
g.no goodsno,
g.title goodstitle,
g.unit_price goodsprice,
rownum num
from T_GOODS g
where g.category = #{categoryno}
and rownum &lt;= #{endPage}
order by
<if test="@com.chengpin.core.util.StringUtils@isNotEmpty(priceUp)">
g.unit_price desc,
</if>
<if test="@com.chengpin.core.util.StringUtils@isNotEmpty(priceDown)">
g.unit_price asc,
</if>
<if test="@com.chengpin.core.util.StringUtils@isNotEmpty(last)">
g.release_time desc,
</if>
g.no desc
)
where
num &gt;= #{startPage}
</select> <!-- 根据用户编号和商品状态查询商品信息 -->
<select id="selectGoodsByState" resultType="Goods">
select * from t_goods where publisher=#{user_no} and state=#{state}
</select>
<select id="selectGoodsDetailsByGoodsNo" parameterType="Map" resultType="Map">
select
g.no "no",
g.selling_point "sellpoint",
g.title "title",
g.unit_price "original_price",
gs.price "present_price",
gs.quantity "quantity",
tgi.content "goodscontent"
from
t_Goods g
left join
T_GOODS_SPECIFICATIONS gs
on g.no = gs.goods_no
left join
T_GOODS_INTRODUCTION tgi
on tgi.goods_no = g.no
where g.no = #{goodsno}
<if test="@com.chengpin.core.util.StringUtils@isNotEmpty(goodscolor)">
and gs.color_value = #{goodscolor}
</if>
and rownum = 1
</select> <select id="selectGoodsInfoAndTotalPriceByUserNoState" resultMap="goodsInfoAndTotalPrice">
select g.no goods_no,g.title,g.release_time,g.shelf_time,g.off_shelf,g.cover_picture
from t_goods g where g.publisher = #{user_no} and g.state=#{state}
group by g.no,g.title,g.release_time,g.shelf_time,g.off_shelf,g.cover_picture
order by g.release_time desc
</select> <resultMap type="Goods" id="goodsInfoAndTotalPrice">
<id property="no" column="no" />
<result property="title" column="title"/>
<result property="release_time" column="release_time"/>
<result property="shelf_time" column="shelf_time"/>
<result property="off_shelf" column="off_shelf"/>
<association property="total" column="goods_no" javaType="Integer"
select="mybatis.mapper.GoodsSpecificationsMapper.selectGoodsTotalByGoodsNo"/>
<association property="lowest_price" column="goods_no" javaType="Double"
select="com.chengpin.mapper.GoodsSpecificationsMapper.selectLowestPrice"/>
</resultMap>
</mapper>

GoodsSpecificationsMapper.xml 内容如下

<?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.chengpin.mapper.GoodsSpecificationsMapper">
<!-- 保存商品规格方法 -->
<insert id="save">
insert into t_goods_specifications
(no,goods_no,color_value,size_value,quantity,price)
values
(goods_specifications_sequence.nextval,#{goods_no},
#{specifications.color_value},#{specifications.size_value},#{specifications.quantity},#{specifications.price})
</insert> <!-- 根据商品编号查询商品总数 -->
<select id="selectGoodsTotalByGoodsNo" parameterType="String" resultType="Integer">
select sum(quantity) total from t_goods_specifications where goods_no = #{goods_no}
</select> <!-- 根据商品编号查询商品最低价 -->
<select id="selectLowestPrice" parameterType="String" resultType="Double">
select min(price) from t_goods_specifications where goods_no = #{goods_no}
</select>
</mapper>

最后发现错误来源于此

        <association property="total" column="goods_no" javaType="Integer"
select="mybatis.mapper.GoodsSpecificationsMapper.selectGoodsTotalByGoodsNo"/>

原来是我吧select 中 对GoodsSpecificationsMapper.xml 文件的 select 查询调用的 空间写错了

我写的是src 路径 并不是 写的 mapper 的namespace 空间位置 把值改如下 便正确了

        <association property="total" column="goods_no" javaType="Integer"
select="com.chengpin.mapper.GoodsSpecificationsMapper.selectGoodsTotalByGoodsNo"/>