# Mybatis使用之增刪改詳解
### 一:簡介
?????? 主要記錄Mybatis的增刪改使用到的標簽屬性及意義。參數方面沒有過多涉及、后面還是會有單獨章節來記錄參數的類型及使用。
?????? 這里沒有查詢、因為查詢算是比較重要的使用方式、同時也比較復雜、牽涉到ResultMap的配置、關聯查詢等。分開的目的是想讓博客每一篇都有重點、調理清晰一點。
### 二:新增
### ???????2.1 insert標簽屬性及意義:
????? 
### ???????2.2 insert之返回生成主鍵:
?????????????2.2.1對于MySql或者SQL Server這種主鍵自增長的數據庫可以使用如下方式獲取主鍵(其中兩個關鍵標簽屬性useGeneratedKeys 和keyProperty的意義見上面insert標簽屬性):
~~~
<insert id="addAuthorWithAutoGeneratedKey" useGeneratedKeys="true" keyProperty="id">
INSERT INTO author (username, password, email, bio, favourite_section)
VALUES (#{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
~~~
?????????????2.2.2對于Oracle這種不支持主鍵自增長的數據庫可以使用下面這種先生成一個主鍵再插入表并將生成主鍵返回的方式來笨處理:
~~~
<insert id="addAuthorWithoutAutoGeneratedKey">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
</selectKey>
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
~~~
?????????????2.2.3對于Oracle一般主鍵都會使用Sequence來實現、此時返回新增記錄的主鍵的方式如下:
????????????
~~~
<insert id="addAuthor" parameterType="author">
INSERT INTO author(id, username) VALUES (seq_author.nextval, #{username})
<selectKey keyProperty="id" resultType="int">
SELECT max(id) FROM author
</selectKey>
</insert>
~~~
?????????????2.2.4 selectKey:

### ???????2.3 實例:
?????????????2.3.1 Mysql中向Author表中插入一條數據并返回主鍵
~~~
<insert id="addAuthorWithAutoGeneratedKey" useGeneratedKeys="true" keyProperty="id">
INSERT INTO author (username, password, email, bio, favourite_section)
VALUES (#{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
~~~
?????????????2.3.2Oracle中向Author表中插入一條數據并返回主鍵
~~~
<insert id="addAuthor" parameterType="author">
INSERT INTO author(id, username) VALUES (seq_author.nextval, #{username})
<selectKey keyProperty="id" resultType="int">
SELECT max(id) FROM author
</selectKey>
</insert>
~~~
### 三:修改、刪除
??????將兩個放在一起的原因是兩者使用的方式比較簡單、其中修改使用方式基本和新增差不多。
### ???????3.1 修改
?????????????3.1.1update標簽及標簽屬性
~~~
<update
id=""
parameterType="author"
statementType="PREPARED"
flushCache="true"
timeout="20"
databaseId=""
keyProperty=""
keyColumn=""
useGeneratedKeys="true"/>
~~~
????????????
???????????? 具體標簽屬性意義見2.2
?????????????3.1.2修改實例
??????????? 當其未設定主鍵為返回值時、返回的是數據庫中影響的行數。比如修改一條記錄、則方法的返回結果為1.
~~~
<update id="updateAuthor" parameterType="author" flushCache="true" statementType="PREPARED">
UPDATE author
SET username = #{username}
WHERE id = #{id}
</update>
~~~
### ???????3.2 刪除
?????????????3.2.1delete標簽及標簽屬性
???????????? 返回的是數據庫中影響的行數。比如刪除10條記錄、則方法的返回結果為10.
~~~
<delete
id=""
parameterType="author"
statementType="PREPARED"
flushCache="true"
timeout="20"/>
~~~
?????????????3.2.2實例
~~~
<delete id="deleteAuthor" parameterType="int">
DELETE FROM author
WHERE id = #{id}
</delete>
~~~
### 四:補充
?????? 更多內容:[Mybatis 目錄](http://blog.csdn.net/crave_shy/article/details/45825599)
?????? github地址:https://github.com/andyChenHuaYing/scattered-items/tree/master/items-mybatis
?????? 源碼下載地址:http://download.csdn.net/detail/chenghuaying/8713311