mybatis學習筆記2-SQL的簡單應用

來源:互聯網
上載者:User

簡單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">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.