1.知识点:通过Mybatis执行多条sql语句需要增加一个参数:allowMultiQueries,然后在mapper中写多个sql语句时用分号将其隔开就行
<property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8&allowMultiQueries=true"/>

关于Html中的字符实体,注意&amp; 这个分号!!!!!

Mybatis动态sql语句
1.if标签与where标签,个人觉得干脆就一起使用 where适用于里面有多个if标签的情况
<select id="listProductByUseIf" parameterType="map" resultType="Product">
SELECT id,name,price FROM product_
<where>
<if test="name!=null and !"".equals(name.trim())">
AND name LIKE concat('%',#{name},'%')
</if>
<if test="price!=null and price!=0">
AND price>#{price}
</if>
</where>
</select>
下面是关于where标签的详细解释
关于set标签,如下:即if里的条件不满足,就不会更新,则是保持数据库的原值

<update id="updateProduct" parameterType="map">
UPDATE product_
<set>
<if test="name!=null and !"".equals(name.trim())">
name=#{name},
</if>
<if test="price!=null and price!=0">
price=#{price},
</if>
</set>
WHERE id=#{id}
</update>
知识点:
一次删除多条的Sql语句:
DELETE FROM category_ WHERE id in (9,10,11)
当然select也一样:
SELECT * FROM category_ WHERE id in (1,2)

关于foreach标签的使用 这篇写的很详细!!!!!:mybatis foreach标签的使用
在Imooc上学习的例子:
<select id="listProductByIds" parameterType="list" resultType="Product">
SELECT * FROM product_ WHERE id IN (
<foreach collection="list" item="item" separator=",">
#{item}
</foreach>
)
</select>
下面这个也是一个教程里的写法,但我觉得上面的好一点,可以直接把()给写出来:
<select id="listProduct" resultType="Product">
SELECT * FROM product_
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
1万+




被折叠的 条评论
为什么被折叠?



