mybatis,mysql之collection 与 association 不生效问题(仅是个人常犯错误)

因为自己马大哈,经常犯这个错误,原以为是拷贝的代码问题,最后发现是自己的遗漏问题!

看代码,先看不生效的

<resultMap type="Price" id="PriceResult">
        <id     property="priceId"        	column="price_id"      />
		<result property="shopId"         	column="shop_id"       />
		<result property="projectId"        column="project_id"       />
        <result property="levelId"     		column="level_id"    />
		<result property="unitId"     		column="unit_id"    />
		<result property="status"       	column="status"       />
		<result property="orderType"       	column="order_type"       />
		<result property="priceSort"    	column="price_sort"   />
		<result property="amount"    	column="amount"   />
		<result property="delFlag"      column="del_flag"     />
		<result property="createBy"     column="create_by"    />
		<result property="createTime"   column="create_time"  />
		<result property="updateBy"     column="update_by"    />
		<result property="updateTime"   column="update_time"  />
		<result property="remark"       column="remark"       />
		<association property="shopInfo"    javaType="Configbase"         resultMap="shopInfoResult" />
		<association property="projectInfo"    javaType="Project"         resultMap="projectInfoResult" />
		<association property="levelInfo"    javaType="Level"         resultMap="levelInfoResult" />
		<association property="unitInfo"    javaType="Unit"         resultMap="unitInfoResult" />
	</resultMap>

	<resultMap id="shopInfoResult" type="Configbase">
		<id property="shopId"    column="shop_id"       />
		<result property="shopName"     	column="shop_name"    />
	</resultMap>

	<resultMap id="projectInfoResult" type="Project">
		<id property="projectId"    column="project_id"       />
		<result property="projectTitle"     	column="project_title"    />
	</resultMap>

	<resultMap id="levelInfoResult" type="Level">
		<id property="levelId"    column="level_id"       />
		<result property="levelTitle"     	column="level_title"    />
	</resultMap>

	<resultMap id="unitInfoResult" type="Unit">
		<id property="unitId"    column="unit_id"       />
		<result property="unitTitle"     	column="unit_title"    />
	</resultMap>
	
	<sql id="selectPriceVo">
        select g.price_id, g.shop_id, g.project_id, g.level_id, g.unit_id, g.order_type, g.status, g.price_sort, g.amount,
			   g.del_flag, g.create_by, g.update_by, g.create_time, g.update_time, g.remark,
			   s.shop_name,
			   p.project_title,
			   l.level_title,
			   u.unit_title
		from tb_market_price g
		left join tb_shop_config_base s on s.shop_id = g.shop_id
		left join tb_market_project p on p.project_id = g.project_id
		left join tb_market_level l on l.level_id = g.level_id
		left join tb_market_unit u on u.unit_id = g.unit_id
    </sql>
    
    <select id="selectPriceList" parameterType="Price" resultMap="PriceResult">
		select g.price_id, g.shop_id, g.project_id, g.level_id, g.unit_id, g.order_type, g.status, g.price_sort, g.amount,
		       g.del_flag, g.create_by, g.update_by, g.create_time, g.update_time, g.remark,
			   s.shop_name
		from tb_market_price g
		left join tb_shop_config_base s on s.shop_id = g.shop_id
		where g.del_flag = '0' and 1 = 1
		<if test="priceId != null and priceId  != ''">
			AND g.price_id = #{priceId}
		</if>
		<if test="shopId != null and shopId != ''">
			AND g.shop_id = #{shopId}
		</if>
		<if test="projectId != null and projectId != ''">
			AND g.project_id = #{projectId}
		</if>
		<if test="levelId != null and levelId != ''">
			AND g.level_id = #{levelId}
		</if>
		<if test="unitId != null and unitId != ''">
			AND g.unit_id = #{unitId}
		</if>
		<if test="status != null and status != ''">
			AND g.status = #{status}
		</if>
		<if test="orderType != null and orderType != ''">
			AND g.order_type = #{orderType}
		</if>
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			AND date_format(g.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			AND date_format(g.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

再看生效的

 <resultMap type="Price" id="PriceResult">
        <id     property="priceId"        	column="price_id"      />
		<result property="shopId"         	column="shop_id"       />
		<result property="projectId"        column="project_id"       />
        <result property="levelId"     		column="level_id"    />
		<result property="unitId"     		column="unit_id"    />
		<result property="status"       	column="status"       />
		<result property="orderType"       	column="order_type"       />
		<result property="priceSort"    	column="price_sort"   />
		<result property="amount"    	column="amount"   />
		<result property="delFlag"      column="del_flag"     />
		<result property="createBy"     column="create_by"    />
		<result property="createTime"   column="create_time"  />
		<result property="updateBy"     column="update_by"    />
		<result property="updateTime"   column="update_time"  />
		<result property="remark"       column="remark"       />
		<association property="shopInfo"    javaType="Configbase"         resultMap="shopInfoResult" />
		<association property="projectInfo"    javaType="Project"         resultMap="projectInfoResult" />
		<association property="levelInfo"    javaType="Level"         resultMap="levelInfoResult" />
		<association property="unitInfo"    javaType="Unit"         resultMap="unitInfoResult" />
	</resultMap>

	<resultMap id="shopInfoResult" type="Configbase">
		<id property="shopId"    column="shop_id"       />
		<result property="shopName"     	column="shop_name"    />
	</resultMap>

	<resultMap id="projectInfoResult" type="Project">
		<id property="projectId"    column="project_id"       />
		<result property="projectTitle"     	column="project_title"    />
	</resultMap>

	<resultMap id="levelInfoResult" type="Level">
		<id property="levelId"    column="level_id"       />
		<result property="levelTitle"     	column="level_title"    />
	</resultMap>

	<resultMap id="unitInfoResult" type="Unit">
		<id property="unitId"    column="unit_id"       />
		<result property="unitTitle"     	column="unit_title"    />
	</resultMap>
	
	<sql id="selectPriceVo">
        select g.price_id, g.shop_id, g.project_id, g.level_id, g.unit_id, g.order_type, g.status, g.price_sort, g.amount,
			   g.del_flag, g.create_by, g.update_by, g.create_time, g.update_time, g.remark,
			   s.shop_name,
			   p.project_title,
			   l.level_title,
			   u.unit_title
		from tb_market_price g
		left join tb_shop_config_base s on s.shop_id = g.shop_id
		left join tb_market_project p on p.project_id = g.project_id
		left join tb_market_level l on l.level_id = g.level_id
		left join tb_market_unit u on u.unit_id = g.unit_id
    </sql>
    
    <select id="selectPriceList" parameterType="Price" resultMap="PriceResult">
		<include refid="selectPriceVo"/>
		where g.del_flag = '0' and 1 = 1
		<if test="priceId != null and priceId  != ''">
			AND g.price_id = #{priceId}
		</if>
		<if test="shopId != null and shopId != ''">
			AND g.shop_id = #{shopId}
		</if>
		<if test="projectId != null and projectId != ''">
			AND g.project_id = #{projectId}
		</if>
		<if test="levelId != null and levelId != ''">
			AND g.level_id = #{levelId}
		</if>
		<if test="unitId != null and unitId != ''">
			AND g.unit_id = #{unitId}
		</if>
		<if test="status != null and status != ''">
			AND g.status = #{status}
		</if>
		<if test="orderType != null and orderType != ''">
			AND g.order_type = #{orderType}
		</if>
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			AND date_format(g.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			AND date_format(g.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

原因是因为只写了查询单个的以下这段,而查列表的时候没有

select g.price_id, g.shop_id, g.project_id, g.level_id, g.unit_id, g.order_type, g.status, g.price_sort, g.amount,
			   g.del_flag, g.create_by, g.update_by, g.create_time, g.update_time, g.remark,
			   s.shop_name,
			   p.project_title,
			   l.level_title,
			   u.unit_title
		from tb_market_price g
		left join tb_shop_config_base s on s.shop_id = g.shop_id
		left join tb_market_project p on p.project_id = g.project_id
		left join tb_market_level l on l.level_id = g.level_id
		left join tb_market_unit u on u.unit_id = g.unit_id

核心代码,修改下即可 

<include refid="selectPriceVo"/>

仅是个人常犯错误,记录下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值