Mybatis配置文件中Insert 元素標籤添加配置有哪些呢?

Insert 元素標籤添加方式

  • 添加記錄不返回主鍵配置
  • 添加記錄返回主鍵1(屬性配置)
  • 添加記錄返回主鍵2(屬性配置)
  • 批量添加記錄返回影響總記錄行數(屬性配置)

案例實操

添加記錄不返回主鍵配置

<insert id="addUserNoKey" parameterType="User">
insert into user(userName,userPwd) values(#{userName},#{userPwd})
</insert>java

添加記錄返回主鍵1(屬性配置)

<insert id="addUserHasKey1"  parameterType="user">
<selectKey keyProperty="id" order="AFTER" resultType="int">
select LAST_INSERT_ID() as id
</selectKey>
insert into user(userName,userPwd) values(#{userName},#{userPwd})
</insert>數組

Oracle 狀況it

<selectKey resultType="int" order="BEFORE" keyProperty="id">
SELECT LOGS_SEQ.nextval AS ID FROM DUAL
</selectKey>io

添加記錄返回主鍵2(屬性配置)

<insert id="addUserHasKey2" parameterType="user" useGeneratedKeys="true"keyProperty="id">
insert into user(userName,userPwd) values(#{userName},#{userPwd})
</insert>擴展

批量添加記錄返回影響總記錄行數(屬性配置)

<insert id="addUserBatch" parameterType="list" >
insert into user(user_name,user_pwd) values
<foreach collection="list" item="item" separator=",">
(#{item.userName},#{item.userPwd})
</foreach>
</insert>配置

擴展

Update 元素標籤使用

更新單條記錄返回影響行數

<!-- 更新單條 -->
<update id="updateUser" parameterType="User">
update user set userName=#{userName} ,userPwd=#{userPwd} where id=#{id}
</update>date

批量更新多條記錄 屬性配置

<update id="updateUserBatch" parameterType="java.util.Map">
update user set userPwd=#{pwd} where id in
<foreach collection="ids" item="item" open="(" separator=","  close=")">
#{item}
</foreach>
</update>foreach

Delete 元素標籤使用

刪除單條記錄

<delete id="delUserById" parameterType="int">
delete from user where id=#{id}
</delete>select

批量刪除多條記錄(屬性配置)

<delete id="delUserBatch" parameterType="java.util.Map">
delete from user where   id in
<foreach collection="ids" item="item" open="(" separator="," close=")"index="index">
#{item}
</foreach>
</delete>next

或下列形式,入參爲數組類型

<delete id="deleteUserBatch2" > delete from user where id in <foreach collection="array" item="item" open="(" separator="," close=")"> #{item} </foreach> </delete>

相關文章
相關標籤/搜索