qq_33521706 2021-06-27 13:11 采纳率: 42.9%
浏览 35
已采纳

ssm框架 mapper 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.imooc.o2o.dao.ProductDao">
<resultMap id="productMap" type="com.imooc.o2o.entity.Product">
		<id column="product_id" property="productId" />
		<result column="product_name" property="productName" />
		<result column="product_desc" property="productDesc" />
		<result column="img_addr" property="imgAddr" />
		<result column="normal_price" property="normalPrice" />
		<result column="promotion_price" property="promotionPrice" />
		<result column="priority" property="priority" />
		<result column="create_time" property="createTime" />
		<result column="last_edit_time" property="lastEditTime" />
		<result column="enable_status" property="enableStatus" />
		<result column="point" property="point" />
		<association property="productCategory" column="product_category_id"
			javaType="com.imooc.o2o.entity.ProductCategory">
			<id column="product_category_id" property="productCategoryId" />
			<result column="product_category_name" property="productCategoryName" />
			<result column="product_category_desc" property="productCategoryDesc" />
			<result column="priority" property="priority" />
			<result column="create_time" property="createTime" />
			<result column="last_edit_time" property="lastEditTime" />
		</association>
		<association property="shop" column="shop_id"
			javaType="com.imooc.o2o.entity.Shop">
			<id column="shop_id" property="shopId" />
			<result column="owner_id" property="ownerId" />
			<result column="shop_name" property="shopName" />
			<result column="shop_desc" property="shopDesc" />
			<result column="shop_addr" property="shopAddr" />
			<result column="phone" property="phone" />
			<result column="shop_img" property="shopImg" />
			<result column="longitude" property="longitude" />
			<result column="latitude" property="latitude" />
			<result column="priority" property="priority" />
			<result column="create_time" property="createTime" />
			<result column="last_edit_time" property="lastEditTime" />
			<result column="enable_status" property="enableStatus" />
			<result column="advice" property="advice" />
		</association>
		<collection property="productImgList" column="product_id"
			ofType="com.imooc.o2o.entity.ProductImg">
			<id column="product_img_id" property="productImgId" />
			<result column="img_addr" property="imgAddr" />
			<result column="img_desc" property="imgDesc" />
			<result column="priority" property="priority" />
			<result column="create_time" property="createTime" />
			<result column="product_id" property="productId" />
		</collection>
	</resultMap>
	<select id="queryProductList" resultMap="productMap">
		SELECT
		product_id,
		product_name,
		product_desc,
		img_addr,
		normal_price,
		promotion_price,
		priority,
		create_time,
		last_edit_time,
		enable_status,
		point,
		product_category_id,
		shop_id
		FROM
		tb_product
		<where>
			<if
				test="productCondition.shop!=null
				 and productCondition.shop.shopId!=null">
				and shop_id = #{productCondition.shop.shopId}
			</if>
			<if
				test="productCondition.productCategory!=null
				 and productCondition.productCategory.productCategoryId!=null">
				and product_category_id =
				#{productCondition.productCategory.productCategoryId}
			</if>
			<!-- 写like语句的时候 一般都会写成 like '% %' 在mybatis里面写就是应该是 like '%${name} %' 而不是 
				'%#{name} %' ${name} 是不带单引号的,而#{name} 是带单引号的 -->
			<if test="productCondition.productName!=null">
				and product_name like '%${productCondition.productName}%'
			</if>
			<if test="productCondition.enableStatus!=null">
				and enable_status = #{productCondition.enableStatus}
			</if>
		</where>
		ORDER BY
		priority DESC
		LIMIT #{rowIndex},#{pageSize};
	</select>

	<select id="queryProductCount" resultType="int">
		SELECT count(1) FROM tb_product
		<where>
			<if
				test="productCondition.shop!=null
				 and productCondition.shop.shopId!=null">
				and shop_id = #{productCondition.shop.shopId}
			</if>
			<if
				test="productCondition.productCategory!=null
				 and productCondition.productCategory.productCategoryId!=null">
				and product_category_id =
				#{productCondition.productCategory.productCategoryId}
			</if>
			<!-- 写like语句的时候 一般都会写成 like '% %' 在mybatis里面写就是应该是 like '%${name} %' 而不是 
				'%#{name} %' ${name} 是不带单引号的,而#{name} 是带单引号的 -->
			<if test="productCondition.productName!=null">
				and product_name like '%${productCondition.productName}%'
			</if>
			<if test="productCondition.enableStatus!=null">
				and enable_status = #{productCondition.enableStatus}
			</if>
		</where>
	</select>
	<select id="queryProductByProductId" resultMap="productMap"
		parameterType="Long">
		<!-- 具体的sql -->
		SELECT
		p.product_id,
		p.product_name,
		p.product_desc,
		p.img_addr,
		p.normal_price,
		p.promotion_price,
		p.priority,
		p.create_time,
		p.last_edit_time,
		p.enable_status,
		p.point,
		p.product_category_id,
		p.shop_id,
		pm.product_img_id,
		pm.img_addr,
		pm.img_desc,
		pm.priority,
		pm.create_time
		FROM
		tb_product p
		LEFT JOIN
		tb_product_img pm
		ON
		p.product_id =
		pm.product_id
		WHERE
		p.product_id =
		#{productId}
		ORDER BY
		pm.priority DESC
	</select>
	<insert id="insertProduct" parameterType="com.imooc.o2o.entity.Product"
		useGeneratedKeys="true" keyProperty="productId" keyColumn="product_id">
		INSERT INTO
		tb_product(product_name,product_desc,img_addr,
		normal_price,promotion_price,priority,create_time,
		last_edit_time,enable_status,product_category_id,
		shop_id)
		VALUES
		(#{productName},#{productDesc},#{imgAddr},
		#{normalPrice},#{promotionPrice},#{priority},#{createTime},
		#{lastEditTime},#{enableStatus},#{productCategory.productCategoryId},
		#{shop.shopId})
	</insert>
	<update id="updateProduct" parameterType="com.imooc.o2o.entity.Product" 
		useGeneratedKeys="true" keyProperty="productId">
		UPDATE tb_product
		<set>
			<if test="productName != null">product_name=#{productName},</if>
			<if test="productDesc != null">product_desc=#{productDesc},</if>
			<if test="imgAddr != null">img_addr=#{imgAddr},</if>
			<if test="normalPrice != null">normal_price=#{normalPrice},</if>
			<if test="promotionPrice != null">promotion_price=#{promotionPrice},</if>
			<if test="priority != null">priority=#{priority},</if>
			<if test="lastEditTime != null">last_edit_time=#{lastEditTime},</if>
			<if test="enableStatus != null">enable_status=#{enableStatus},</if>
			<if test="point != null">point=#{point},</if>
			<if
				test="productCategory != null
				 and productCategory.productCategoryId != null">
				product_category_id=#{productCategory.productCategoryId}
			</if>
		</set>
		WHERE product_id = #{productId}
		AND shop_id=#{shop.shopId}
	</update>
	<update id="updateProductCategoryToNull" parameterType="Long"
		keyProperty="product_id" useGeneratedKeys="true">
		UPDATE tb_product
		SET
		product_category_id = null
		WHERE product_category_id =
		#{productCategoryId}
	</update>

	<delete id="deleteProduct">
		DELETE FROM
		tb_product
		WHERE
		product_id = #{productId}
		AND shop_id=#{shopId}
	</delete>
	
</mapper>

<insert> 标签中添加了useGeneratedKeys="true" keyProperty="productId"这两个属性没有报错。而在<update>标签中添加了这两个属性就报错,为什么呢?在<update>标签中我也没有使用resultType属性,为什么会报错呢?请大佬帮我解答一下谢谢。

  • 写回答

1条回答 默认 最新

  • 404警告 2021-06-27 14:08
    关注

    useGeneratedKeys =true 这个表示插入数据之后返回一个自增的主键id给你对应实体类中的主键属性。通过这个设置可以解决在主键自增的情况下通过实体的getter方法获取主键。只在insert标签使用

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料