共计 1838 个字符,预计需要花费 5 分钟才能阅读完成。
Insert 元素标签增加形式
- 增加记录不返回主键配置
- 增加记录返回主键 1(属性配置)
- 增加记录返回主键 2(属性配置)
- 批量增加记录返回影响总记录行数(属性配置)
案例实操
增加记录不返回主键配置
<insert id=”addUserNoKey” parameterType=”User”>
insert into user(userName,userPwd) values(#{userName},#{userPwd})
</insert>
增加记录返回主键 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 状况
<selectKey resultType=”int” order=”BEFORE” keyProperty=”id”>
SELECT LOGS_SEQ.nextval AS ID FROM DUAL
</selectKey>
增加记录返回主键 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>
批量更新多条记录 属性配置
<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 元素标签应用
删除单条记录
<delete id=”delUserById” parameterType=”int”>
delete from user where id=#{id}
</delete>
批量删除多条记录(属性配置)
<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>或下列模式,入参为数组类型
<delete id=”deleteUserBatch2″ >
delete from user where id in
<foreach collection=”array” item=”item” open=”(” separator=”,” close=”)”>
#{item}
</foreach>
</delete>