Mybatis配置文件中Insert 元素标签添加配置有哪些呢?
Mybatis配置文件中Insert 元素标签添加配置有哪些呢?
如需要跟多资料请点击下方图片⬇(扫码加好友→备注66,不备注拒绝添加哦)
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>
h collection="array" item="item" open="(" separator="," close=")">
#{item}
###