1.自增类型
MySQL SQLSever DB2等数据库提供了自增类型,Oracle中使用Sequence。
MySQL中使用 auto_increment为ID创建自增ID,在Mybatis中 属性 useGeneratedKeys="true"用于支持自增类型,可以再插入数据以后可以读取自动增加的ID;keyProperty为自增字段属性对应的bean属性。
2.动态SQL
- <if> 单路分支
<if test="body!=null">
cn_note_body=#{body},
</if>
- <choose><when>多路分支
<choose>
<when test="body!=null">
cn_note_body=#{body},
</when>
<when test="title!=null">
cn_note_title=#{title},
</when>
</when>
- <trim> 去掉多余的 "," "and" "or"
<trim suffixOverrides=",">
<if test="body!=null">
cn_note_body=#{body},
</if>
<if test="title!=null">
cn_note_title=#{title},
</if>
cn_note_last_modify_time = #{lastModifyTime}
</trim>
- <foreach> 批量删除中可以使用
将
delete fro cn_note where cn_note_id=?
delete fro cn_note where cn_note_id=?
delete fro cn_note where cn_note_id=?
替换为
delete fro cn_note where cn_note_id in (?,?,?) 访问数据库一次,效率高
<!-- 持久层方法 int deleteNotes(List<String> list) -->
<delete id="deleteNotes">
delete from cn_note where cn_note_id in
<!-- 以"("开始,以")"结尾,中间用","间隔,变量名为"id"(类似迭代器) -->
<foreach collection="list" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</delete>