簡單EG:
<select id=”selectPerson” parameterType=”int” resultType=”hashmap”>
SELECT * FROM PERSON WHERE ID = #{id}
</select>
這個語句被稱作selectPerson,使用一個int (或Integer)類型的參數,並返回一個HashMap
類型的對象,其中的鍵是列名,值是列對應的值。
注意參數註釋:
#{id}
這就告訴 MyBatis 創建一個預處理語句參數。使用 JDBC,這樣的一個參數在 SQL中會
由一個“?”來標識,並被傳遞到一個新的預處理語句中,就像這樣:
// Similar JDBC code, NOT MyBatis…
String selectPerson = “SELECT * FROM PERSON WHERE ID=?”;
PreparedStatement ps = conn.prepareStatement(selectPerson);
ps.setInt(1,id);
select 元素有很多屬性允許你配置,來決定每條語句的作用細節。
<select
id=”selectPerson” //唯一標示,用於引用該SQL
parameterType=”int” //語句參數類的名字或者別名
parameterMap=”deprecated” //已經廢棄
resultType=”hashmap” //返回的類型名或者別名
resultMap=”personResultMap” // 引用外部的定義好的resultMap
flushCache=”false” //是否清空緩存
useCache=”true” //將其設置爲 true,將會導致本條語句的結果被緩存
timeout=”10000” //這個設置驅動程序等待數據庫返回請求結果,並拋出異常時間的
最大等待值。默認不設置(驅動自行處理)
fetchSize=”256” //這是暗示驅動程序每次批量返回的結果行數。默認不設置(驅動
自行處理)。
statementType=”PREPARED” //STATEMENT,PREPARED 或 CALLABLE 的一種。這會讓 MyBat i
使用選擇使用 Statement,PreparedStatement或 CallableStatement。
默認值:PREPARED。
resultSetType=”FORWARD_ONLY” //FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE
中的一種。默認是不設置(驅動自行處理)。
>
<insert
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
keyProperty="" //(僅對insert有用) 標記一個屬性, MyBat is會通過getGeneratedKeys
或者通過 insert 語句的selectKey 子元素設置它的值。默認:不設置。
useGeneratedKeys="" //(僅對 insert 有 用 ) 這 會 告 訴 MyBat is 使用 JDBC 的
getGeneratedKeys 方法來取出由數據(比如:像 MySQL 和 SQL
Server 這樣的數據庫管理系統的自動遞增字段)內部生成的主鍵。
默認值:false。
timeout="20000">
<update
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000">
<delete
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000">
insert update delete的示例
<insert id="insertAuthor" parameterType="domain.blog.Author">
insert into Author (id,username,password,email,bio)
values (#{id},#{username},#{password},#{email},#{bio})
</insert>
<update id="updateAuthor" parameterType="domain.blog.Author">
update Author set
username = #{username},
password = #{password},
email = #{email},
bio = #{bio}
where id = #{id}
</update>
<delete id="deleteAuthor” parameterType="int">
delete from Author where id = #{id}
</delete>
如前所述,插入語句有一點多,它有一些屬性和子元素用來處理主鍵的生成。
首先,如果你的數據庫支持自動生成主鍵的字段(比如 MySQL 和 SQL Server),那麼
你可以設置 useGeneratedKeys=”true”,而且設置 keyProperty 到你已經做好的目標屬性上。
例如,如果上面的 Author 表已經對 id 使用了自動生成的列類型,那麼語句可以修改爲:
<insert id="insertAuthor" parameterType="domain.blog.Author"
useGeneratedKeys=”true” keyProperty=”id”>
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
</insert>
MyBat is 有另外一種方法來處理數據庫不支持自動生成類型,或者可能 JDBC 驅動不支
持自動生成主鍵時的主鍵生成問題。
這裏有一個簡單(甚至很傻)的示例,它可以生成一個隨機 ID(可能你不會這麼做,
但是這展示了 MyBatis 處理問題的靈活性,因爲它並不真的關心 ID 的生成):
<insert id="insertAuthor" parameterType="domain.blog.Author">
<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>
注:
<selectKey
keyProperty="id" //selectKey 語句結果應該被設置的目標屬性。
resultType="int" //結果的類型。MyBat is 通常可以算出來,但是寫上也沒有問題。
MyBat is 允許任何簡單類型用作主鍵的類型,包括字符串。
order="BEFORE" //這可以被設置爲 BEFORE 或 AFTER。如果設置爲 BEFORE,那
麼它會首先選擇主鍵,設置 keyProperty 然後執行插入語句。如果
設置爲 AFTER,那麼先執行插入語句,然後是 selectKey 元素-
這和如 Oracle 數據庫相似,可以在插入語句中嵌入序列調用。
statementType="PREPARED">