发表于: 2020-06-25 22:26:14
1 1684
今天完成的事情:今天顺利完成第一次小课堂,有些小紧张,中间代码运行出错,还好自己稳住解决了,然后就是做小课堂的后续工作,因为第一次做,还不知道完整步骤。
明天计划的事情:看看能不能把任务四给了结了。
遇到的问题:暂无
收获:今天收获就是更加熟悉动态SQL,对Mybatis加深了记忆,讲出来的东西是不一样,虽然讲的很菜,但是自己收获最大。
<!--动态update-set-->
<update id="updateOne" parameterType="Item">
update item
<set>
<if test="name != null">name=#{name}</if>
<if test="state != null">state=#{state}</if>
</set>
where id=#{id}
</update>
<!--sql重用-->
<sql id="forItem">id,name,intro,catalog_id,shop_id,price,state,create_at,update_at</sql>
<!--if标签,根据状态(上架/下架)和商品的分类和店铺名查找所有符合条件的商品-->
<select id="selectMore" resultType="Item" >
select <include refid="forItem"></include> from item
where state = #{state}
<if test="name!=null">
and name=#{name}
</if>
<if test="catalogId != 0">
and catalog_id=#{catalogId}
</if>
<if test="shopId != 0">
and shop_id=#{shopId}
</if>
<if test="craeateAt !=0 ">
and create_at=#{craeateAt}
</if>
</select>
<!--where标签-->
<select id="selectMore" resultType="Item" >
select <include refid="forItem"></include> from item
<where>
<if test="state !=0 ">
and state = #{state}
</if>
<if test="name!=null and name!=''">
and name=#{name}
</if>
<if test="catalogId != 0">
and catalog_id=#{catalogId}
</if>
<if test="shopId != 0">
and shop_id=#{shopId}
</if>
</where>
</select>
<!-- 动态choose-when-otherwise-->
<select id="selectMore" resultType="Item">
select <include refid="forItem"></include> from item
<where>
<choose>
<when test="state != 0">
state = #{state}
</when>
<otherwise>
state = #{state}
</otherwise>
</choose>
</where>
</select>
<!--trim标签使用-->
<update id="updateOne" parameterType="Item">
update item
<trim prefix="set" suffixOverrides=",">
<if test="name != null">name=#{name},</if>
<if test="state != null">state=#{state},</if>
</trim>
where id=#{id}
</update>
<!-- 上面的可用trim替换 -->
<select id="selectMore" resultType="Item" >
select <include refid="forItem"></include> from item
<trim prefix="where" prefixOverrides="and">
<if test="state !=0 ">
and state = #{state}
</if>
<if test="name!=null and name!=''">
and name=#{name}
</if>
<if test="catalogId != 0">
and catalog_id=#{catalogId}
</if>
<if test="shopId != 0">
and shop_id=#{shopId}
</if>
</trim>
</select>
<!--foreach标签,批量插入-->
<sql id="insertItem">name,intro,catalog_id,shop_id,price,state,create_at,update_at</sql>
<insert id="insertMore" parameterType="List" useGeneratedKeys="true" keyProperty="id">
insert into item (<include refid="insertItem"></include>)
values
<foreach collection="list" item="item" separator=",">
(#{item.name},#{item.intro},#{item.catalogId},#{item.shopId},#{item.price},#{item.state},#{item.createAt},#{item.updateAt})
</foreach>
</insert>
<!--foreach标签,批量删除-->
<delete id="deleteMore" >
delete from item where id in
<foreach collection="array" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
评论