[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KQkpLbgK-1602572239472)(https://imgkr.cn-bj.ufileos.com/768074eb-5e03-4ad1-81ee-5432b6614e4a.jpg)]
## Insert 元素标签添加方式
- 添加记录不返回主键配置
- 添加记录返回主键1(属性配置)
- 添加记录返回主键2(属性配置)
- 批量添加记录返回影响总记录行数(属性配置)
## 案例实操
#### 添加记录不返回主键配置
~~~ xml
<insert id="addUserNoKey" parameterType="User">
insert into user(userName,userPwd) values(#{userName},#{userPwd})
</insert>
~~~
#### 添加记录返回主键1(属性配置)
~~~ xml
<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** **情况**
~~~ xml
<selectKey resultType="int" order="BEFORE" keyProperty="id">
SELECT LOGS_SEQ.nextval AS ID FROM DUAL
</selectKey>
~~~
#### 添加记录返回主键2(属性配置)
~~~ xml
<insert id="addUserHasKey2" parameterType="user" useGeneratedKeys="true"keyProperty="id">
insert into user(userName,userPwd) values(#{userName},#{userPwd})
</insert>
~~~
#### 批量添加记录返回影响总记录行数(属性配置)
~~~ xml
<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 元素标签使用
#### 更新单条记录返回影响行数
~~~ xml
<!-- 更新单条 -->
<update id="updateUser" parameterType="User">
update user set userName=#{userName} ,userPwd=#{userPwd} where id=#{id}
</update>
~~~
#### 批量更新多条记录 属性配置
~~~ xml
<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>
~~~
### Delete 元素标签使用
#### 删除单条记录
~~~ xml
<delete id="delUserById" parameterType="int">
delete from user where id=#{id}
</delete>
~~~
#### 批量删除多条记录(属性配置)
~~~ xml
<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>
~~~
或下列形式,入参为数组类型
~~~ xml
<delete id="deleteUserBatch2" >
delete from user where id in
<foreach collection="array" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
~~~
h collection="array" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
~~~
###
需要视频配套文档或更多资料+我们程序员小姐姐v:lezijie007(加好友时备注:b站-LT,不备注拒绝添加哟)