Insert 元素标签添加方式
添加记录不返回主键配置
添加记录返回主键1(属性配置)
添加记录返回主键2(属性配置)
批量添加记录返回影响总记录行数(属性配置)
案例实操
添加记录不返回主键配置
<insertid="addUserNoKey"parameterType="User">
insert into user(userName,userPwd) values(#{userName},#{userPwd})
</insert>
添加记录返回主键1(属性配置)
<insertid="addUserHasKey1"parameterType="user">
<selectKeykeyProperty="id"order="AFTER"resultType="int">
select LAST_INSERT_ID() as id
</selectKey>
insert into user(userName,userPwd) values(#{userName},#{userPwd})
</insert>
Oracle 情况
<selectKeyresultType="int"order="BEFORE"keyProperty="id">
SELECT LOGS_SEQ.nextval AS ID FROM DUAL
</selectKey>
添加记录返回主键2(属性配置)
<insertid="addUserHasKey2"parameterType="user"useGeneratedKeys="true"keyProperty="id">
insert into user(userName,userPwd) values(#{userName},#{userPwd})
</insert>
批量添加记录返回影响总记录行数(属性配置)
<insertid="addUserBatch"parameterType="list">
insert into user(user_name,user_pwd) values
<foreachcollection="list"item="item"separator=",">
(#{item.userName},#{item.userPwd})
</foreach>
</insert>
###
扩展
Update 元素标签使用
更新单条记录返回影响行数
<!-- 更新单条 -->
<updateid="updateUser"parameterType="User">
update user set userName=#{userName} ,userPwd=#{userPwd} where id=#{id}
</update>
批量更新多条记录 属性配置
<updateid="updateUserBatch"parameterType="java.util.Map">
update user set userPwd=#{pwd} where id in
<foreachcollection="ids"item="item"open="("separator=","close=")">
#{item}
</foreach>
</update>
Delete 元素标签使用
删除单条记录
<deleteid="delUserById"parameterType="int">
delete from user where id=#{id}
</delete>
批量删除多条记录(属性配置)
<deleteid="delUserBatch"parameterType="java.util.Map">
delete from user where id in
<foreachcollection="ids"item="item"open="("separator=","close=")"index="index">
#{item}
</foreach>
</delete>
或下列形式,入参为数组类型
<deleteid="deleteUserBatch2">
delete from user where id in
<foreachcollection="array"item="item"open="("separator=","close=")">
#{item}
</foreach>
</delete>
###